Home > Back-end >  Converting between RTF and HTML
Converting between RTF and HTML

Time:09-24

On the (had the desire to convert some RTF text into HTML or take HTML and generate a RTF document? Probably not. But if you did, 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 called: XAML to HTML conversion Demo.

That The sample from The 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 The RTF to XAML and XAML to RTF easily. The key is to use System. Windows. Controls. RichTextBox which can load The RTF/XAML from a stream and save it as XAML/RTF.

RTF to XAML

C #
Private static string ConvertRtfToXaml (string rtfText)
{
Var richTextBox=new richTextBox ();
If (string. IsNullOrEmpty (rtfText)) return "";
Var textRange=new textRange (richTextBox) Document. ContentStart, richTextBox. Document. ContentEnd);
Using (var rtfMemoryStream=new MemoryStream ())
{
Using (var rtfStreamWriter=new StreamWriter (rtfMemoryStream))
{
RtfStreamWriter. Write (rtfText);
RtfStreamWriter. Flush ();
RtfMemoryStream. Seek (0, SeekOrigin. Begin);
TextRange. Load (rtfMemoryStream, DataFormats. Rtf);
}
}
Using (var rtfMemoryStream=new MemoryStream ())
{
TextRange=new textRange (richTextBox) Document. ContentStart, richTextBox. Document. ContentEnd);
TextRange. Save (rtfMemoryStream, DataFormats Xaml);
RtfMemoryStream. Seek (0, SeekOrigin. Begin);
Using (var rtfStreamReader=new StreamReader (rtfMemoryStream))
{
Return rtfStreamReader. ReadToEnd ();
}
}
}

XAML to RTF

C #
Private static string ConvertXamlToRtf (string xamlText)
{
Var richTextBox=new richTextBox ();
If (string. IsNullOrEmpty (xamlText)) return "";
Var textRange=new textRange (richTextBox) Document. ContentStart, richTextBox. Document. ContentEnd);
Using (var xamlMemoryStream=new MemoryStream ())
{
Using (var xamlStreamWriter=new StreamWriter (xamlMemoryStream))
{
XamlStreamWriter. Write (xamlText);
XamlStreamWriter. Flush ();
XamlMemoryStream. Seek (0, SeekOrigin. Begin);
TextRange. Load (xamlMemoryStream, DataFormats Xaml);
}
}
Using (var rtfMemoryStream=new MemoryStream ())
{
TextRange=new textRange (richTextBox) Document. ContentStart, richTextBox. Document. ContentEnd);
TextRange. Save (rtfMemoryStream, DataFormats. Rtf);
RtfMemoryStream. Seek (0, SeekOrigin. Begin);
Using (var rtfStreamReader=new StreamReader (rtfMemoryStream))
{
Return rtfStreamReader. ReadToEnd ();
}
}
}

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

C #
Public interface IMarkupConverter
{
String ConvertXamlToHtml (string xamlText);
String ConvertHtmlToXaml (string htmlText);
String ConvertRtfToHtml (string rtfText);
String ConvertHtmlToRtf (string htmlText);
}

Public class MarkupConverter: IMarkupConverter
{
Public string ConvertXamlToHtml (string xamlText)
{
Return HtmlFromXamlConverter. ConvertXamlToHtml (xamlText, false);
}

Public string ConvertHtmlToXaml (string htmlText)
{
Return HtmlToXamlConverter. ConvertHtmlToXaml (htmlText, true);
}

Public string ConvertRtfToHtml (string rtfText)
{
Return RtfToHtmlConverter. ConvertRtfToHtml (rtfText);
}

Public string ConvertHtmlToRtf (string htmlText)
{
Return HtmlToRtfConverter. ConvertHtmlToRtf (htmlText);
}
}

With this I am now able to convert from RTF to HTML and from HTML to RTF. 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 or ConvertHtmlToRtf functions, they must also be running in a STA. If you can’t have your program run in a STA (for example: when running an ASP .NET website) then you must create a new STA thread to run the conversion. Like this:

C #
MarkupConverter MarkupConverter=new MarkupConverter ();

Private string ConvertRtfToHtml (string rtfText)
{
Var thread=new thread (ConvertRtfInSTAThread);
Var threadData=https://bbs.csdn.net/topics/new ConvertRtfThreadData {RtfText=RtfText};
Thread. SetApartmentState (ApartmentState. STA);
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related