【问题标题】:MS Word shows only first record from a rtf file created by TextWriterMS Word 仅显示 TextWriter 创建的 rtf 文件中的第一条记录
【发布时间】:2015-12-18 16:16:21
【问题描述】:

我正在使用 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

我尝试用谷歌搜索解决方案,但无济于事。 任何帮助将不胜感激。

【问题讨论】:

    标签: c# ms-word rtf textwriter wpf-extended-toolkit


    【解决方案1】:

    {\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>
    

    你真的想将正文标签的内容连接在一起,或者完全避免连接。

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

      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);
          }
      }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多