Search code examples
.nethtmlrtfrichtext

How do I convert HTML to RTF (Rich Text) in .NET without paying for a component?


Is there a free third-party or .NET class that will convert HTML to RTF (for use in a rich-text enabled Windows Forms control)?

The "free" requirement comes from the fact that I'm only working on a prototype and can just load the BrowserControl and just render HTML if need be (even if it is slow) and that Developer Express is going to be releasing their own such control soon-ish.

I don't want to learn to write RTF by hand, and I already know HTML, so I figure this is the quickest way to get some demonstrable code out the door quickly.


Solution

  • Actually there is a simple and free solution: use your browser, ok this is the trick I used:

    var webBrowser = new WebBrowser();
    webBrowser.CreateControl(); // only if needed
    webBrowser.DocumentText = *yourhtmlstring*;
    while (_webBrowser.DocumentText != *yourhtmlstring*)
        Application.DoEvents();
    webBrowser.Document.ExecCommand("SelectAll", false, null);
    webBrowser.Document.ExecCommand("Copy", false, null);
    *yourRichTextControl*.Paste(); 
    

    This could be slower than other methods but at least it's free and works!