E-Nuff E-Nuff -4 years ago 331
C# Question

Unrecognized structure in data format 'Rich Text Format'

I want to convert

RTF
to
HTML
. But I get an Error in the
TextRange.Load
Function. I have the following Code:

private static string ConvertRtfToXaml(string rtfText)
{
var richTextBox = new System.Windows.Controls.RichTextBox();
if (string.IsNullOrEmpty(rtfText)) return "";

var textRange = new TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd);

//Create a MemoryStream of the Rtf content

using (var rtfMemoryStream = new MemoryStream())
{
using (var rtfStreamWriter = new StreamWriter(rtfMemoryStream))
{
rtfStreamWriter.Write(rtfText);
rtfStreamWriter.Flush();
rtfMemoryStream.Seek(0, SeekOrigin.Begin);

//Load the MemoryStream into TextRange ranging from start to end of RichTextBox.
textRange.Load(rtfMemoryStream, System.Windows.Forms.DataFormats.Rtf);
}
}

using (var rtfMemoryStream = new MemoryStream())
{

textRange = new TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd);
textRange.Save(rtfMemoryStream, System.Windows.DataFormats.Xaml);
rtfMemoryStream.Seek(0, SeekOrigin.Begin);
using (var rtfStreamReader = new StreamReader(rtfMemoryStream))
{
return rtfStreamReader.ReadToEnd();
}
}

}


The Function
textRange.Load(rtfMemoryStream, System.Windows.Forms.DataFormats.Rtf);
throws the following Error:


Unrecognized structure in data format 'Rich Text Format'.
Parameter name: stream


My RTF string looks like this:

{\rtf1\ansi\ansicpg1252\deff0\deflang1031{\fonttbl{\f0\fnil\fcharset0Arial;}}\viewkind4\uc1\pard\fs20 Hallo\par
\par
Wir bitten um Auftragsbest\''e4tigung und Liefertermin\fs20\par
}


The RTF string looks normal to me, where is the Parameter stream?

Answer Source

The RTF is invalid, as it contains \''e4 - there are two apostrophes here when there should be only one.

RTF Special Characters

\'hh A hexadecimal value, based on the specified character set (may be used to identify 8-bit values).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download