从富文本格式转换为纯文本问题 [英] Converting from rich text format to plain text problems

查看:381
本文介绍了从富文本格式转换为纯文本问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们目前有一个应用程序(Windows 服务)可以连接到我们的另一个应用程序并获取发票.在发票中有一个用于页脚/页眉字段的 RTF 字段.当我们抓取数据时,RTF 会通过以下代码转换为纯文本:

We currently have an application (Windows service) that connects to another of our applications and grabs invoices. In the invoices there is a RTF field for the footer/header fields. When we grab the data the RTF is converted to plain text with the following code:

public static string ConvertFromRTFToPlainText(string rtfString)
{
    if (rtfString == null)
        return null; 

    System.Windows.Forms.RichTextBox rtBox = new System.Windows.Forms.RichTextBox();

    if (rtfString.StartsWith("{\\rtf1"))
        rtBox.Rtf = rtfString;
    else
        rtBox.Text = rtfString;

    return rtBox.Text;
}

这在大部分情况下都有效,但在某些情况下(一个特定的客户每次都会得到它)我得到这个例外:

This has worked for the most part but in some cases (one particular client gets it everytime) I get this exception:

Exception Message:Error creating window handle.
Stack trace:
at System.Windows.Forms.NativeWindow.CreateHandle(CreateParams cp)
at System.Windows.Forms.Control.CreateHandle()
at System.Windows.Forms.TextBoxBase.CreateHandle()
at System.Windows.Forms.RichTextBox.set_Rtf(String value)
at SmartTrade.Common.API.Tools.RTFHelperUtility.ConvertFromRTFToPlainText(String rtfString)
at SmartTrade.Desktop.Proxy.API.ObjectMapper.InvoiceObjectMapper.CovertToAPIInvoice(Invoice domainInvoice)

任何有关为什么会发生这种情况或我们如何解决它的帮助将不胜感激.

Any help on why this is happening or how we can work around it would be much appreciated.

编辑:感谢 Jeremy 的解释,我正在寻找 RTF 转换替代方案的建议.

Edit: Thanks to Jeremy for the explanation, I am after suggestions for RTF conversion alternatives.

推荐答案

我最终使用了这个.我知道它可能无法解析 100% 的 RTF 文本,但我们针对我们的实时数据运行它以对其进行测试,并且它可以很好地满足我们的目的.

I ended up using this. I know that it may not parse 100% of RTF text but we ran it against our live data to test it and it works fine for our purposes.

Regex.Replace(rtfString, @"\{\*?\\[^{}]+}|[{}]|\\\n?[A-Za-z]+\n?(?:-?\d+)?[ ]?", "");

这篇关于从富文本格式转换为纯文本问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆