Convert Rtf to HTML [closed]
Asked Answered
L

8

53

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?

Lichee answered 13/1, 2009 at 15:16 Comment(5)
To send a PDF file is not a solution?Warfare
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.Lichee
I can't answer since this is closed as off-topic, but thanks to GNU, I recommend using UnRTF. brew install unrtf, then unrtf --html input.rtf > output.html.Sophey
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://mcmap.net/q/353592/-convert-rtf-to-html-in-netFarra
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.Lichee
M
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

Milne answered 12/3, 2009 at 13:41 Comment(2)
Thanks a lot. This was useful. Hopefully this will fix the issues we were having. It just came up again today, perfect timing. :-)Lichee
When downloaded the code make sure all the project are in .NET 4Amoakuh
O
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.

Olympian answered 28/4, 2011 at 18:32 Comment(2)
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.Rehabilitation
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.Diabolic
R
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

Roop answered 13/1, 2009 at 17:21 Comment(2)
This one almost worked. I could have added the things I needed, but it wasn't worth the effort.Lichee
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!Carrycarryall
C
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).

Creuse answered 15/7, 2015 at 0:44 Comment(3)
Hmmm... if you used this approach to paste into a program like word and recopy the data back onto the clipboard, it might actually work.Farra
@Farra I agree! Kind of a disgusting hack, but yes, it could work.Creuse
I just make it work with a TRichEdit to load the RTF file, and a TWebBrowser (in Edit mode) to paste the content and then save the result file in HTML. I have automated everything through codeGarrick
V
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.

Vitality answered 13/1, 2009 at 16:13 Comment(1)
Why bother converting from RTF->HMTL if he already is converting Report->HTML? He should skip RTF altogether as it is not needed.Divergency
M
-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.

Modesta answered 13/1, 2009 at 15:23 Comment(4)
Word? He's trying to get rid of the bad markup! ;)Meistersinger
oh i forgot that... but that should gives some control over the result markup no?Modesta
By just effectively exporting it from word? I haven't use office automation in like... 3 versions ... but that said, I doubt it.Meistersinger
I mean you could edit the word document before exporting it... like removing certain kind of elements etc.Modesta
D
-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?

Divergency answered 13/1, 2009 at 15:18 Comment(1)
XSLT is for transforming XML, not HTML.Darryldarryn
W
-4

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

Warfare answered 13/1, 2009 at 15:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.