Paste Formatted Text, Not Images or HTML

Why I cannot paste formatted text copied to clipboard?

The HTML data format doesn't mean the payload is plain HTML. The structure of the content is described in the docs, in HTML Clipboard Format. The Version, StartHTML and EndHTML elements are mandatory.

The following code will put an HTML snippet in the Clipboard that can be pasted in applications that understand the HTML format, like Word :

var text = @"Version:0.9
StartHTML:0000000055
EndHTML:0000000088
<a href='#'>Click me!</a><br/>";

System.Windows.Forms.Clipboard.SetText(text,TextDataFormat.Html);

Without those elements the text isn't recognized as an HTML payload and can't be pasted.

The HTML format can contain more than the snippet. If someone copies text from a browser, the HTML payload may end up containing styles, fonts and indexes that aren't part of the actual snippet but are necessary to allow correct rendering. This question's title looks like this when copied:

Version:0.9StartHTML:0000000224EndHTML:0000001613StartFragment:0000000260EndFragment:0000001577SourceURL:https://stackoverflow.com/questions/51750187/why-i-cannot-paste-formatted-text-copied-to-clipboard#51750187<html><body><!--StartFragment--><h1 itemprop="name" class="grid--cell fs-headline1 fl1" style="margin: 0px 0px 0.5em; padding: 0px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: inherit; font-stretch: inherit; line-height: 1.3; font-family: Arial, "Helvetica Neue", Helvetica, sans-serif; font-size: 2.07692rem !important; vertical-align: baseline; box-sizing: inherit; flex: 1 1 auto !important; color: rgb(36, 39, 41); letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;"><a href="https://stackoverflow.com/questions/51750187/why-i-cannot-paste-formatted-text-copied-to-clipboard" class="question-hyperlink" style="margin: 0px 0px 0.5em; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: normal; font-stretch: inherit; line-height: 1.35; font-family: inherit; font-size: 24px; vertical-align: baseline; box-sizing: inherit; color: rgb(36, 39, 41); text-decoration: none; cursor: pointer;">Why I cannot paste formatted text copied to clipboard?</a></h1><!--EndFragment--></body></html>00

Text/Html JTextPane not rendering properly when pasting formatted text

It's a common mistake. You need to set :

((HTMLDocument)myJTextPane.getDocument()).setPreservesUnknownTags(false);
//considering that you already equipped yout JTextPane with an HTMLDocument.
//Note that HTMLEditorKit automatically installs one.

I believe that's enough to fix your issue.

quickly turn copied content (formatted text, images) to the clipboard into local markdown files and images

My first choice meanwhile is this: https://github.com/euangoddard/clipboard2markdown.
By far the easiest solution. It nicely handles the images in particular. Kudos to the dev!

Other apps paste the HTML rather than the text version of our pasteboard data in iOS 5

For those of you playing along at home, here's the answer we discovered.

"public.utf8-plain-text" has historically been the correct UTI to use when putting an NSString containing unformatted text into the pasteboard. The built-in controls all request and use this version of the pasteboard contents during their "paste" operations (for iOS versions prior to 5). If you put only "public.plain-text" or "public.text" text in the pasteboard, the built-in controls ignore it completely, saying the pasteboard is empty (not giving you the "paste" option).

In iOS 5, something changed and when the built-in controls request plain text in the situation above, they will get the "public.html" text.

For iOS 5 you must use "public.text" instead of "public.plain-text" or "public.utf8-plain-text" even though the latter two are arguably more correct and the former is too vague to be of use at all.

Since earlier iOS versions ignore "public.text", our solution is to put all three versions on the pasteboard: "public.text" and "public.utf8-plain-text" will both get you the plain text and "public.html" will get you the HTML text. This seems to satisfy both iOS 4 and 5 without having to put an explicit iOS version test in the code, at the cost of one more entry in the dictionary.

One more fail for the kids at Apple.

EDIT for 2016 and iOS 8/9

I've been trying to solve this problem once and for all ever since I posted this question. Whenever I do a Google search, I always end up back at this question.

Somewhere along the line, iOS introduced a "web archive" concept for putting HTML on the pasteboard. It's not documented well anywhere. I found an answer here which pre-dates my question, so that's a little frustrating, but it works. I've updated it to take advantage of built-in base64 encoding in later versions of iOS. It goes something like this:

NSMutableDictionary * contents = [NSMutableDictionary dictionaryWithCapacity:6];
NSString * htmlText = @"<h1>A Heading</h1><p>A paragraph.</p>"
//... put other formats in the dictionary, then...
NSData * data = [htmlText dataUsingEncoding:NSUTF8StringEncoding];
NSString * base64encodedString = [data base64EncodedStringWithOptions:0];
NSString * webArchiveString = [NSString stringWithFormat:
@"<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
"<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">"
"<plist version=\"1.0\">"
"<dict>"
"<key>WebMainResource</key>"
"<dict>"
"<key>WebResourceData</key>"
"<data>%@</data>"
"<key>WebResourceFrameName</key>"
"<string></string>"
"<key>WebResourceMIMEType</key>"
"<string>text/html</string>"
"<key>WebResourceTextEncodingName</key>"
"<string>UTF-8</string>"
"<key>WebResourceURL</key>"
"<string>about:blank</string>"
"</dict>"
"</dict>"
"</plist>", base64encodedString];
[contents setObject:webArchiveString forKey:@"Apple Web Archive pasteboard type"];
UIPasteboard * pasteboard = [UIPasteboard generalPasteboard];
pasteboard.items = [NSArray arrayWithObject:contents];

How can I copy rich text contents to the clipboard with JavaScript?

i searched for a week now and finally found my answer!!!
for those of you looking to copy rich text to the clipboard with javascript, then use the function at the link below, works like a charm.
no need of flash and other stuff suggested :)

Copying an image to clipboard using JavaScript/jquery

How to paste rich text into a UITextView?

Duncan asked above, "Are you sure you have the uitextview set to handle rich text." I wasn't aware of a setting for that, but I checked the UITextView class reference again and found the allowsEditingTextAttributes property. I hadn't used that before because I was providing my own formatting buttons and didn't need to enable the system Bold/Italic/Underline options. But when I set that property to YES for my UITextView, then it started accepting formatted text automatically. Whew!



Related Topics



Leave a reply



Submit