我想将RTF
转换为HTML
。但是我在TextRange.Load
函数中遇到错误。我有以下代码:
private static string ConvertRtfToXaml(string rtfText)
{
var richTextBox = new System.Windows.Controls.RichTextBox();
if (string.IsNullOrEmpty(rtfText)) return "";
var textRange = new TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd);
//Create a MemoryStream of the Rtf content
using (var rtfMemoryStream = new MemoryStream())
{
using (var rtfStreamWriter = new StreamWriter(rtfMemoryStream))
{
rtfStreamWriter.Write(rtfText);
rtfStreamWriter.Flush();
rtfMemoryStream.Seek(0, SeekOrigin.Begin);
//Load the MemoryStream into TextRange ranging from start to end of RichTextBox.
textRange.Load(rtfMemoryStream, System.Windows.Forms.DataFormats.Rtf);
}
}
using (var rtfMemoryStream = new MemoryStream())
{
textRange = new TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd);
textRange.Save(rtfMemoryStream, System.Windows.DataFormats.Xaml);
rtfMemoryStream.Seek(0, SeekOrigin.Begin);
using (var rtfStreamReader = new StreamReader(rtfMemoryStream))
{
return rtfStreamReader.ReadToEnd();
}
}
}
函数textRange.Load(rtfMemoryStream, System.Windows.Forms.DataFormats.Rtf);
抛出以下错误:
无法识别的数据格式“富文本格式”结构。 参数名称:stream
我的RTF字符串如下所示:
{\rtf1\ansi\ansicpg1252\deff0\deflang1031{\fonttbl{\f0\fnil\fcharset0Arial;}}\viewkind4\uc1\pard\fs20 Hallo\par
\par
Wir bitten um Auftragsbest\''e4tigung und Liefertermin\fs20\par
}
RTF字符串对我来说很正常,参数流在哪里?
答案 0 :(得分:0)
你有一个错误的逃脱/ unescape:
\''
将其更改为
\'
TextRange.Load
应该是:
textRange.Load(rtfMemoryStream, System.Windows.DataFormats.Rtf);
在程序集PresentationCore
中定义了System.Windows.DataFormats.Rtf
。
答案 1 :(得分:0)