Converting from rich text format to plain text problems
Asked Answered
T

2

4

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.

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

Thunderpeal answered 1/5, 2012 at 20:11 Comment(2)
I don't think you're supposed to use windowing controls (anything from System.Windows.Forms) from a service. It might even be running on a headless box somewhere in a closet.Redistrict
Thanks Thomas,Yes I am aware of this, any suggestions for a reliable alternative for the rich text conversion?Thunderpeal
T
3

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+)?[ ]?", "");
Thunderpeal answered 10/5, 2012 at 21:28 Comment(0)
J
3

I would say this is probably thrown on terminal type machines that don't have the UI libraries installed? Or possibly don't have them loaded (ie - if no user is logged in)

It's generally not a great idea to use UI libraries in a service, because there is no guarantee that those libraries are accessible if no user is logged in.

I would find a different way to remove the RTF formatting

Junejuneau answered 1/5, 2012 at 20:25 Comment(1)
Thanks Jeremy, I am trying to find an alternative for the conversion.Thunderpeal
T
3

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+)?[ ]?", "");
Thunderpeal answered 10/5, 2012 at 21:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.