How to convert HTML to RTF?

Asked

Viewed 735 times

2

I have an Asp.Net MVC project where I would like to convert an HTML string to RTF in Controller.

I’ve tried the following ways:

Source 1

using(var webBrowser = new WebBrowser()){
   webBrowser.CreateControl();
   webBrowser.DocumentText = minhaStringHTML;

   while(webBrowser.DocumentText != minhaStringHTML)
   {
       Application.DoEvents();
   }

    webBrowser.Document.ExecCommand("SelectAll", false, null);
    webBrowser.Document.ExecCommand("Copy", false, null);

    using(var rtc = new RichTextBox())
    {
       meuRTF = rtc.Paste();
    }
}

But soon in the first line presented, I got the following error:

{"Unable to create an instance of Activex control '8856f961-340a-11d0-a96b-00c04fd705a2' because the current thread is not in a STA (single-threaded Apartment)."}


So I continued the research and got this project.

I downloaded Solution, compiled the Class Library project, copied the DLL (Markupconverter.dll) for my project, where I made the proper reference, and tried its use in the following way:

IMarkupConverter markupConverter;
markupConverter = new MarkupConverter.MarkupConverter();
var rtfResult = markupConverter.ConvertHtmlToRtf(meuHtml);

But I got the following mistake:

{"The call thread must be STA, because many components of the User Interface so require."}

I did several researches, but found nothing practical and free of charge to perform HTML to RTF conversion.

1 answer

1


Unfortunately, there are no good open sources converters... So far the only available is which Voce used which is this project.

In case this error occurred because the conversion uses the WPF Richtextbox which requires a single threaded Apartment (STA). So it needs to be running on STA, in the case of ASP.NET, you cannot run on STA, so you would need to create a STA thread to run the conversion.

MarkupConverter markupConverter = new MarkupConverter(); 


private string ConvertRtfToHtml(string rtfText) 
{ 
   var thread = new Thread(ConvertRtfInSTAThread); 
   var threadData = new ConvertRtfThreadData { RtfText = rtfText }; 
   thread.SetApartmentState(ApartmentState.STA); 
   thread.Start(threadData); 
   thread.Join(); 
   return threadData.HtmlText; 
} 

private void ConvertRtfInSTAThread(object rtf) 
{ 
   var threadData = rtf as ConvertRtfThreadData; 
   threadData.HtmlText = markupConverter.ConvertRtfToHtml(threadData.RtfText); 
} 


private class ConvertRtfThreadData 
{ 
   public string RtfText { get; set; } 
   public string HtmlText { get; set; } 
}

All this information is on the previous link. In documentation form.

  • Thank you very much, Bastei change some things in the code, because this you used refers to converting RTF to HTML, and I need the opposite... But 100% working here! I hadn’t quite understood the documentation.

  • 1

    I leave just one observation, that library for some reason can not convert ó and Ó for your RTF correspondents, but I was able to make it work by doing a Replace before conversion. html.Replace("ó", "ó");

Browser other questions tagged

You are not signed in. Login or sign up in order to post.