views:

110

answers:

2

Hello all!

There is a way do convert HTML or PDF to RTF/DOC or HTML/PDF to image using DevExpress or Infragistics?

I tried this using DevExpress:

string html = new StreamReader(Server.MapPath(@".\teste.htm")).ReadToEnd();

            RichEditControl richEditControl = new RichEditControl();
            string rtf;
            try
            {
                richEditControl.HtmlText = html;
                rtf = richEditControl.RtfText;
            }
            finally
            {
                richEditControl.Dispose();
            }

            StreamWriter sw = new StreamWriter(@"D:\teste.rtf");
            sw.Write(rtf);
            sw.Close();

But I have a complex html content (tables, backgrounds, css etc) and the final result is not good...

+2  A: 

I suggest you to use latest DevExpress version (version 10.1.5 this time). It handles tables much better than previous ones.

Please use the following code to avoid encoding issues (StreamReader and StreamWriter in your sample always use Encoding.UTF8 encoding, this will corrupt any content stored with another encoding):

    using (RichEditControl richEditControl = new RichEditControl()) {
        richEditControl.LoadDocument(Server.MapPath(@".\teste.htm"), DocumentFormat.Html);
        richEditControl.SaveDocument(@"D:\teste.rtf", DocumentFormat.Rtf);
    }

Also take a look at the richEditControl.Options.Import.Html and richEditControl.Options.Export.Rtf properties, you may find them useful for some cases.

DevExpress Team
+1  A: 

To convert Html content into image or Pdf you may use the following code:

using (RichEditControl richEditControl = new RichEditControl()) {
    richEditControl.LoadDocument(Server.MapPath(@".\teste.htm"), DocumentFormat.Html);
    using (PrintingSystem ps = new PrintingSystem()) {
        PrintableComponentLink pcl = new PrintableComponentLink(ps);
        pcl.Component = richEditControl;
        pcl.CreateDocument();
        //pcl.PrintingSystem.ExportToPdf("teste.pdf");
        pcl.PrintingSystem.ExportToImage("teste.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
    }
}
DevExpress Team
Hello DevExpress Team! We have the DevExpress version 9.3 and the result in PDF, JPG or RTF was not good. The html source is much complex, many table and CSS's. But, tanks for the tip!
Fabio
Table support has been introduced in XtraRichEdit since version 10.1 . Version 9.3 can only read table content as sequence of paragraphs. That's why you cannot achieve good results when converting complex html with 9.3.
DevExpress Team