使用DevExpress或Infragistics将HTML或PDF转换为RTF / DOC或HTML / PDF到图像

时间:2010-07-02 11:50:18

标签: c# .net asp.net devexpress infragistics

有一种方法可以使用DevExpress或Infragistics将HTML或PDF转换为RTF / DOC或HTML / PDF图像吗?

我使用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();

但我有一个复杂的html内容(表格,背景,CSS等),最终结果并不好......

2 个答案:

答案 0 :(得分:3)

要将Html内容转换为图像或Pdf,您可以使用以下代码:

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);
    }
}

答案 1 :(得分:2)

我建议你使用最新的DevExpress版本(这次是10.1.5版本)。它比以前更好地处理表格。

请使用以下代码以避免编码问题(示例中的StreamReader和StreamWriter始终使用Encoding.UTF8编码,这将损坏使用其他编码存储的任何内容):

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

另请查看richEditControl.Options.Import.Html和richEditControl.Options.Export.Rtf属性,您可能会发现它们在某些情况下很有用。