简体   繁体   English

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

[英]WPF RichTextBox Rtf property wrong converting non ascii chars

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

The RichTextBox cannot convert non ascii chars from my rtf string in my WPF application. 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;
}

Actual behavior: My RichTextbox display "Entête ".实际行为:我的 RichTextbox 显示“Entête”。 Problem with the conversion of "ê" (non ASCII chars) “ê”(非 ASCII 字符)的转换问题

Expected behavior: My RichTextbox display "Entête ".预期行为:我的 RichTextbox 显示“Entête”。 Problem with the conversion of "ê" “ê”的转换问题

The RTF string from code above does not standard-compliant RTF.上面代码中的RTF字符串不符合标准的 RTF。 The non-ASCII characters must be escaped!非 ASCII 字符必须转义! If replace the {\ltrch entête} fragment by this one {\ltrch Ent\'eate} the non-Unicode character will be displayed correctly.如果用这个{\ltrch Ent\'eate} eate} 替换{\ltrch entête}片段,非 Unicode 字符将正确显示。

In some cases, it can be assumed that the RTF documents created by different programs, or different versions of programs, may be different: to occurs a version incompatibility.在某些情况下,可以假设不同程序或不同版本的程序创建的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();
}

For additional information see:有关其他信息,请参阅:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM