Skip to content Skip to sidebar Skip to footer

Html To Rtf Converter For .net

I've already seen lots of posts on the site for RTF to HTML and some other posts talking about some HTML to RTF converters, but I'm really trying to get a full breakdown of what is

Solution 1:

For what its worth and in no particular order.

A while ago i wanted to export to RTF and then import from RTF the RTF in question being manipulated by MS Word.

The first problem is RTF is not an open standard. It is an internal MS standard and there fore they alter it as and when they like and do not generally worry about compatibility. Currently the versions of RTF are 1.3 to 1.9 and they are all different. Internally they use twips for measurement just for good measure.

I bought the O'Reilly pocket book on the subject which helped and read a lot of the MS documentation which is good, but there is a lot of it and lots for each version.

Because of the way RTF is coded using regex to manipulate is incredibly hard work and needs careful handling and concentration to test and get to work. I use a Mac editor that had built in regex so i could steadily test each section and build it into the code.

Because of the number of versions there is also a lot of incompatibility between versions but there is a lot of commonality and in the end it was reasonably hard/easy to get where i wanted (after about a weeks reading and a weeks coding) and producing a really simple version.

I never found a commercial solution but i had to have a free on because of budget so that cut a lot out but take great care in choosing one to make sure it does what you want and has support.

I don't think where you are coming from HTML/XML/XHTML, i was converting CSV formats, it the RTF.

I am not sure if i would advise to DIY or buy. Probably on balance DIY but your own circumstances will dictate that.

Edit: One thing going from content to RTF is easier than vice versa.

BTW not criticising MS fior the RTF versions, hey it's theirs and proprietary so they can do what they like.

Solution 2:

I would recommend doing it yourself as the task is not really that complex. Firstly, the easiest way convert one Xml format into another Xml format is with an Xslt. Converting Xml documents in C# is super easy.

Here is a good msdn blog post to get you started. Mike even mentions that it was easier to do this by hand that to deal with a third party.

link

Actually, I already answered this question here. Guess that makes this a duplicate.

Solution 3:

I just came across this WYSIWYG rich text editor (RTE) for the web that also has an HTML to RTF converter, Cute Editor for .NET. Does anyone have any experience with this component? My main experience for web based RTEs have been CKEditor (fckEditor) and TinyMCE but as far as I can tell CKEditor and TinyMCE do not have HTML to RTF converters built in.

Solution 4:

Since I'm required to implement some mailmerge capabilities with rich-text formatting on a Web application, I thought it'd be nice to share my experiences.

Basically, I explored two alternatives:

  • using Google Docs API to leverage Google Docs capabilities
  • using XSLT, as shown on this essay

Google Docs API works well. Problem is, when you upload an HTML document with page breaks, like this:

<pstyle="page-break-before:always;display:none;"/>

and ask Google to convert the doc in RTF, you lose all breaks, which does not fit my requirements. However, if page breaks aren't an issue for you, you might check this solution out.

The XSLT solution works... sort of.

It works if you reference MSXML3 COM object directly, bypassing System.Xml classes. Otherwise I couldn't make it work. Moreover, it seems to honor all but basic formatting and tags, disregarding text color, size and the like. However, it honors page breaks. :-)

Here's a quick library I wrote, using tidy.net to force HTML to XHTML conversion. Hope it helps.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespaceADDS.Mailmerge
{

    publicclassXHTML2RTF
    {

        MSXML2.FreeThreadedDOMDocument _xslDoc;
        MSXML2.FreeThreadedDOMDocument _xmlDoc;
        MSXML2.IXSLProcessor _xslProcessor;
        MSXML2.XSLTemplate _xslTemplate;
        static XHTML2RTF instance = null;
        staticreadonlyobject padlock = newobject();

        XHTML2RTF()
        {
            _xslDoc = new MSXML2.FreeThreadedDOMDocument();
            //XSLData.xhtml2rtf is a resource file // containing XSL for transformation// I got XSL from here: // http://www.codeproject.com/KB/HTML/XHTML2RTF.aspx
            _xslDoc.loadXML(XSLData.xhtml2rtf);
            _xmlDoc = new MSXML2.FreeThreadedDOMDocument();
            _xslTemplate = new MSXML2.XSLTemplate();
            _xslTemplate.stylesheet = _xslDoc;
            _xslProcessor = _xslTemplate.createProcessor();
        }

        publicstringConvertToRTF(string xhtmlData)
        {
            try
            {
                string sXhtml = "";
                TidyNet.Tidy tidy = new TidyNet.Tidy();
                tidy.Options.XmlOut = true;
                tidy.Options.Xhtml = true;
                using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(xhtmlData)))
                {
                    StringBuilder sb = new StringBuilder();
                    using (MemoryStream sw = new MemoryStream())
                    {
                        TidyNet.TidyMessageCollection messages = new TidyNet.TidyMessageCollection();
                        tidy.Parse(ms, sw, messages);
                        sXhtml = Encoding.UTF8.GetString(sw.ToArray());
                    }
                }

                _xmlDoc.loadXML(sXhtml);
                _xslProcessor.input = _xmlDoc;
                _xslProcessor.transform();
                return _xslProcessor.output.ToString();
            }
            catch (Exception exc)
            {
                thrownew Exception("Error in xhtml conversion. ", exc);
            }
        }

        publicstatic XHTML2RTF Instance
        {
            get
            {
                lock (padlock)
                {
                    if (instance == null)
                    {
                        instance = new XHTML2RTF();
                    }
                    return instance;
                }
            }
        }
    }



}

Post a Comment for "Html To Rtf Converter For .net"