How to Set HTML to Clipboard in C#

How to set HTML to clipboard in C#?

When setting HTML text, you need to provide a header with additional information to what fragment of the html you actually want to paste while being able to provide additional styling around it:

Version:0.9
StartHTML:000125
EndHTML:000260
StartFragment:000209
EndFragment:000222
<HTML>
<head>
<title>HTML clipboard</title>
</head>
<body>
<!–StartFragment–><b>Hello!</b><!–EndFragment–>
</body>
</html>

With the header (and correct indexes), calling Clipboard.SetText with TextDataFormat.Html will do the trick.

To handle HTML and plain text pastes, you can’t use the Clipboard.SetText method, as it clears the clipboard each time it’s called; you need to create a DataObject instance, call its SetData method once with HTML and once with plain text, and then set the object to clipboard using Clipboard.SetDataObject.

Update

See "Setting HTML/Text to Clipboard revisited" for more details and ClipboardHelper implementation.

How to copy both - HTML and text to the clipboard?

You can NOT use Clipboard.SetData for setting both HTML and plain text. The second call of SetData will clear the content of clipboard that has been set by first call and store the new data.

You should use DataObject and Clipboard.SetDataObject().

Example:

DataObject dataObj = new DataObject();
dataObj.SetData(DataFormats.Html, htmlWithHeader);
dataObj.SetData(DataFormats.Text, plainText);

Clipboard.SetDataObject(dataObj);

Clipboard.SetText(HTML) no longer work

You can see if this works for you. I use this pretty regularly.

It looks very similar to yours though, and it is is c# but you can use an online code translator.

   public void SetHyperlinkOnClipboard( string link, string description )
{
try
{
const string sContextStart = "<HTML><BODY><!--StartFragment -->";
const string sContextEnd = "<!--EndFragment --></BODY></HTML>";
const string m_sDescription = "Version:0.9" + Constants.vbCrLf + "StartHTML:How to Set HTML to Clipboard in C#aa" + Constants.vbCrLf + "EndHTML:bb" + Constants.vbCrLf + "StartFragment:cccccccccc" + Constants.vbCrLf + "EndFragment:

Related Topics

dd" + Constants.vbCrLf;

string sHtmlFragment = "<A HREF=" + Strings.Chr( 34 ) + link + Strings.Chr( 34 ) + ">" + description + "</A>";

string sData = m_sDescription + sContextStart + sHtmlFragment + sContextEnd;
sData = sData.Replace( "How to Set HTML to Clipboard in C#aa", m_sDescription.Length.ToString().PadLeft( 10, '0' ) );
sData = sData.Replace( "bb", sData.Length.ToString().PadLeft( 10, '0' ) );
sData = sData.Replace( "cccccccccc", ( m_sDescription + sContextStart ).Length.ToString().PadLeft( 10, '0' ) );
sData = sData.Replace( "

Related Topics

dd", ( m_sDescription + sContextStart + sHtmlFragment ).Length.ToString().PadLeft( 10, '0' ) );
Clipboard.SetDataObject( new DataObject( DataFormats.Html, sData ), true );
}
catch( Exception ex )
{

}
}

How do I copy formatted HTML string into a Clipboard for paste using C#?

Unless something has changed since the olden days (.NET 2, which was the last time I tried this) you need to add the proper clipboard header lines before putting it on the clipboard, as described here:

http://blogs.msdn.com/b/jmstall/archive/2007/01/21/html-clipboard.aspx

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

How do I copy text to the clipboard with partial italics?

It seems you have to insert a 'header' in the html string, I found 2 examples for this:

  • MSDN blog
  • StackOverFlow

This works with Word:

Example code:

    Clipboard.SetText(@"Version:1.0
StartHTML:000125
EndHTML:000260
StartFragment:000209
EndFragment:000222
<HTML>
<head>
<title>HTML clipboard</title>
</head>
<body>
<!–StartFragment–><b>Hello!</b><!–EndFragment–>
</body>
</html>",
TextDataFormat.Html);

This copies a Hello! to your clipboard, you have to change the fragments based on the size I think so I don't know exactly how that would work with a dynamic string but I hope this will get you started. 666

If you can also use RTF

Clipboard.SetText(@"{\rtf1\ansi This is in \i\f0\fs17 italic\i0.}",
TextDataFormat.Rtf);

Example with string

var q = "test123";
Clipboard.SetText(@"{\rtf1\ansi This is in \i\f0\fs17 " + q + @"\i0.}",
TextDataFormat.Rtf);

or

var q = "test123";
Clipboard.SetText( $@"{\rtf1\ansi This is in \i\f0\fs17 {q}\i0.}",
TextDataFormat.Rtf);

Note the @ before the 2nd part of the string, if you need to escape certain characters (you will need that with RTF) add @ in front of each opening ".

This seems to be a lot easier because you don't have to insert the header but the formatting itself is abit more complicated imo.



Related Topics



Leave a reply



Submit