WPF RichTextBox Rtf属性转换非ascii字符错误



. NET Core Version: 3.1.405Windows版本:Windows 10

RichTextBox不能从我的WPF应用程序中的rtf字符串转换非ascii字符。

string rtf ="{\rtf1\ansi\ansicpg1252\uc1\htmautsp\deff2{\fonttbl{\f0\fcharset0 Times New Roman;}{\f2\fcharset0 Arial;}}                                                     {\colortbl\red0\green0\blue0;\red255\green255\blue255;}\loch\hich\dbch\pard\plain\ltrpar\itap0{\lang32\fs30\f2\cf0 \cf0\qj\sl15\slmult0{\f2 {\ltrch entête}\li0\ri0\sa0\sb0\fi0\qj\sl15\slmult0\par}}}"
if (rtf.Length > 2)
{
FlowDocument flowDocument = new FlowDocument
{
LineHeight = 1,
Language = XmlLanguage.GetLanguage(Thread.CurrentThread.CurrentUICulture.Name),                            
};
using (MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(rtf)))
{
TextRange text = new TextRange(flowDocument.ContentStart, flowDocument.ContentEnd);
if (stream.Length != 0)
{
text.Load(stream, DataFormats.Rtf);
}
text.ClearAllProperties();
}
return flowDocument;
}

实际行为:我的RichTextbox显示"Entê"转换"ê"(非ASCII字符)

预期行为:我的RichTextbox显示"Entête "。"ê">

转换问题

上面代码中的RTF字符串不符合标准的RTF。非ascii字符必须转义!如果将{ltrch entête}片段替换为{ltrch Ent'eate},则非unicode字符将正确显示。

在某些情况下,可以假设由不同程序或不同版本的程序创建的RTF文档可能不同:从而发生版本不兼容。

/// <summary>
/// This method loads the `rtf` string to the `rtb` RichTextBox control. Before loading any non ASCII characters is converting to escaped.
/// </summary>
/// <param name="rtb">The RichTextBox control to which the RTF-string will be loaded</param>
/// <param name="rtf">The RTF-string that will be loaded to the RichTextBox control. The string can contain non-ASCII characters.</param>
public void LoadRtfString(RichTextBox rtb, string rtf)
{
var flowDocument = new FlowDocument
{
LineHeight = 1,
Language = XmlLanguage.GetLanguage(Thread.CurrentThread.CurrentUICulture.Name),
};
using (MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(ConvertNonAsciiToEscaped(rtf))))
{
var text = new TextRange(flowDocument.ContentStart, flowDocument.ContentEnd);
text.ClearAllProperties();
if (stream.Length != 0)
{
text.Load(stream, DataFormats.Rtf);
}
}
rtb.Document = flowDocument;   
}
/// <param name="rtf">An RTF string that can contain non-ASCII characters and should be converted to correct format before loading to the RichTextBox control.</param>
/// <returns>The source RTF string with converted non ASCII to escaped characters.</returns>
public string ConvertNonAsciiToEscaped(string rtf)
{
var sb = new StringBuilder();
foreach (var c in rtf)
{
if (c <= 0x7f)
sb.Append(c);
else
sb.Append("\u" + Convert.ToUInt32(c) + "?");
}
return sb.ToString();
}

有关其他信息,请参阅:

  • Microsoft Office Word 2003富文本格式(RTF)规范
  • 富文本格式(RTF)规范,版本1.6

最新更新