I'm using RichTextBox to convert a string in RTF to plain text, using this piece of code:
private string ConvertToText(string rtf)
{
if (string.IsNullOrWhiteSpace(rtf)) return string.Empty;
if (!rtf.Contains("{\\rtf")) return rtf.Trim();
using (var helper = new System.Windows.Forms.RichTextBox())
{
helper.Rtf = rtf;
var plainText = helper.Text;
if (string.IsNullOrWhiteSpace(plainText)) return string.Empty;
return "<< Rule in Rich Text Format converted to Plain Text >>\n\n"
+ plainText
+ "\n\n<< Rule in Rich Text Format converted to Plain Text >>";
}
}
It works OK on all developers machines, but it does not work when deployed to an Azure Web Site:
{
"$id" :"1",
"Message" :"An error has occurred.",
"ExceptionMessage":"Out of memory.",
"ExceptionType" :"System.OutOfMemoryException",
"StackTrace" :"
at System.Drawing.Graphics.FromHdcInternal(IntPtr hdc)\r\n
at System.Drawing.Font.GetHeight()\r\n
at System.Drawing.Font.get_Height()\r\n
at System.Windows.Forms.Control.get_FontHeight()\r\n
at System.Windows.Forms.TextBoxBase.get_PreferredHeight()\r\n
at System.Windows.Forms.TextBoxBase.AdjustHeight(Boolean returnIfAnchored)\r\n
at System.Windows.Forms.TextBoxBase.set_Multiline(Boolean value)\r\n
at System.Windows.Forms.RichTextBox.set_Multiline(Boolean value)\r\n
at System.Windows.Forms.RichTextBox..ctor()\r\n
at ... "
}
It happens no matter the size of the RTF string. I'm already using the "Copy to Local" property of the System.Windows.Forms
reference. Does anyone have experience in dealing with RTF and Azure? Is there an alternative way to convert RTF to plain text?
This is an adapted C# version of this C++ forum link suggested by @Gusman on the comments:
It works! =D