iTextSharp Can not Convert All HTML to PDF

百般思念 提交于 2019-11-28 11:48:39

问题


Using the sample codes from here I come up with these codes -

var my_html = this.GetmyReportHtml();
    var my_css = this.GetmyReportHtmlCss();

    Byte[] bytes;

    using (var ms = new MemoryStream())
    {
        using (var doc = new iTextSharp.text.Document(PageSize.LETTER))
        {
            using (var writer = PdfWriter.GetInstance(doc, ms))
            {
                doc.Open();

                try
                {
                    using (var msCss = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(my_css)))
                    {
                        using (var msHtml = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(my_html)))
                        {
                            iTextSharp.tool.xml.XMLWorkerHelper.GetInstance().ParseXHtml(writer, doc, msHtml, msCss);
                        }
                    }
                }
                catch (Exception ex)
                {

                }
                finally
                {
                    doc.Close();
                }
            }
        }

        bytes = ms.ToArray();
    }

    System.IO.File.WriteAllBytes(@"c:\\temp\test.pdf", bytes);

PDF has been generated. However my_html has 6 pages in my case, only half of the content are converted to pdf.

Anyone knows what happened here? How to know if iTextSharp.tool.xml.XMLWorkerHelper.GetInstance().ParseXHtml works properly?

Thanks


回答1:


Found out the problem. In this line of codes -

using (var doc = new iTextSharp.text.Document(PageSize.LETTER))

PageSize.LETTER can not be passed in.



来源:https://stackoverflow.com/questions/28923080/itextsharp-can-not-convert-all-html-to-pdf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!