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

C# 如何使用.NET 4.0读取.RTF文件,c#,wpf,.net-3.5,interop,.net-4.0,C#,Wpf,.net 3.5,Interop,.net 4.0,我见过使用Word 9.0对象库的示例。但我在VS2010中有Office2010测试版和.NET4.0。关于如何使用新单词DLL有什么建议吗 因此,我只想使用.NET3.5或更高版本将RTF的功能转换为文本。您真的不熟悉将.RTF加载到Word中吗。net具有可以处理.RTF文件的RichTextBox控件。请参见此处:(如何:将文件加载到Windows窗体RichTextBox控件)我用WPF找到了一个更好的解决方案,使用TextRange FlowDocument document = n

我见过使用Word 9.0对象库的示例。但我在VS2010中有Office2010测试版和.NET4.0。关于如何使用新单词DLL有什么建议吗


因此,我只想使用.NET3.5或更高版本将RTF的功能转换为文本。

您真的不熟悉将.RTF加载到Word中吗。net具有可以处理.RTF文件的RichTextBox控件。请参见此处:(如何:将文件加载到Windows窗体RichTextBox控件)

我用WPF找到了一个更好的解决方案,使用TextRange

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);
}
现在,您可以在documentTextRange.text中看到提取的文本

public enum eFileType
{
    Invalid = -1,
    TextDocument = 0,
    RichTextDocument,
    WordDocument
}

public interface IRead
{
    string Read(string file);
}

public static class FileManager
{
    public static eFileType GetFileType(string extension)
    {
        var type = eFileType.Invalid;
        switch (extension)
        {
            case ".txt": type = eFileType.TextDocument;
                break;
            case ".rtf": type = eFileType.RichTextDocument;
                break;
            case ".docx": type = eFileType.WordDocument;
                break;
        }
        return type;
    }
}


public class TextDocument : IRead
{
    public string Read(string file)
    {
        try
        {
            var reader = new StreamReader(file);
            var content = reader.ReadToEnd();
            reader.Close();
            return content;
        }
        catch
        {
            return null;
        }
    }
}

public class RichTextDocument : IRead
{
    public string Read(string file)
    {
        try
        {
            var wordApp = new Application();
            object path = file;
            object nullobj = System.Reflection.Missing.Value;
            var doc = wordApp.Documents.Open(ref path,
                                                  ref nullobj,
                                                  ref nullobj,
                                                  ref nullobj,
                                                  ref nullobj,
                                                  ref nullobj,
                                                  ref nullobj,
                                                  ref nullobj,
                                                  ref nullobj,
                                                  ref nullobj,
                                                  ref nullobj,
                                                  ref nullobj,
                                                  ref nullobj,
                                                  ref nullobj,
                                                  ref nullobj,
                                                  ref nullobj);
            var result = wordApp.ActiveDocument.Content.Text;
            var doc_close = (_Document)doc;
            doc_close.Close();
            return result;
        }
        catch
        {
            return null;
        }
    }
}

public class WordDocument : IRead
{
    public string Read(string file)
    {
        try
        {
            var wordApp = new Application();
            object path = file;
            object nullobj = System.Reflection.Missing.Value;
            var doc = wordApp.Documents.Open(ref path,
                                                  ref nullobj,
                                                  ref nullobj,
                                                  ref nullobj,
                                                  ref nullobj,
                                                  ref nullobj,
                                                  ref nullobj,
                                                  ref nullobj,
                                                  ref nullobj,
                                                  ref nullobj,
                                                  ref nullobj,
                                                  ref nullobj,
                                                  ref nullobj,
                                                  ref nullobj,
                                                  ref nullobj,
                                                  ref nullobj);
            var result = wordApp.ActiveDocument.Content.Text;
            var doc_close = (_Document)doc;
            doc_close.Close();
            return result;
        }
        catch
        {
            return null;
        }
    }
}

public class Factory
{
    public IRead Get(eFileType type)
    {
        IRead read = null;
        switch (type)
        {
            case eFileType.RichTextDocument: read = new RichTextDocument();
                break;
            case eFileType.WordDocument: read = new WordDocument();
                break;
            case eFileType.TextDocument: read = new TextDocument();
                break;
        }
        return read;
    }
}

public class ResumeReader
{
    IRead _read;
    public ResumeReader(IRead read)
    {
        if (read == null) throw new InvalidDataException("read cannot be null");

        _read = read;
    }
    public string Read(string file)
    {
        return _read.Read(file);
    }
}    

如果有人需要ASP.NET的解决方案,我发现了这个完美的解决方案:

添加对
System.Windows.Forms
或的引用,并对其进行引用

接下来,您可以通过创建一个临时的
RichTextBox
来提取文本:

RichTextBox box = new RichTextBox();
box.Rtf = File.ReadAllText(Path);
string text = box.Text;

文本实际上比这要难一点。您根本不考虑文本编码。