如何使用.NET 4.0读取.RTF文件

Job*_*Joy 10 c# wpf interop .net-4.0 .net-3.5

我见过使用Word 9.0对象库的示例.但我在VS2010中拥有Office 2010 Beta和.NET 4.0.有关如何使用新Word Dlls的任何提示?

所以我只是想用.NET3.5或更高版本获得RTF到TEXT的功能.

Job*_*Joy 10

我使用TextRange获得了WPF的更好解决方案.

FlowDocument document = new FlowDocument();

//Read the file stream to a Byte array 'data'
TextRange txtRange = null;

using (MemoryStream stream = new MemoryStream(data))
{
    // create a TextRange around the entire document
    txtRange = new TextRange(document.ContentStart, document.ContentEnd);
    txtRange.Load(stream, DataFormats.Rtf);
}
Run Code Online (Sandbox Code Playgroud)

现在,您可以在documentTextRange.Text中看到提取的文本


小智 5

你是否真的将.RTF加载到Word中?.net具有可以处理.RTF文件的RichTextBox控件.请参阅此处:http://msdn.microsoft.com/en-us/library/1z7hy77a.aspx(如何:将文件加载到Windows窗体RichTextBox控件中)