52

We have a crystal report that we need to send out as an e-mail, but the HTML generated from the crystal report is pretty much just plain ugly and causes issues with some e-mail clients. I wanted to export it as rich text and convert that to HTML if it's possible.

Any suggestions?

Aaron Smith
  • 3,332
  • 4
  • 29
  • 25
  • 1
    To send a PDF file is not a solution? – stiduck Jan 13 '09 at 15:21
  • 1
    No, we're sending other documents as PDF attachments, but we want the e-mail to have a body. The part I need the HTML for is the body. – Aaron Smith Jan 13 '09 at 15:29
  • I can't answer since this is closed as off-topic, but thanks to [GNU](https://www.gnu.org/), I recommend using **[UnRTF](https://www.gnu.org/software/unrtf/)**. `brew install unrtf`, then `unrtf --html input.rtf > output.html`. – GabLeRoux May 02 '17 at 13:56
  • 3
    Not sure why people are talking about random command line utilities in a C# question (or in what way this is considered "off-topic"!?) -- but the short answer is: just use `RtfPipe` via NuGet, the syntax is just `var html = Rtf.ToHtml(rtf);`, and it supports a ton of features. For a longer answer, see: https://stackoverflow.com/a/59572912/398630 – BrainSlugs83 Jan 03 '20 at 03:43
  • 3
    As you can see, I asked this question over 10 years ago, when Nuget didn't even exist. Thanks for the comment/answer. Also don't know why/when this was closed as off topic, because I'm obviously asking for a way to do it, not for a library to use. Anyway, good day to you all. – Aaron Smith Jan 04 '20 at 04:11

8 Answers8

32

I would check out this tool on CodeProject RTFConverter. This guy gives a great breakdown of how the program works along with details of the conversion.

Writing Your Own RTF Converter

Richard R
  • 1,337
  • 2
  • 16
  • 23
8

There is also a sample on the MSDN Code Samples gallery called Converting between RTF and HTML which allows you to convert between HTML, RTF and XAML.

Matthew Manela
  • 16,572
  • 3
  • 64
  • 66
  • I tried that, works nicely but has a problem with spaces at the beginning of a line : in such cases, it just leaves " " instead of converting it to Ampersand + nbsp; or adding a void SPAN tag . Thus, the resulting html code has no leading spaces at all. – Jack Griffin Jun 07 '13 at 17:38
  • This worked nicely but I noticed it misses the units value (px) off of the CSS margin property when converting from Xaml to HTML (part of the RTF to HTML process). Simple fix in the method ParseXamlThickness(string thickness) just add '+ "px"' (no single quotes) to each of the return values in the switch statement. – RyanfaeScotland Jan 21 '16 at 12:12
7

Mike Stall posted the code for one he wrote in c# here :

https://learn.microsoft.com/en-us/archive/blogs/jmstall/writing-an-rtf-to-html-converter-posting-code-in-blogs

Community
  • 1
  • 1
missaghi
  • 5,044
  • 2
  • 33
  • 43
  • This one almost worked. I could have added the things I needed, but it wasn't worth the effort. – Aaron Smith Mar 12 '09 at 15:45
  • Yeah, it came so close - but I think the RTF specs might have changed since he wrote it... so it mangled my text. What a pity! – Shaul Behr Mar 10 '11 at 20:06
4

UPDATED:

I got home and tried the below code and it does not work. For anyone wondering, the clipboard does not just magically convert stuff like I'd hoped. Rather, it allows an application to sort of "upload" a data object with a variety of paste formats, and then then you paste (which in my metaphor would be the "download") the program being pasted into specifies its preferred format. I personally ended up using this code, which has been recommended previously, and it was enormously easy to use and very effective. After you have imported the code (in VStudio, Project -> Add Existing Files) you then just go html to rtf like this:

return HtmlToRtfConverter.ConvertHtmlToRtf(myRtfString);

or the opposite direction:

return RtfToHtmlConverter.ConvertHtmlToRtf(myHtmlString);

(below is my previous incorrect answer, in case anyone is interested in the chronology of this answer haha)

Most if not all of the above answers provide comprehensive, often Library-based solutions to the problem at hand. I am away from my computer and thus cannot test the idea, but one alternative, cheap and vaguely hack-y method would be the following.

private string HTMLFromRtf(string rtfString)
{
            Clipboard.SetData(DataFormats.Rtf, rtfString);
            return Clipboard.GetData(DataFormats.Html);         
}

Again, not totally sure if this would work, but just messing around with some html on my iPhone I suspect it would. Documentation is here. More in depth explanation/docs RE the getting and setting of data models in the clipboard can be found here.

(Yes I am fully aware I'm here years later, but I assume this question is one which some people still want answered).

Max von Hippel
  • 2,856
  • 3
  • 29
  • 46
2

If you don't mind getting your hands dirty, it isn't that difficult to write an RTF to HTML converter.

Writing a general purpose RTF->HTML converter would be somewhat complicated because you would need to deal with hundreds of RTF verbs. However, in your case you are only dealing with those verbs used specifically by Crystal Reports. I'll bet the standard RTF coding generated by Crystal doesn't vary much from report to report.

I wrote an RTF to HTML converter in C++, but it only deals with basic formatting like fonts, paragraph alignments, etc. My translator basically strips out any specialized formatting that it isn't prepared to deal with. It took about 400 lines of C++. It basically scans the text for RTF tags and replaces them with equivalent HTML tags. RTF tags that aren't in my list are simply stripped out. A regex function is really helpful when writing such a converter.

Kluge
  • 3,567
  • 3
  • 24
  • 21
  • Why bother converting from RTF->HMTL if he already is converting Report->HTML? He should skip RTF altogether as it is not needed. – Andrew Hare Jan 13 '09 at 17:18
-1

I think you can load it in a Word document object by using .NET office programmability support and Visual Studio tools for office.

And then use the document instance to re-save as an HTML document.

I am not sure how but I believe it is possible entirely in .NET without any 3rd party library.

chakrit
  • 61,017
  • 25
  • 133
  • 162
-3

I am not aware of any libraries to do this (but I am sure there are many that can) but if you can already create HTML from the crystal report why not use XSLT to clean up the markup?

Andrew Hare
  • 344,730
  • 71
  • 640
  • 635
-4

You can try to upload it to google docs, and download it as HTML.

stiduck
  • 510
  • 4
  • 9