Convert RTF to HTML in .NET
Asked Answered
S

4

6

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

But how would I convert RTF to HTML?

Saintpierre answered 24/1, 2012 at 13:56 Comment(0)
D
1

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();
    }
Doralin answered 19/9, 2012 at 12:13 Comment(2)
I converted rtf to html successfully using your code but when I loaded that html contents in TinyMCE editor and updated them, it saves only plane text. It looses its style. Please help.Mungo
Actually @Fraxinella answer should be accepted one since this answer promotes paid tool while solution that BrainSlugs83 has provided is free and it works.Seedcase
F
16

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.

Fraxinella answered 3/1, 2020 at 3:40 Comment(0)
D
1

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();
    }
Doralin answered 19/9, 2012 at 12:13 Comment(2)
I converted rtf to html successfully using your code but when I loaded that html contents in TinyMCE editor and updated them, it saves only plane text. It looses its style. Please help.Mungo
Actually @Fraxinella answer should be accepted one since this answer promotes paid tool while solution that BrainSlugs83 has provided is free and it works.Seedcase
T
0

The only problem is when you work on a budget, additional costs mean lower profit, so I started to develop my own version. The main problem is that at the moment it only supports Bold and Italic, and certain entities (&amp, &copy, &reg, &trade, &euro and &###) and lacks both font and color support, but it is still a work in progress. I am adding font and color, but my headache is that these could come from stylesheets rather than the old fashion html tags.

I am writing this in VB.NET and have posted the startup code on CodeProject

Startup Code

Torray answered 20/7, 2015 at 19:30 Comment(1)
Rather than just the blurb can you add enough details of the usage synopsis to your answer so that someone can pick it up right away and understand it?Synapsis
A
-1

If you want to do it programattically you should parse your rtf (is a simple text based file), convert rtf control words to html tags.

Here you can find the rtf specs http://www.biblioscape.com/rtf15_spec.htm

or use an already existing converter: http://sourceforge.net/projects/rtf2html-lite/

Adenoma answered 24/1, 2012 at 14:5 Comment(1)
Its not straight forward converting tags yourself. No idea how to get a C++ Library into .NET. (its not a DLL)Saintpierre

© 2022 - 2024 — McMap. All rights reserved.