Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert RTF to HTML in .NET

Tags:

html

.net

rtf

I've managed to do the reverse using WebBrowser and RichTextBox.

But how would I convert RTF to HTML?

like image 695
IAmGroot Avatar asked Dec 22 '22 03:12

IAmGroot


2 Answers

If you pop-up NuGet and search for "RTF", the most popular result right now looks like RtfPipe; you can install it right there, or via the package manager console via:

Install-Package RtfPipe

Then in your C#, you can convert RTF to HTML super easily:

var html = RtfPipe.Rtf.ToHtml(rtf);

According to the readme.md on their GitHub page:

This library attempts to support the core RTF features documented in the RTF Specification 1.9.1. These features include:

  • Character formatting (bold, italics, color, ...)
  • Tables (including nested tables)
  • Lists
  • Hyperlinks
  • Pictures
  • Heading levels
  • HTML encapsulation (e.g. as performed by Outlook)

With that said, there are numerous cases for non-trivial documents where the library will not produce the "correct" visual representation when compared to other RTF readers (such as MS Word).

I piped my RTF into it, and it worked amazingly. YYMV.

like image 137
BrainSlugs83 Avatar answered Dec 27 '22 12:12

BrainSlugs83


Disclaimer: I'm working for this company.

As I see, the question is old but maybe someone search solution for this too. Our component RTF to HTML allows to convert RTF to HTML. You may download a component or try online-demo. Try the trial version first if you have a doubt. :) Trial is free.

Here's the code sample for the converting from RTF to HTML in ASP.NET:

    SautinSoft.RtfToHtml r = new SautinSoft.RtfToHtml();
    r.OutputFormat = SautinSoft.RtfToHtml.eOutputFormat.HTML_401;
    r.ImageStyle.IncludeImageInHtml = false; //To save images inside HTML as binary data specify this property to 'true'

    r.ImageStyle.ImageFolder = Server.MapPath(""); 
    r.ImageStyle.ImageSubFolder = "images";
    r.ImageStyle.ImageFileName = "picture";       

    string rtf = ".....";
    string html = r.ConvertString(rtf);        

    //show HTML
    if (html.Length>0)
    {
        Response.Buffer = true;
        Response.Clear();
        Response.ContentType = "text/html";
        Response.Write(html);
        Response.Flush();
        Response.End();
    }
like image 43
SautinSoft Avatar answered Dec 27 '22 11:12

SautinSoft