<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Matthew Manela&#039;s Home Page &#187; HTML</title>
	<atom:link href="http://matthewmanela.com/category/html/feed/" rel="self" type="application/rss+xml" />
	<link>http://matthewmanela.com</link>
	<description>The life and work of Matthew Manela</description>
	<lastBuildDate>Sun, 25 Jul 2010 18:00:44 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Converting RTF to HTML</title>
		<link>http://matthewmanela.com/2009/09/28/converting-rtf-to-html/</link>
		<comments>http://matthewmanela.com/2009/09/28/converting-rtf-to-html/#comments</comments>
		<pubDate>Mon, 28 Sep 2009 21:28:00 +0000</pubDate>
		<dc:creator>MattManela</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[RTF]]></category>
		<category><![CDATA[WPF]]></category>
		<category><![CDATA[XAML]]></category>

		<guid isPermaLink="false">http://blogs.msdn.com/matt/archive/2009/09/28/converting-rtf-to-html.aspx</guid>
		<description><![CDATA[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...]]></description>
			<content:encoded><![CDATA[<P>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.&nbsp; The sample is called: <A href="http://msdn.microsoft.com/en-us/library/aa972129.aspx" mce_href="http://msdn.microsoft.com/en-us/library/aa972129.aspx">XAML to HTML Conversion Demo</A>.&nbsp; </P>
<P>The sample has code which converts HTML to and from a XAML Flow Document.&nbsp; 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 <A title=http://msdn.microsoft.com/en-us/library/system.windows.controls.richtextbox.aspx href="http://msdn.microsoft.com/en-us/library/system.windows.controls.richtextbox.aspx" mce_href="http://msdn.microsoft.com/en-us/library/system.windows.controls.richtextbox.aspx">System.Windows.Controls.RichTextBox</A> which can load RTF from a stream and save it as XAML.&nbsp; This conversion is shown below:</P><PRE style="BORDER-BOTTOM: #cecece 1px solid; BORDER-LEFT: #cecece 1px solid; PADDING-BOTTOM: 5px; BACKGROUND-COLOR: #fbfbfb; MIN-HEIGHT: 40px; PADDING-LEFT: 5px; WIDTH: 650px; PADDING-RIGHT: 5px; OVERFLOW: auto; BORDER-TOP: #cecece 1px solid; BORDER-RIGHT: #cecece 1px solid; PADDING-TOP: 5px"><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">        <SPAN style="COLOR: #0000ff">private</SPAN> <SPAN style="COLOR: #0000ff">static</SPAN> <SPAN style="COLOR: #0000ff">string</SPAN> ConvertRtfToXaml(<SPAN style="COLOR: #0000ff">string</SPAN> rtfText)
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">        {
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">            var richTextBox = <SPAN style="COLOR: #0000ff">new</SPAN> RichTextBox();
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">            <SPAN style="COLOR: #0000ff">if</SPAN> (<SPAN style="COLOR: #0000ff">string</SPAN>.IsNullOrEmpty(rtfText)) <SPAN style="COLOR: #0000ff">return</SPAN> "<SPAN style="COLOR: #8b0000"></SPAN>";
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px"></PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">            var textRange = <SPAN style="COLOR: #0000ff">new</SPAN> TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd);
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px"></PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">            <SPAN style="COLOR: #0000ff">using</SPAN> (var rtfMemoryStream = <SPAN style="COLOR: #0000ff">new</SPAN> MemoryStream())
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">            {
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">                <SPAN style="COLOR: #0000ff">using</SPAN> (var rtfStreamWriter = <SPAN style="COLOR: #0000ff">new</SPAN> StreamWriter(rtfMemoryStream))
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">                {
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">                    rtfStreamWriter.Write(rtfText);
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">                    rtfStreamWriter.Flush();
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">                    rtfMemoryStream.Seek(0, SeekOrigin.Begin);
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px"></PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">                    textRange.Load(rtfMemoryStream, DataFormats.Rtf);
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">                }
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">            }
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px"></PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">            <SPAN style="COLOR: #0000ff">using</SPAN> (var rtfMemoryStream = <SPAN style="COLOR: #0000ff">new</SPAN> MemoryStream())
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">            {
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px"></PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">                textRange = <SPAN style="COLOR: #0000ff">new</SPAN> TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd);
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">                textRange.Save(rtfMemoryStream, DataFormats.Xaml);
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">                rtfMemoryStream.Seek(0, SeekOrigin.Begin);
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">                <SPAN style="COLOR: #0000ff">using</SPAN> (var rtfStreamReader = <SPAN style="COLOR: #0000ff">new</SPAN> StreamReader(rtfMemoryStream))
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">                {
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">                    <SPAN style="COLOR: #0000ff">return</SPAN> rtfStreamReader.ReadToEnd();
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">                }
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">            }
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px"></PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">        }</PRE></PRE>
<P>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.&nbsp; Here is the interface:</P><PRE style="BORDER-BOTTOM: #cecece 1px solid; BORDER-LEFT: #cecece 1px solid; PADDING-BOTTOM: 5px; BACKGROUND-COLOR: #fbfbfb; MIN-HEIGHT: 40px; PADDING-LEFT: 5px; WIDTH: 650px; PADDING-RIGHT: 5px; OVERFLOW: auto; BORDER-TOP: #cecece 1px solid; BORDER-RIGHT: #cecece 1px solid; PADDING-TOP: 5px"><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px"> <SPAN style="COLOR: #0000ff">public</SPAN> <SPAN style="COLOR: #0000ff">interface</SPAN> IMarkupConverter
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">    {
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">        <SPAN style="COLOR: #0000ff">string</SPAN> ConvertXamlToHtml(<SPAN style="COLOR: #0000ff">string</SPAN> xamlText);
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">        <SPAN style="COLOR: #0000ff">string</SPAN> ConvertHtmlToXaml(<SPAN style="COLOR: #0000ff">string</SPAN> htmlText);
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">        <SPAN style="COLOR: #0000ff">string</SPAN> ConvertRtfToHtml(<SPAN style="COLOR: #0000ff">string</SPAN> rtfText);
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">    }
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px"></PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">    <SPAN style="COLOR: #0000ff">public</SPAN> <SPAN style="COLOR: #0000ff">class</SPAN> MarkupConverter : IMarkupConverter
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">    {
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">        <SPAN style="COLOR: #0000ff">public</SPAN> <SPAN style="COLOR: #0000ff">string</SPAN> ConvertXamlToHtml(<SPAN style="COLOR: #0000ff">string</SPAN> xamlText)
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">        {
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">            <SPAN style="COLOR: #0000ff">return</SPAN> HtmlFromXamlConverter.ConvertXamlToHtml(xamlText, <SPAN style="COLOR: #0000ff">false</SPAN>);
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">        }
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px"></PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">        <SPAN style="COLOR: #0000ff">public</SPAN> <SPAN style="COLOR: #0000ff">string</SPAN> ConvertHtmlToXaml(<SPAN style="COLOR: #0000ff">string</SPAN> htmlText)
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">        {
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">            <SPAN style="COLOR: #0000ff">return</SPAN> HtmlToXamlConverter.ConvertHtmlToXaml(htmlText, <SPAN style="COLOR: #0000ff">true</SPAN>);
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">        }
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px"></PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">        <SPAN style="COLOR: #0000ff">public</SPAN> <SPAN style="COLOR: #0000ff">string</SPAN> ConvertRtfToHtml(<SPAN style="COLOR: #0000ff">string</SPAN> rtfText)
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">        {
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">            <SPAN style="COLOR: #0000ff">return</SPAN> RtfToHtmlConverter.ConvertRtfToHtml(rtfText);
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">        }
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">    }</PRE></PRE>
<P>With this I am now able to convert from RTF to HTML.&nbsp; However, there is one catch - the conversion uses the RichTextBox WPF control which requires a single threaded apartment (STA).&nbsp; Therefore in order to run your code that calls the ConvertRtfToHtml function, it must also be running in a STA.&nbsp; 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:</P><PRE style="BORDER-BOTTOM: #cecece 1px solid; BORDER-LEFT: #cecece 1px solid; PADDING-BOTTOM: 5px; BACKGROUND-COLOR: #fbfbfb; MIN-HEIGHT: 40px; PADDING-LEFT: 5px; WIDTH: 650px; PADDING-RIGHT: 5px; OVERFLOW: auto; BORDER-TOP: #cecece 1px solid; BORDER-RIGHT: #cecece 1px solid; PADDING-TOP: 5px"><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">MarkupConverter markupConverter = <SPAN style="COLOR: #0000ff">new</SPAN> MarkupConverter();
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px"></PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px"><SPAN style="COLOR: #0000ff">private</SPAN> <SPAN style="COLOR: #0000ff">string</SPAN> ConvertRtfToHtml(<SPAN style="COLOR: #0000ff">string</SPAN> rtfText)
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">{
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">   var thread = <SPAN style="COLOR: #0000ff">new</SPAN> Thread(ConvertRtfInSTAThread);
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">   var threadData = <SPAN style="COLOR: #0000ff">new</SPAN> ConvertRtfThreadData { RtfText = rtfText };
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">   thread.SetApartmentState(ApartmentState.STA);
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">   thread.Start(threadData);
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">   thread.Join();
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">   <SPAN style="COLOR: #0000ff">return</SPAN> threadData.HtmlText;
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">}
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px"></PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px"><SPAN style="COLOR: #0000ff">private</SPAN> <SPAN style="COLOR: #0000ff">void</SPAN> ConvertRtfInSTAThread(<SPAN style="COLOR: #0000ff">object</SPAN> rtf)
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">{
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">   var threadData = rtf <SPAN style="COLOR: #0000ff">as</SPAN> ConvertRtfThreadData;
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">   threadData.HtmlText = markupConverter.ConvertRtfToHtml(threadData.RtfText);
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">}
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">        
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px"><SPAN style="COLOR: #0000ff">private</SPAN> <SPAN style="COLOR: #0000ff">class</SPAN> ConvertRtfThreadData
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">{
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">   <SPAN style="COLOR: #0000ff">public</SPAN> <SPAN style="COLOR: #0000ff">string</SPAN> RtfText { <SPAN style="COLOR: #0000ff">get</SPAN>; <SPAN style="COLOR: #0000ff">set</SPAN>; }
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">   <SPAN style="COLOR: #0000ff">public</SPAN> <SPAN style="COLOR: #0000ff">string</SPAN> HtmlText { <SPAN style="COLOR: #0000ff">get</SPAN>; <SPAN style="COLOR: #0000ff">set</SPAN>; }
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">}</PRE></PRE>
<P>Here is the zip contain the code for the Markup converter: <A href="http://blogs.msdn.com/matt/attachment/9900386.ashx" mce_href="http://blogs.msdn.com/matt/attachment/9900386.ashx">MarkupConverter.zip</A></P><img src="http://blogs.msdn.com/aggbug.aspx?PostID=9900386" width="1" height="1">]]></content:encoded>
			<wfw:commentRss>http://matthewmanela.com/2009/09/28/converting-rtf-to-html/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
<enclosure url="http://blogs.msdn.com/matt/attachment/9900386.ashx" length="64021" type="application/x-zip-compressed" />
		</item>
		<item>
		<title>Converting RTF to HTML</title>
		<link>http://matthewmanela.com/2009/09/28/converting-rtf-to-html-2/</link>
		<comments>http://matthewmanela.com/2009/09/28/converting-rtf-to-html-2/#comments</comments>
		<pubDate>Mon, 28 Sep 2009 21:28:00 +0000</pubDate>
		<dc:creator>MattManela</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[RTF]]></category>
		<category><![CDATA[WPF]]></category>
		<category><![CDATA[XAML]]></category>

		<guid isPermaLink="false">/b/matt/archive/2009/09/28/converting-rtf-to-html.aspx</guid>
		<description><![CDATA[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...]]></description>
			<content:encoded><![CDATA[<P>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.&nbsp; The sample is called: <A href="http://msdn.microsoft.com/en-us/library/aa972129.aspx" mce_href="http://msdn.microsoft.com/en-us/library/aa972129.aspx">XAML to HTML Conversion Demo</A>.&nbsp; </P>
<P>The sample has code which converts HTML to and from a XAML Flow Document.&nbsp; 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 <A title=http://msdn.microsoft.com/en-us/library/system.windows.controls.richtextbox.aspx href="http://msdn.microsoft.com/en-us/library/system.windows.controls.richtextbox.aspx" mce_href="http://msdn.microsoft.com/en-us/library/system.windows.controls.richtextbox.aspx">System.Windows.Controls.RichTextBox</A> which can load RTF from a stream and save it as XAML.&nbsp; This conversion is shown below:</P><PRE style="BORDER-BOTTOM: #cecece 1px solid; BORDER-LEFT: #cecece 1px solid; PADDING-BOTTOM: 5px; BACKGROUND-COLOR: #fbfbfb; MIN-HEIGHT: 40px; PADDING-LEFT: 5px; WIDTH: 650px; PADDING-RIGHT: 5px; OVERFLOW: auto; BORDER-TOP: #cecece 1px solid; BORDER-RIGHT: #cecece 1px solid; PADDING-TOP: 5px"><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">        <SPAN style="COLOR: #0000ff">private</SPAN> <SPAN style="COLOR: #0000ff">static</SPAN> <SPAN style="COLOR: #0000ff">string</SPAN> ConvertRtfToXaml(<SPAN style="COLOR: #0000ff">string</SPAN> rtfText)
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">        {
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">            var richTextBox = <SPAN style="COLOR: #0000ff">new</SPAN> RichTextBox();
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">            <SPAN style="COLOR: #0000ff">if</SPAN> (<SPAN style="COLOR: #0000ff">string</SPAN>.IsNullOrEmpty(rtfText)) <SPAN style="COLOR: #0000ff">return</SPAN> "<SPAN style="COLOR: #8b0000"></SPAN>";
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px"></PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">            var textRange = <SPAN style="COLOR: #0000ff">new</SPAN> TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd);
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px"></PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">            <SPAN style="COLOR: #0000ff">using</SPAN> (var rtfMemoryStream = <SPAN style="COLOR: #0000ff">new</SPAN> MemoryStream())
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">            {
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">                <SPAN style="COLOR: #0000ff">using</SPAN> (var rtfStreamWriter = <SPAN style="COLOR: #0000ff">new</SPAN> StreamWriter(rtfMemoryStream))
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">                {
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">                    rtfStreamWriter.Write(rtfText);
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">                    rtfStreamWriter.Flush();
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">                    rtfMemoryStream.Seek(0, SeekOrigin.Begin);
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px"></PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">                    textRange.Load(rtfMemoryStream, DataFormats.Rtf);
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">                }
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">            }
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px"></PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">            <SPAN style="COLOR: #0000ff">using</SPAN> (var rtfMemoryStream = <SPAN style="COLOR: #0000ff">new</SPAN> MemoryStream())
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">            {
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px"></PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">                textRange = <SPAN style="COLOR: #0000ff">new</SPAN> TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd);
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">                textRange.Save(rtfMemoryStream, DataFormats.Xaml);
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">                rtfMemoryStream.Seek(0, SeekOrigin.Begin);
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">                <SPAN style="COLOR: #0000ff">using</SPAN> (var rtfStreamReader = <SPAN style="COLOR: #0000ff">new</SPAN> StreamReader(rtfMemoryStream))
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">                {
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">                    <SPAN style="COLOR: #0000ff">return</SPAN> rtfStreamReader.ReadToEnd();
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">                }
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">            }
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px"></PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">        }</PRE></PRE>
<P>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.&nbsp; Here is the interface:</P><PRE style="BORDER-BOTTOM: #cecece 1px solid; BORDER-LEFT: #cecece 1px solid; PADDING-BOTTOM: 5px; BACKGROUND-COLOR: #fbfbfb; MIN-HEIGHT: 40px; PADDING-LEFT: 5px; WIDTH: 650px; PADDING-RIGHT: 5px; OVERFLOW: auto; BORDER-TOP: #cecece 1px solid; BORDER-RIGHT: #cecece 1px solid; PADDING-TOP: 5px"><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px"> <SPAN style="COLOR: #0000ff">public</SPAN> <SPAN style="COLOR: #0000ff">interface</SPAN> IMarkupConverter
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">    {
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">        <SPAN style="COLOR: #0000ff">string</SPAN> ConvertXamlToHtml(<SPAN style="COLOR: #0000ff">string</SPAN> xamlText);
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">        <SPAN style="COLOR: #0000ff">string</SPAN> ConvertHtmlToXaml(<SPAN style="COLOR: #0000ff">string</SPAN> htmlText);
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">        <SPAN style="COLOR: #0000ff">string</SPAN> ConvertRtfToHtml(<SPAN style="COLOR: #0000ff">string</SPAN> rtfText);
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">    }
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px"></PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">    <SPAN style="COLOR: #0000ff">public</SPAN> <SPAN style="COLOR: #0000ff">class</SPAN> MarkupConverter : IMarkupConverter
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">    {
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">        <SPAN style="COLOR: #0000ff">public</SPAN> <SPAN style="COLOR: #0000ff">string</SPAN> ConvertXamlToHtml(<SPAN style="COLOR: #0000ff">string</SPAN> xamlText)
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">        {
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">            <SPAN style="COLOR: #0000ff">return</SPAN> HtmlFromXamlConverter.ConvertXamlToHtml(xamlText, <SPAN style="COLOR: #0000ff">false</SPAN>);
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">        }
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px"></PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">        <SPAN style="COLOR: #0000ff">public</SPAN> <SPAN style="COLOR: #0000ff">string</SPAN> ConvertHtmlToXaml(<SPAN style="COLOR: #0000ff">string</SPAN> htmlText)
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">        {
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">            <SPAN style="COLOR: #0000ff">return</SPAN> HtmlToXamlConverter.ConvertHtmlToXaml(htmlText, <SPAN style="COLOR: #0000ff">true</SPAN>);
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">        }
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px"></PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">        <SPAN style="COLOR: #0000ff">public</SPAN> <SPAN style="COLOR: #0000ff">string</SPAN> ConvertRtfToHtml(<SPAN style="COLOR: #0000ff">string</SPAN> rtfText)
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">        {
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">            <SPAN style="COLOR: #0000ff">return</SPAN> RtfToHtmlConverter.ConvertRtfToHtml(rtfText);
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">        }
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">    }</PRE></PRE>
<P>With this I am now able to convert from RTF to HTML.&nbsp; However, there is one catch - the conversion uses the RichTextBox WPF control which requires a single threaded apartment (STA).&nbsp; Therefore in order to run your code that calls the ConvertRtfToHtml function, it must also be running in a STA.&nbsp; 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:</P><PRE style="BORDER-BOTTOM: #cecece 1px solid; BORDER-LEFT: #cecece 1px solid; PADDING-BOTTOM: 5px; BACKGROUND-COLOR: #fbfbfb; MIN-HEIGHT: 40px; PADDING-LEFT: 5px; WIDTH: 650px; PADDING-RIGHT: 5px; OVERFLOW: auto; BORDER-TOP: #cecece 1px solid; BORDER-RIGHT: #cecece 1px solid; PADDING-TOP: 5px"><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">MarkupConverter markupConverter = <SPAN style="COLOR: #0000ff">new</SPAN> MarkupConverter();
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px"></PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px"><SPAN style="COLOR: #0000ff">private</SPAN> <SPAN style="COLOR: #0000ff">string</SPAN> ConvertRtfToHtml(<SPAN style="COLOR: #0000ff">string</SPAN> rtfText)
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">{
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">   var thread = <SPAN style="COLOR: #0000ff">new</SPAN> Thread(ConvertRtfInSTAThread);
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">   var threadData = <SPAN style="COLOR: #0000ff">new</SPAN> ConvertRtfThreadData { RtfText = rtfText };
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">   thread.SetApartmentState(ApartmentState.STA);
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">   thread.Start(threadData);
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">   thread.Join();
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">   <SPAN style="COLOR: #0000ff">return</SPAN> threadData.HtmlText;
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">}
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px"></PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px"><SPAN style="COLOR: #0000ff">private</SPAN> <SPAN style="COLOR: #0000ff">void</SPAN> ConvertRtfInSTAThread(<SPAN style="COLOR: #0000ff">object</SPAN> rtf)
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">{
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">   var threadData = rtf <SPAN style="COLOR: #0000ff">as</SPAN> ConvertRtfThreadData;
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">   threadData.HtmlText = markupConverter.ConvertRtfToHtml(threadData.RtfText);
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">}
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">        
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px"><SPAN style="COLOR: #0000ff">private</SPAN> <SPAN style="COLOR: #0000ff">class</SPAN> ConvertRtfThreadData
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">{
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">   <SPAN style="COLOR: #0000ff">public</SPAN> <SPAN style="COLOR: #0000ff">string</SPAN> RtfText { <SPAN style="COLOR: #0000ff">get</SPAN>; <SPAN style="COLOR: #0000ff">set</SPAN>; }
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">   <SPAN style="COLOR: #0000ff">public</SPAN> <SPAN style="COLOR: #0000ff">string</SPAN> HtmlText { <SPAN style="COLOR: #0000ff">get</SPAN>; <SPAN style="COLOR: #0000ff">set</SPAN>; }
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">}</PRE></PRE>
<P>Here is the zip contain the code for the Markup converter: <A href="http://blogs.msdn.com/matt/attachment/9900386.ashx" mce_href="http://blogs.msdn.com/matt/attachment/9900386.ashx">MarkupConverter.zip</A></P><img src="http://blogs.msdn.com/aggbug.aspx?PostID=9900386" width="1" height="1">]]></content:encoded>
			<wfw:commentRss>http://matthewmanela.com/2009/09/28/converting-rtf-to-html-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
<enclosure url="http://blogs.msdn.com/cfs-file.ashx/__key/CommunityServer-Components-PostAttachments/00-09-90-03-86/MarkupConverter.zip" length="64021" type="application/x-zip-compressed" />
		</item>
		<item>
		<title>Converting RTF to HTML</title>
		<link>http://matthewmanela.com/2009/09/28/converting-rtf-to-html-3/</link>
		<comments>http://matthewmanela.com/2009/09/28/converting-rtf-to-html-3/#comments</comments>
		<pubDate>Mon, 28 Sep 2009 21:28:00 +0000</pubDate>
		<dc:creator>MattManela</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[RTF]]></category>
		<category><![CDATA[WPF]]></category>
		<category><![CDATA[XAML]]></category>

		<guid isPermaLink="false">/b/matt/archive/2009/09/28/converting-rtf-to-html.aspx</guid>
		<description><![CDATA[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...]]></description>
			<content:encoded><![CDATA[<P>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.&nbsp; The sample is called: <A href="http://msdn.microsoft.com/en-us/library/aa972129.aspx" mce_href="http://msdn.microsoft.com/en-us/library/aa972129.aspx">XAML to HTML Conversion Demo</A>.&nbsp; </P>
<P>The sample has code which converts HTML to and from a XAML Flow Document.&nbsp; 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 <A title=http://msdn.microsoft.com/en-us/library/system.windows.controls.richtextbox.aspx href="http://msdn.microsoft.com/en-us/library/system.windows.controls.richtextbox.aspx" mce_href="http://msdn.microsoft.com/en-us/library/system.windows.controls.richtextbox.aspx">System.Windows.Controls.RichTextBox</A> which can load RTF from a stream and save it as XAML.&nbsp; This conversion is shown below:</P><PRE style="BORDER-BOTTOM: #cecece 1px solid; BORDER-LEFT: #cecece 1px solid; PADDING-BOTTOM: 5px; BACKGROUND-COLOR: #fbfbfb; MIN-HEIGHT: 40px; PADDING-LEFT: 5px; WIDTH: 650px; PADDING-RIGHT: 5px; OVERFLOW: auto; BORDER-TOP: #cecece 1px solid; BORDER-RIGHT: #cecece 1px solid; PADDING-TOP: 5px"><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">        <SPAN style="COLOR: #0000ff">private</SPAN> <SPAN style="COLOR: #0000ff">static</SPAN> <SPAN style="COLOR: #0000ff">string</SPAN> ConvertRtfToXaml(<SPAN style="COLOR: #0000ff">string</SPAN> rtfText)
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">        {
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">            var richTextBox = <SPAN style="COLOR: #0000ff">new</SPAN> RichTextBox();
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">            <SPAN style="COLOR: #0000ff">if</SPAN> (<SPAN style="COLOR: #0000ff">string</SPAN>.IsNullOrEmpty(rtfText)) <SPAN style="COLOR: #0000ff">return</SPAN> "<SPAN style="COLOR: #8b0000"></SPAN>";
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px"></PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">            var textRange = <SPAN style="COLOR: #0000ff">new</SPAN> TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd);
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px"></PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">            <SPAN style="COLOR: #0000ff">using</SPAN> (var rtfMemoryStream = <SPAN style="COLOR: #0000ff">new</SPAN> MemoryStream())
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">            {
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">                <SPAN style="COLOR: #0000ff">using</SPAN> (var rtfStreamWriter = <SPAN style="COLOR: #0000ff">new</SPAN> StreamWriter(rtfMemoryStream))
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">                {
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">                    rtfStreamWriter.Write(rtfText);
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">                    rtfStreamWriter.Flush();
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">                    rtfMemoryStream.Seek(0, SeekOrigin.Begin);
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px"></PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">                    textRange.Load(rtfMemoryStream, DataFormats.Rtf);
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">                }
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">            }
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px"></PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">            <SPAN style="COLOR: #0000ff">using</SPAN> (var rtfMemoryStream = <SPAN style="COLOR: #0000ff">new</SPAN> MemoryStream())
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">            {
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px"></PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">                textRange = <SPAN style="COLOR: #0000ff">new</SPAN> TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd);
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">                textRange.Save(rtfMemoryStream, DataFormats.Xaml);
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">                rtfMemoryStream.Seek(0, SeekOrigin.Begin);
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">                <SPAN style="COLOR: #0000ff">using</SPAN> (var rtfStreamReader = <SPAN style="COLOR: #0000ff">new</SPAN> StreamReader(rtfMemoryStream))
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">                {
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">                    <SPAN style="COLOR: #0000ff">return</SPAN> rtfStreamReader.ReadToEnd();
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">                }
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">            }
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px"></PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">        }</PRE></PRE>
<P>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.&nbsp; Here is the interface:</P><PRE style="BORDER-BOTTOM: #cecece 1px solid; BORDER-LEFT: #cecece 1px solid; PADDING-BOTTOM: 5px; BACKGROUND-COLOR: #fbfbfb; MIN-HEIGHT: 40px; PADDING-LEFT: 5px; WIDTH: 650px; PADDING-RIGHT: 5px; OVERFLOW: auto; BORDER-TOP: #cecece 1px solid; BORDER-RIGHT: #cecece 1px solid; PADDING-TOP: 5px"><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px"> <SPAN style="COLOR: #0000ff">public</SPAN> <SPAN style="COLOR: #0000ff">interface</SPAN> IMarkupConverter
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">    {
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">        <SPAN style="COLOR: #0000ff">string</SPAN> ConvertXamlToHtml(<SPAN style="COLOR: #0000ff">string</SPAN> xamlText);
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">        <SPAN style="COLOR: #0000ff">string</SPAN> ConvertHtmlToXaml(<SPAN style="COLOR: #0000ff">string</SPAN> htmlText);
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">        <SPAN style="COLOR: #0000ff">string</SPAN> ConvertRtfToHtml(<SPAN style="COLOR: #0000ff">string</SPAN> rtfText);
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">    }
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px"></PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">    <SPAN style="COLOR: #0000ff">public</SPAN> <SPAN style="COLOR: #0000ff">class</SPAN> MarkupConverter : IMarkupConverter
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">    {
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">        <SPAN style="COLOR: #0000ff">public</SPAN> <SPAN style="COLOR: #0000ff">string</SPAN> ConvertXamlToHtml(<SPAN style="COLOR: #0000ff">string</SPAN> xamlText)
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">        {
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">            <SPAN style="COLOR: #0000ff">return</SPAN> HtmlFromXamlConverter.ConvertXamlToHtml(xamlText, <SPAN style="COLOR: #0000ff">false</SPAN>);
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">        }
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px"></PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">        <SPAN style="COLOR: #0000ff">public</SPAN> <SPAN style="COLOR: #0000ff">string</SPAN> ConvertHtmlToXaml(<SPAN style="COLOR: #0000ff">string</SPAN> htmlText)
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">        {
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">            <SPAN style="COLOR: #0000ff">return</SPAN> HtmlToXamlConverter.ConvertHtmlToXaml(htmlText, <SPAN style="COLOR: #0000ff">true</SPAN>);
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">        }
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px"></PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">        <SPAN style="COLOR: #0000ff">public</SPAN> <SPAN style="COLOR: #0000ff">string</SPAN> ConvertRtfToHtml(<SPAN style="COLOR: #0000ff">string</SPAN> rtfText)
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">        {
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">            <SPAN style="COLOR: #0000ff">return</SPAN> RtfToHtmlConverter.ConvertRtfToHtml(rtfText);
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">        }
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">    }</PRE></PRE>
<P>With this I am now able to convert from RTF to HTML.&nbsp; However, there is one catch - the conversion uses the RichTextBox WPF control which requires a single threaded apartment (STA).&nbsp; Therefore in order to run your code that calls the ConvertRtfToHtml function, it must also be running in a STA.&nbsp; 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:</P><PRE style="BORDER-BOTTOM: #cecece 1px solid; BORDER-LEFT: #cecece 1px solid; PADDING-BOTTOM: 5px; BACKGROUND-COLOR: #fbfbfb; MIN-HEIGHT: 40px; PADDING-LEFT: 5px; WIDTH: 650px; PADDING-RIGHT: 5px; OVERFLOW: auto; BORDER-TOP: #cecece 1px solid; BORDER-RIGHT: #cecece 1px solid; PADDING-TOP: 5px"><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">MarkupConverter markupConverter = <SPAN style="COLOR: #0000ff">new</SPAN> MarkupConverter();
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px"></PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px"><SPAN style="COLOR: #0000ff">private</SPAN> <SPAN style="COLOR: #0000ff">string</SPAN> ConvertRtfToHtml(<SPAN style="COLOR: #0000ff">string</SPAN> rtfText)
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">{
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">   var thread = <SPAN style="COLOR: #0000ff">new</SPAN> Thread(ConvertRtfInSTAThread);
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">   var threadData = <SPAN style="COLOR: #0000ff">new</SPAN> ConvertRtfThreadData { RtfText = rtfText };
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">   thread.SetApartmentState(ApartmentState.STA);
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">   thread.Start(threadData);
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">   thread.Join();
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">   <SPAN style="COLOR: #0000ff">return</SPAN> threadData.HtmlText;
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">}
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px"></PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px"><SPAN style="COLOR: #0000ff">private</SPAN> <SPAN style="COLOR: #0000ff">void</SPAN> ConvertRtfInSTAThread(<SPAN style="COLOR: #0000ff">object</SPAN> rtf)
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">{
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">   var threadData = rtf <SPAN style="COLOR: #0000ff">as</SPAN> ConvertRtfThreadData;
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">   threadData.HtmlText = markupConverter.ConvertRtfToHtml(threadData.RtfText);
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">}
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">        
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px"><SPAN style="COLOR: #0000ff">private</SPAN> <SPAN style="COLOR: #0000ff">class</SPAN> ConvertRtfThreadData
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">{
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">   <SPAN style="COLOR: #0000ff">public</SPAN> <SPAN style="COLOR: #0000ff">string</SPAN> RtfText { <SPAN style="COLOR: #0000ff">get</SPAN>; <SPAN style="COLOR: #0000ff">set</SPAN>; }
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">   <SPAN style="COLOR: #0000ff">public</SPAN> <SPAN style="COLOR: #0000ff">string</SPAN> HtmlText { <SPAN style="COLOR: #0000ff">get</SPAN>; <SPAN style="COLOR: #0000ff">set</SPAN>; }
</PRE><PRE style="BACKGROUND-COLOR: #fbfbfb; MARGIN: 0em; WIDTH: 100%; FONT-FAMILY: consolas,'Courier New',courier,monospace; FONT-SIZE: 11px">}</PRE></PRE>
<P>Here is the zip contain the code for the Markup converter: <A href="http://blogs.msdn.com/matt/attachment/9900386.ashx" mce_href="http://blogs.msdn.com/matt/attachment/9900386.ashx">MarkupConverter.zip</A></P><img src="http://blogs.msdn.com/aggbug.aspx?PostID=9900386" width="1" height="1">]]></content:encoded>
			<wfw:commentRss>http://matthewmanela.com/2009/09/28/converting-rtf-to-html-3/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
<enclosure url="http://blogs.msdn.com/cfs-file.ashx/__key/CommunityServer-Components-PostAttachments/00-09-90-03-86/MarkupConverter.zip" length="64021" type="application/x-zip-compressed" />
		</item>
	</channel>
</rss>
