WPF RichTextBox以纯文本格式打开RTF文件

3

我试图在按钮点击时打开一个文件,在RichTextbox中以纯文本形式查看其内容。但是似乎没有什么能正常工作。

private void loadFile_Click(object sender, RoutedEventArgs e)
{
    OpenFileDialog openFile1 = new OpenFileDialog();
    openFile1.FileName = "Document"; 
    openFile1.DefaultExt = "*.*";
    openFile1.Filter = "All Files|*.*|Rich Text Format|*.rtf|Word Document|*.docx|Word 97-2003 Document|*.doc";

    if (openFile1.ShowDialog() == System.Windows.Forms.DialogResult.OK && openFile1.FileName.Length > 0)
    {
        //richTextbox1.Document.ContentStart = File.ReadAllText(openFile1.FileName);
    }
}

我正在使用WPF,但LoadFile方法不起作用。我希望能够从OpenFileDialog中选择一个文件,并将其作为纯文本加载到RichTextBox中。不需要添加文件格式的代码。
我想要的行为类似于打开.rtf文件,选择所有文本,然后将结果粘贴到RichTextBox中。如何通过单击按钮实现这一点?

我本以为你需要在Word中打开文件,选择所有文本并将RTB文本设置为内容。如果你在Notepad中打开一个 .doc或 .rtf文件,你会看到所有标头字符,这些是你似乎不想要的。("没有看到来自文件格式的添加代码") - CHill60
有没有不打开文件就能完成这个操作的方法? - MCSharp
请注意,您不能直接将 .doc.docx 文件加载到 RichTextBox 中。这些类型不包含 RTF 格式的文本,因此必须先将它们保存为 RTF 文件... - Spontifixus
3个回答

7
使用 TextRangeFileStream
if (openFile1.ShowDialog() == System.Windows.Forms.DialogResult.OK )
{             
  TextRange range;
  System.IO.FileStream fStream;

  if (System.IO.File.Exists(openFile1.FileName))
  {
      range = new TextRange(RichTextBox1.Document.ContentStart, RichTextBox1.Document.ContentEnd);
      fStream = new System.IO.FileStream(openFile1.FileName, System.IO.FileMode.OpenOrCreate);
      range.Load(fStream, System.Windows.DataFormats.Rtf );

      fStream.Close();
  }
}

1
尝试将 System.Windows.DataFormats.Text 更改为 System.Windows.DataFormats.Rtf - paul
这将打开文件并作为RTF读取。但它不会粘贴纯文本,也无法与其他格式(如.doc和.docx)一起使用。 - MCSharp
我已经编辑了标题,以纯文本形式打开RTF文件,因为无法在richtextbox中打开.docx文件。这是最好的答案。 - MCSharp
请使用range.ClearAllProperties()将此答案转换为纯文本。 - jimbojones
1
一个很棒的WPF解决方案,可以将RTF文件转换为纯文本。使用range.Text来输出纯文本。我尝试了大约10次谷歌搜索才找到这个解决方案。 - Gary

0

与 @AbZy 类似,您需要先清除格式:

    private void loadFile_Click(object sender, RoutedEventArgs routedEventArgs)
    {
        OpenFileDialog openFile1 = new OpenFileDialog();
        openFile1.FileName = "Document";
        openFile1.DefaultExt = "*.*";
        openFile1.Filter = "All Files|*.*|Rich Text Format|*.rtf|Word Document|*.docx|Word 97-2003 Document|*.doc";

        if (openFile1.ShowDialog() == true)
        {
            var range = new TextRange(rtf.Document.ContentStart, rtf.Document.ContentEnd);

            using (var fStream = new FileStream(openFile1.FileName, FileMode.OpenOrCreate))
            {
                // load as RTF, text is formatted
                range.Load(fStream, DataFormats.Rtf);
                fStream.Close();
            }
            // clear the formatting, turning into plain text
            range.ClearAllProperties();
        }
    }

这个方法对于RTF文档非常有效,并能去除格式。那么如何将其应用到Word文档格式呢? - MCSharp
简短回答是您需要先将 .doc 或 .docx 转换为 RTF。请参见 http://stackoverflow.com/questions/4647018/fetch-rtf-from-word/4647132 。RichTextbox 支持 RTF,而 Word .doc 和 .docx 拥有比 RTF 更多的功能。 - jimbojones

0
你尝试过使用 richTextbox1.AppendText(File.ReadAllText(openFile1.FileName)) 吗?

虽然速度较慢,但您可以尝试使用以下代码:richTextBox.Selection.Load(stream,DataFormats.Rtf); - Ernesto Alfonso

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接