Converting RTF to HTML

Converting RTF to HTML

Have you ever had the desire to convert some RTF text into HTML? Probably not. But if you do, then you are in luck! I recently had the need to do this conversion and after some searching found out a way to do it by enhancing a sample distributed in the MSDN library. The sample is called: XAML to HTML Conversion Demo.

The sample has code which converts HTML to and from a XAML Flow Document. But this doesn’t make things easier until you realize that there is a way to convert RTF to XAML easily. The key is to use System.Windows.Controls.RichTextBox which can load RTF from a stream and save it as XAML. This conversion is shown below:

With this code we have all we need to convert RTF to HTML. I modified the sample to add this RTF To XAML conversation and then I run that XAML through HTML converter which results in the HTML text. I added an interface to these conversion utilities and converted the sample into a library so that I would be able to use it from other projects. Here is the interface:

publicinterface IMarkupConverter

{

string ConvertXamlToHtml(string xamlText);

string ConvertHtmlToXaml(string htmlText);

string ConvertRtfToHtml(string rtfText);

}

publicclass MarkupConverter : IMarkupConverter

{

publicstring ConvertXamlToHtml(string xamlText)

{

return HtmlFromXamlConverter.ConvertXamlToHtml(xamlText, false);

}

publicstring ConvertHtmlToXaml(string htmlText)

{

return HtmlToXamlConverter.ConvertHtmlToXaml(htmlText, true);

}

publicstring ConvertRtfToHtml(string rtfText)

{

return RtfToHtmlConverter.ConvertRtfToHtml(rtfText);

}

}

With this I am now able to convert from RTF to HTML. However, there is one catch - the conversion uses the RichTextBox WPF control which requires a single threaded apartment (STA). Therefore in order to run your code that calls the ConvertRtfToHtml function, it must also be running in a STA. If you can’t have your program run in a STA then you must create a new STA thread to run the conversion. Like this: