Created
August 27, 2014 13:46
-
-
Save vbfox/65f854f851326752dccd to your computer and use it in GitHub Desktop.
HTML Fragment for clipboard
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static class HtmlFragment | |
{ | |
const string HTML_FRAGMENT_HEADER = "Version:0.9\r\nStartHTML:{0:000000}\r\nEndHTML:{1:000000}" | |
+ "\r\nStartFragment:{2:000000}\r\nEndFragment:{3:000000}\r\n"; | |
const string HTML_FRAGMENT_BEGIN = "<html>\r\n<head>\r\n" | |
+ "<meta http-equiv=\"Content-Type\" content=\"text/html; charset={0}\">\r\n</head>\r\n" | |
+ "<body>\r\n<!--StartFragment-->"; | |
const string HTML_FRAGMENT_END = "<!--EndFragment-->\r\n</body>\r\n</html>\r\n"; | |
static readonly Encoding htmlFragmentEncoding = Encoding.UTF8; | |
static string HtmlToHtmlFragment(string html) | |
{ | |
Debug.Assert(html != null); | |
/* Regarding the specific format of HTML fragments | |
* See the MSDN : http://msdn.microsoft.com/en-us/library/aa767917.aspx | |
* Or this blog : http://tommycarlier.blogspot.com/2005/08/copying-html-fragment-to-clipboard.html | |
*/ | |
var htmlBegin = string.Format(HTML_FRAGMENT_BEGIN, htmlFragmentEncoding.WebName); | |
var beginSample = String.Format(HTML_FRAGMENT_HEADER, 0, 0, 0, 0); | |
int countBegin = htmlFragmentEncoding.GetByteCount(beginSample); | |
int countHtmlBegin = htmlFragmentEncoding.GetByteCount(htmlBegin); | |
int countHtml = htmlFragmentEncoding.GetByteCount(html); | |
int countHtmlEnd = htmlFragmentEncoding.GetByteCount(HTML_FRAGMENT_END); | |
var finalBegin = String.Format(HTML_FRAGMENT_HEADER, countBegin, | |
countBegin + countHtmlBegin + countHtml + countHtmlEnd, | |
countBegin + countHtmlBegin, | |
countBegin + countHtmlBegin + countHtml); | |
return finalBegin + htmlBegin + html + HTML_FRAGMENT_END; | |
} | |
static MemoryStream HtmlToHtmlFragmentStream(string html) | |
{ | |
Debug.Assert(html != null); | |
var htmlTotal = HtmlToHtmlFragment(html); | |
return new MemoryStream(Encoding.UTF8.GetBytes(htmlTotal)); | |
} | |
public static void SetHtmlFragment(this DataObject dataObject, string html) | |
{ | |
if (dataObject == null) throw new ArgumentNullException("dataObject"); | |
if (html == null) throw new ArgumentNullException("html"); | |
var stream = HtmlToHtmlFragmentStream(html); | |
dataObject.SetData(DataFormats.Html, stream); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class HtmlFragmentProgram | |
{ | |
void Main() | |
{ | |
var dataObj = new DataObject(); | |
dataObj.SetHtmlFragment("<table border=1><tr><td><b>1.22</b></td><td>1.98</td></td></table>"); | |
dataObj.Dump(); | |
Clipboard.SetDataObject(dataObj); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment