从Word获取RTF
我有一个Word文档,并希望导出包括格式为RTF(或HTML)的内容。
Word.Application wordApp = new Word.Application(); Word.Document currentDoc = wordApp.Documents.Open("file.docx"); currentDoc.SaveAs("file.rtf", Word.WdSaveFormat.wdFormatRTF); currentDoc = wordApp.Documents.Open("file.rtf"); Word.Range range = currentDoc.Range(); String RTFText = range.Text;
我已经尝试过上面的代码,但我似乎只得到了Text,没有hte格式。
有任何想法吗?
如果您想阅读rtf代码,请尝试使用:
Word.Application wordApp = new Word.Application(); Word.Document currentDoc = wordApp.Documents.Open("file.docx"); currentDoc.SaveAs("file.rtf", Word.WdSaveFormat.wdFormatRTF);
然后像普通文本文件一样打开它:
string rtf = File.ReadAllText("file.rtf");
使用您的方法不起作用,因为您正在访问Text属性,因此Word只提供纯文本。