Pages

Friday, 26 November 2010

How to copy an HTML content to the Clipboard (C#)

If you want to copy an HTML content to the Clipboard you have to understand the concept of HTML Clipboard Format. When you copy a piece of HTML using Ctrl+C shortcut, OS creates previously mentioned header for you but if you want to achieve the same thing in code you have to prepare the header by yourself.

   1:      Version:0.9
   2:      StartHTML:71
   3:      EndHTML:170
   4:      StartFragment:140
   5:      EndFragment:160
   6:      StartSelection:140
   7:      EndSelection:160
   8:      <!DOCTYPE>
   9:      <HTML>
  10:      <HEAD>
  11:      <TITLE>The HTML Clipboard</TITLE>
  12:      <BASE HREF="http://sample/specs"> 
  13:      </HEAD>
  14:      <BODY>
  15:      <!--StartFragment -->
  16:      <P>The Fragment</P>
  17:      <!--EndFragment -->
  18:      </BODY>
  19:      </HTML>

Header explanation:
  • Version vv - Version number of the clipboard. Starting version is 0.9.
  • StartHTML - Byte count from the beginning of the clipboard to the start of the context, or -1 if no context.
  • EndHTML - Byte count from the beginning of the clipboard to the end of the context, or -1 if no context.
  • StartFragment - Byte count from the beginning of the clipboard to the start of the fragment.
  • EndFragment - Byte count from the beginning of the clipboard to the end of the fragment.
  • StartSelection - Byte count from the beginning of the clipboard to the start of the selection.
  • EndSelection - Byte count from the beginning of the clipboard to the end of the selection.
According to the Microsoft StartSelection and EndSelection attributes are optional because sufficient information is provided in the fragment section. Below you can find an example of method which you can use for coping an HTML content to the Clipboard under the condition that input parameter 'fullHtmlContent' contains all necessary tags (a fully functional HTML document).


   1:          public void CopyToClipboard(string fullHtmlContent)
   2:          {
   3:              System.Text.StringBuilder sb = new System.Text.StringBuilder();
   4:              string header = @"Version:1.0
   5:                              StartHTML:<<<<<<<1
   6:                              EndHTML:<<<<<<<2
   7:                              StartFragment:<<<<<<<3
   8:                              EndFragment:<<<<<<<4";
   9:              sb.Append(header);
  10:              int startHTML = sb.Length;
  11:              sb.Append(fullHtmlContent);
  12:              int endHTML = sb.Length;
  13:   
  14:              sb.Replace("<<<<<<<1", startHTML.To8CharsString());
  15:              sb.Replace("<<<<<<<2", endHTML.To8CharsString());
  16:              sb.Replace("<<<<<<<3", startHTML.To8CharsString());
  17:              sb.Replace("<<<<<<<4", endHTML.To8CharsString());
  18:   
  19:              Clipboard.Clear();
  20:              Clipboard.SetText(sb.ToString(), TextDataFormat.Html);
  21:          }

   1:          public static string To8CharsString(this int x)
   2:          {
   3:              return x.ToString("0#######");
   4:          }

Useful links:

No comments:

Post a Comment