3

我正在尝试将 rtf 文档加载到 RichTextBox,如下所示:

private void LoadTextDocument(string fileName)
{
    Rtb.Document.Blocks.Clear();
    TextRange range;
    System.IO.FileStream fStream;
    if (System.IO.File.Exists(fileName))
    {
        range = new TextRange(Rtb.Document.ContentStart, Rtb.Document.ContentEnd);
        fStream = new System.IO.FileStream(fileName, System.IO.FileMode.Open);
        range.Load(fStream, System.Windows.DataFormats.Rtf);
        fStream.Close();
    }
}

但是文档中包含的公式高于应该

截屏:

4

1 回答 1

1

问题是,.NET RTF 框甚至不能与使用 OpenOffice、Word 或其他编辑器创建的 RTF 文件几乎 100% 兼容。所以我担心这项任务甚至不是那么容易甚至是不可能的。我花了几天时间尝试使用 RichTextBox 找到解决不同问题的方法(替换在 Word 中创建的更复杂的 RTF 中的文本)。

最后,我使用了 RegEx 解决方案并直接在文件中替换文本而不使用 RTB。

于 2013-11-19T18:15:33.427 回答