0

我正在使用这个 dll iTextSharp 5.3.0 来制作一个 pdf 文件。有没有办法在 pdf 中转换完整的 .aspx 页面?我的页面有网格和服务器端代码。

这是我的代码:

受保护的无效Button1_Click(对象发送者,EventArgs e){

    createPDF(Server.MapPath("Default.aspx"));

}


private void createPDF(string html)
{


    TextReader reader = new StringReader(html);

    // step 1: creation of a document-object
    Document document = new Document(PageSize.A4, 30, 30, 30, 30);

    // step 2:
    // we create a writer that listens to the document
    // and directs a XML-stream to a file
    PdfWriter writer = PdfWriter.GetInstance(document, new FileStream("c://test.pdf", FileMode.Create));


    HTMLWorker worker = new HTMLWorker(document);

    document.Open();
    worker.StartDocument();
    List<IElement> p = HTMLWorker.ParseToList(new StreamReader(html), new StyleSheet());
    for (int k = 0; k < p.Count; k++)
    {
        document.Add((IElement)p[k]);
    }



    worker.EndDocument();
    worker.Close();
    document.Close();

}

它正在工作,但文件 test.pdf 只是纯文本。html 没有得到很好的解释,我的网格丢失了,我的服务器端值(网格中的值)也丢失了。我还尝试了这里的代码: http ://forums.asp.net/t/1199774.aspx 和这里: Itextsharp 中的 HTMLParser 问题

提前致谢!

4

2 回答 2

0

ITextSharp 仅呈现内联 css,在添加 CSS 文件时会出现问题。

            System.Web.HttpContext.Current.Response.ContentType = "application/pdf";
            System.Web.HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=BookingDetails.pdf");
            System.Web.HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
            StringWriter sw = new StringWriter();
            HtmlTextWriter hw = new HtmlTextWriter(sw);

            this.CreateBookingMainDiv.RenderControl(hw);

            StringReader sr = new StringReader(sw.ToString());
            Document pdfDoc = new Document(new Rectangle(922,1296),7f,7f,7f,0f);

            PdfWriter writer = PdfWriter.GetInstance(pdfDoc, System.Web.HttpContext.Current.Response.OutputStream);

            pdfDoc.Open();

            //HtmlPipeline

            CssAppliers ca = new CssAppliersImpl();
            //ICssFile cfile = new CssFileProcessor();
            HtmlPipelineContext htmlContext = new HtmlPipelineContext(ca);
            htmlContext.SetTagFactory(Tags.GetHtmlTagProcessorFactory());
            //CSS stuff

            //var cssResolver = new StyleAttrCSSResolver();
            //var DamcoCss = XMLWorkerHelper.GetCSS(new FileStream(HttpContext.Current.Server.MapPath("~/css/damco.css"), FileMode.Open));
            ICssFile cfile = new CssFileImpl();

            ICSSResolver cssResolver = XMLWorkerHelper.GetInstance().GetDefaultCssResolver(true);

            //String DamcoCss = HttpContext.Current.Server.MapPath("~/css/damco.css");
            //String BootStrapCss = HttpContext.Current.Server.MapPath("~/css/bootstrap.css");
            //String BootStrapCssTheme = HttpContext.Current.Server.MapPath("~/css/bootstrap-theme.css");

            //Add the external CSS file        

            //cssResolver.AddCssFile(DamcoCss, true);
            //cssResolver.AddCssFile(BootStrapCss, true);
            //cssResolver.AddCssFile(BootStrapCssTheme, true);

            //Pipeline
            IPipeline pipeline = new CssResolverPipeline(cssResolver, new HtmlPipeline(htmlContext, new PdfWriterPipeline(pdfDoc, writer)));
            //XMLWorker
            XMLWorker worker = new XMLWorker(pipeline, true);
            //and...we parse
            XMLParser parser = new XMLParser(true, worker);
            //parser.AddListener(worker);
            parser.Parse(sr);
            parser.Flush();
            pdfDoc.Close();

            System.Web.HttpContext.Current.Response.Write(pdfDoc);
            System.Web.HttpContext.Current.ApplicationInstance.CompleteRequest();
            //System.Web.HttpContext.Current.Response.End();

使用 XMLWorker 而不是 HTMLWorker。像魅力一样工作。

于 2014-10-27T12:42:09.913 回答
0

这是我诚实的建议!不要在 HTMLWorker.ParseToList 上浪费时间。它有一个非常基本的 HTML 解析器。试试这个包,你永远不会回头! https://github.com/pruiz/WkHtmlToXSharp

于 2012-06-28T11:49:54.323 回答