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

Posted

技术标签:

【中文标题】如何使用 .NET 4.0 读取 .RTF 文件【英文标题】:How to read an .RTF file using .NET 4.0 【发布时间】:2011-01-18 16:13:30 【问题描述】:

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

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

【问题讨论】:

【参考方案1】:

我得到了一个更好的 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 中看到提取的文本

【讨论】:

【参考方案2】:

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

【讨论】:

【参考方案3】:
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);
    
    

修改语法高亮显示

【讨论】:

文本实际上是相当困难的那样。您根本没有考虑文本编码。【参考方案4】:

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

添加对System.Windows.Forms 或download the DLL itself 的引用并对其进行引用。

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

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

【讨论】:

以上是关于如何使用 .NET 4.0 读取 .RTF 文件的主要内容,如果未能解决你的问题,请参考以下文章

如何在CRichEditCtrl控件中直接读如RTF格式的文件(这个是通过流的方式来读取文件)

C#/VB.NET 将RTF转为PDF

如何为asp.net 4.0编写IIS预热脚本?

在 XmlReader .NET 4.0 中加载失败目录文件

Notepad++ Rtf 编辑插件

如何使用 php 保存 rtf 文件?