MS Word показывает только первую запись из файла RTF, созданного TextWriter

Я использую 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>) Значения в этой коллекции были преобразованы в RTF Xceed.Wpf.Toolkit.RichTextBox

"{\\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 фигурные скобки (}{ незадолго до 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

Я пытался найти решение Google, но безрезультатно. Любая помощь будет оценена.

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>

где на самом деле вы хотите объединить содержимое тегов 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);
      }
  }
Другие вопросы по тегам