2015-01-14 7 views
1

최종 사용자가 내용을 입력하여 단어 문서 보고서를 생성 할 수있는 jquery 서식있는 텍스트 편집기 (http://jqueryte.com/)가 있습니다.VB.NET에서 HTML to RTF 문자열 변환

과정은 다음과 같습니다 :

사용자는 콘텐츠 채워 -> 데이터베이스에 서식있는 텍스트 상자 내용의 HTML을 저장. -> 저장된 HTML 컨텐트를 데이터베이스에서 가져 와서 Microsoft Word에서 열 수 있도록 RTF 문자열로 변환하십시오.

HTML을 RTF (HTML 문자열을 가져 와서 그에 해당하는 RTF 문자열을 제공하는 함수)로 변환하려고했지만 모든 HTML 태그를 조작하기에는 너무 복잡해졌습니다. 나는 많은 것을 수색했으나 문제에 대한 해결책은 찾지 못했다.

도움을 주시면 감사하겠습니다.

미리 감사드립니다.

+0

jquery rte 구문과 word rte 구문이 같지 않다고 생각합니다. –

+0

어떻게 전환을 달성 할 수 있습니까? –

답변

0

하나의 옵션은

string html = "<html><head><style>p{margin:0}</style></head><body style=\"font-family:Arial;\">" + value.Replace("<p>&nbsp;</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; 

말씀이 말씀의 RTF 파일이 매우 부피가 큰 것을 그대로 나는뿐만 아니라하여 RichTextBox를 사용한 이유 ... (일부에 의해 눈살을 찌푸리게하지만)가 MS 오피스 워드 상호 운용성을 사용하는 것입니다 ... Word의 RTF 데이터가 RichTextBox에 입력되면 불필요한 코드가 모두 필터링됩니다.