Convert from text to text

We currently have an application (Windows service) that connects to another of our applications and captures invoices. The invoices have an RTF field for the footer / header fields. When we capture data, 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 works for the most part, but in some cases (each specific client receives it every time). 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 will be greatly appreciated.

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

+4
source share
2 answers

I ended up using this. I know that he cannot parse 100% of the RTF text, but we used it for our live data to check it, and it works great for our purposes.

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

I would say that this is probably on terminal type machines that do not have user interface libraries installed? Or perhaps they are not loaded (i.e. - if no user is logged in)

As a rule, it is not recommended to use the user interface libraries in the service, because there is no guarantee that these libraries are available if no user has registered.

I would find another way to remove RTF formatting

+3
source

Source: https://habr.com/ru/post/986156/


All Articles