MS Word只显示由TextWriter创建的rtf文件中的第一条记录

本文关键字:文件 rtf 记录 一条 创建 Word 显示 TextWriter MS | 更新日期: 2023-09-27 18:27:23

我正在使用TextWriter制作一个基于字符串的RTF文件。

TextWriter tw = new StreamWriter(@"C:'test'test.rtf",false,Encoding.GetEncoding("iso-8859-1"));
tw.Write(text);

下面是我提供给TextWriter的字符串。我用String.Concat<T>(IEnumerable<T>)该集合中的值已由Xceed.Wpf.Toolkit.RichTextBox 转换为RTF

"{''rtf1''ansi''ansicpg1252''uc1''htmautsp''deff2{''fonttbl{''f0''fcharset0 Times New Roman;}{''f2''fcharset0 Segoe UI;}}{''colortbl'
'red0''green0''blue0;''red255''green255''blue255;}''loch''hich''dbch''pard''plain''ltrpar''itap0{''lang1033''fs24''f2''cf0 ''cf0''ql{'
'f2 {''b''ltrch test.}''li0''ri0''sa0''sb0''fi0''ql''par}'r'n{''f2 {''b''ltrch (test, test, test, test)}''li0''ri0''sa0''sb0''fi0''ql'
'par}'r'n{''f2 ''li0''ri0''sa0''sb0''fi0''ql''par}'r'n{''f2 {''ltrch test: test}''li0''ri0''sa0''sb0''fi0''ql''par}'r'n}'r'n}{''rtf1'
'ansi''ansicpg1252''uc1''htmautsp''deff2{''fonttbl{''f0''fcharset0 Times New Roman;}{''f2''fcharset0 Segoe UI;}}{''colortbl''red0'
'green0''blue0;''red255''green255''blue255;}''loch''hich''dbch''pard''plain''ltrpar''itap0{''lang1033''fs24''f2''cf0 ''cf0''ql{''f2 {'
'b''ltrch test.}''li0''ri0''sa0''sb0''fi0''ql''par}'r'n{''f2 {''b''ltrch (test, test, test, test)}''li0''ri0''sa0''sb0''fi0''ql''par}'r
'n{''f2 ''li0''ri0''sa0''sb0''fi0''ql''par}'r'n{''f2 {''ltrch test: test}''li0''ri0''sa0''sb0''fi0''ql''par}'r'n}'r'n}"

这是RTF文件:

{'rtf1'ansi'ansicpg1252'uc1'htmautsp'deff2{'fonttbl{'f0'fcharset0 Times New Roman;}{'f2'fcharset0 Segoe UI;}}{'colortbl
'red0'green0'blue0;'red255'green255'blue255;}'loch'hich'dbch'pard'plain'ltrpar'itap0{'lang1033'fs24'f2'cf0 'cf0'ql{'f2 {'b'ltrch 
test.}'li0'ri0'sa0'sb0'fi0'ql'par}
{'f2 {'b'ltrch (test, test, test, test)}'li0'ri0'sa0'sb0'fi0'ql'par}
{'f2 'li0'ri0'sa0'sb0'fi0'ql'par}
{'f2 {'ltrch test: test}'li0'ri0'sa0'sb0'fi0'ql'par}
}
}{'rtf1'ansi'ansicpg1252'uc1'htmautsp'deff2{'fonttbl{'f0'fcharset0 Times New Roman;}{'f2'fcharset0 Segoe UI;}}{'colortbl
'red0'green0'blue0;'red255'green255'blue255;}'loch'hich'dbch'pard'plain'ltrpar'itap0{'lang1033'fs24'f2'cf0 'cf0'ql{'f2 {'b'ltrch 
test.}'li0'ri0'sa0'sb0'fi0'ql'par}
{'f2 {'b'ltrch (test, test, test, test)}'li0'ri0'sa0'sb0'fi0'ql'par}
{'f2 'li0'ri0'sa0'sb0'fi0'ql'par}
{'f2 {'ltrch test: test}'li0'ri0'sa0'sb0'fi0'ql'par}
}
}

它在记事本中打开良好。但我必须删除2个大括号(第二个rtf1标记之前的}{)才能让MS Word显示整个文件:

{'rtf1'ansi'ansicpg1252'uc1'htmautsp'deff2{'fonttbl{'f0'fcharset0 Times New Roman;}{'f2'fcharset0 Segoe UI;}}{'colortbl
'red0'green0'blue0;'red255'green255'blue255;}'loch'hich'dbch'pard'plain'ltrpar'itap0{'lang1033'fs24'f2'cf0 'cf0'ql{'f2 {'b'ltrch 
test.}'li0'ri0'sa0'sb0'fi0'ql'par}
{'f2 {'b'ltrch (test, test, test, test)}'li0'ri0'sa0'sb0'fi0'ql'par}
{'f2 'li0'ri0'sa0'sb0'fi0'ql'par}
{'f2 {'ltrch test: test}'li0'ri0'sa0'sb0'fi0'ql'par}
}
'rtf1'ansi'ansicpg1252'uc1'htmautsp'deff2{'fonttbl{'f0'fcharset0 Times New Roman;}{'f2'fcharset0 Segoe UI;}}{'colortbl
'red0'green0'blue0;'red255'green255'blue255;}'loch'hich'dbch'pard'plain'ltrpar'itap0{'lang1033'fs24'f2'cf0 'cf0'ql{'f2 {'b'ltrch 
test.}'li0'ri0'sa0'sb0'fi0'ql'par}
{'f2 {'b'ltrch (test, test, test, test)}'li0'ri0'sa0'sb0'fi0'ql'par}
{'f2 'li0'ri0'sa0'sb0'fi0'ql'par}
{'f2 {'ltrch test: test}'li0'ri0'sa0'sb0'fi0'ql'par}
}
}

直到我去掉那些大括号,Word只显示第一条记录:

test.
(test, test, test, test)
test: test

而不是整个文档:

test.
(test, test, test, test)
test: test
test.
(test, test, test, test)
test: test

我试着用谷歌搜索一个解决方案,但没有成功。如有任何帮助,我们将不胜感激。

MS Word只显示由TextWriter创建的rtf文件中的第一条记录

{'rtf1代码告诉文字处理器文档已经开始;之后,它进行括号匹配,直到找到匹配的括号,这是}{对的第一个字符。从技术上讲,读者已经完成了它应该做的事情——阅读一个完整的RTF文档——然后停止阅读。

在我看来,你是在使用你的WPF控件来构建许多完整的RTF文档,然后将它们连接在一起,并希望Word能像阅读单个文档一样阅读它们,但事实并非如此。

因此,这就像通过将<html>元素连接在一起生成一个网页;

<html>
    <head>
    </head>
    <body>
        <p>para 1</para
    </body>
</html>
<html>
    <head>
    </head>
    <body>
        <p>para 1</para
    </body>
</html>

真正希望将body标记的内容连接在一起,或者根本避免连接。

我的猜测是,您正在转换一批文本,正确的方法是加载包含完整内容的控件。伪代码可能类似于;

  var ctrl = new Xceed.Wpf.Toolkit.RichTextBox();
  foreach(var textToConvert in input)
  {
       ctrl.AppendText(textToConvert);
  }
  var doc = ctrl.Document;
  var content = new TextRange(doc.ContentStart, doc.ContentEnd);
  if (content.CanSave(DataFormats.Rtf))
  {
      using (var stream = new MemoryStream())
      {
          content.Save(stream, DataFormats.Rtf);
      }
  }