[英]HTML to RTF string conversion in VB.NET
我有一個jquery Rich Textbox編輯器( http://jqueryte.com/ ),它允許最終用戶填寫其內容以生成Word文檔報告。
過程如下:
用戶填寫內容->將Richtext框內容的HTML保存到數據庫中。 ->從數據庫中提取存儲的HTML內容,並將其轉換為RTF字符串,以便在Microsoft Word中打開它。
我嘗試將HTML轉換為RTF(該函數將接受我的HTML字符串並給出等效的RTF字符串),但是它變得太復雜了,無法操作所有HTML標記。 我進行了很多搜索,但找不到該問題的任何解決方案(至少不是免費的解決方案除外)。
任何幫助將不勝感激。
提前致謝。
一種選擇是使用MS Office Word Interop(盡管有些人不贊成)...
string html = "<html><head><style>p{margin:0}</style></head><body style=\"font-family:Arial;\">" + value.Replace("<p> </p>", "<p><br></p>") + "</body></html>";
byte[] htmlBytes = Encoding.UTF8.GetBytes(html);
string htmlPath = Path.GetTempFileName() + ".html";
string rtfPath = htmlPath.Replace(".html", ".rtf");
FileStream fs = new FileStream(htmlPath, FileMode.Create, FileAccess.Write);
fs.Write(htmlBytes, 0, htmlBytes.Length);
fs.Close();
Application word = new Application();
Document doc = word.Documents.Open(htmlPath);
doc.SaveAs(rtfPath, WdSaveFormat.wdFormatRTF);
doc.Close();
word.Quit();
fs = new FileStream(rtfPath, FileMode.Open, FileAccess.Read);
byte[] rtfBytes = new byte[fs.Length];
fs.Read(rtfBytes, 0, rtfBytes.Length);
fs.Close();
string rtf = Encoding.ASCII.GetString(rtfBytes);
Thread thread = new Thread(() =>
{
RichTextBox rtb = new RichTextBox();
rtb.Rtf = rtf;
rtf = rtb.Rtf;
});
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
thread.Join();
return rtf;
我使用RichTextBox和Word的原因是Word的RTF文件非常龐大...當將Word的RTF數據輸入到RichTextBox中時,它會過濾掉所有不需要的代碼。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.