Insert HTML Formatted Text On Word Document Using C# [modified]
-
Hello, I have created word document using Microsoft.Interop Library,for that i have to insert text which is contain html tags,now i need to formatt that text and putup that html text on word document,means need to put up html formatted text on word document using c#. Thanks,
nikunj padaliya
modified on Tuesday, May 26, 2009 12:44 AM
-
Hello, I have created word document using Microsoft.Interop Library,for that i have to insert text which is contain html tags,now i need to formatt that text and putup that html text on word document,means need to put up html formatted text on word document using c#. Thanks,
nikunj padaliya
modified on Tuesday, May 26, 2009 12:44 AM
it is late answer i know )) Create a word doc, and insert a bookmark into that, then you can paste your string to that bookmarked place wit trhe code below private void button2_Click(object sender, EventArgs e) { WordApp = new Word.ApplicationClass(); fileName = @"yourworddocname"; Microsoft.Office.Interop.Word.Document aDoc = WordApp.Documents.Open(ref fileName, ref missing, ref readOnly, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref Format, ref realencode, ref isVisible, ref missing, ref missing, ref missing, ref missing); WordApp.Visible = true; WordApp.Visible = true; bookmarkdene(aDoc, "yourbookmarinworddoc", yaz); } private static string HtmlClipboardData(string html) { StringBuilder sb = new StringBuilder(); Encoding encoding = Encoding.GetEncoding("utf-8"); string Header = @" Version: 1.0 StartHTML: {0:000000} EndHTML: {1:000000} StartFragment: {2:000000} EndFragment: {3:000000} "; string HtmlPrefix = @" !DOCTYPE HTML PUBLIC ""-//W3C//DTD HTML 4.0 Transitional//TR"" html head meta http-equiv=Content-Type content=""text/html; charset={0}"" head body !--StartFragment-- "; HtmlPrefix = string.Format(HtmlPrefix, encoding.WebName); string HtmlSuffix = @" <!--EndFragment--> </body> </html> "; // Get lengths of chunks int HeaderLength = encoding.GetByteCount(Header); HeaderLength -= 16; // extra formatting characters {0:000000} int PrefixLength = encoding.GetByteCount(HtmlPrefix); int HtmlLength = encoding.GetByteCount(html); int SuffixLength = encoding.GetByteCount(HtmlSuffix); // Determine locations of chunks int StartHtml = HeaderLength; int StartFragment = StartHtml + PrefixLength; int EndFragment = StartFragment + HtmlLength; int EndHtml = EndFragment + SuffixLength; // Build the data sb.AppendFormat(Header, StartHtml, EndHtml, StartFragment, EndFragment); sb.Append(HtmlPrefix); sb.Ap