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.9
StartHTML:0000000224
EndHTML:0000001613
StartFragment:0000000260
EndFragment:0000001577
SourceURL: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
The code that returned the HTML content is :
var obj=System.Windows.Forms.Clipboard.GetDataObject();
var html=obj.GetData("HTML Format");
Console.WriteLine(html);
The browser adds the data using multiple formats. The following line
Console.WriteLine(obj.GetFormats());
Returned 6 different formats :
- HTML Format
- System.String
- UnicodeText
- Text
- Locale
- OEMText
The reason for all this is that the clipboard is used to transfer data between applications that DON'T use or understand the same formats. The source application adds data to the clipboard in the formats it understands. It's the source's responsibility to put as much data in the clipboard as possible to ensure the content can be reproduced on the other end.
Target applications request the clipboard data in the format they understand. The clipboard can try and convert from one format to another. When that's not possible, it will return NULL
.
Multiple Formats
To support multiple targets, an application can create one DataObject that contains multiple formats. The following code adds an HTML payload that can be pasted into Word and a Text payload that can be pasted into any text editor:
var html = @"Version:0.9
StartHTML:0000000055
EndHTML:0000000088
<a href='#'>Click me!</a><br/>";
var plainText=@"<a href='#'>Click me!</a><br/>";
var obj=new DataObject();
obj.SetText(html,TextDataFormat.Html);
obj.SetText(plainText,TextDataFormat.Text);
Clipboard.SetDataObject(obj);