简体   繁体   English

使用itextSharp 5.3.0将aspx转换为pdf

[英]aspx to pdf using itextSharp 5.3.0

I am using this dll iTextSharp 5.3.0 to make a pdf file . 我正在使用此dll iTextSharp 5.3.0制作pdf文件。 Is there a way to convert full .aspx page in pdf ? 有没有办法在pdf中转换完整的.aspx页? My page has grids and server side code . 我的页面有网格和服务器端代码。

This is my code: 这是我的代码:

protected void Button1_Click(object sender, EventArgs e) { 受保护的无效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();

}

It's working but the file test.pdf is just plain text. 它正在工作,但是文件test.pdf只是纯文本。 The html isn't well interpreted, my grids are missing and my server side values (the values from the grids) are also missing . html解释不正确,我的网格丢失了,服务器端值(来自网格的值)也丢失了。 I also tried the codes from here: http://forums.asp.net/t/1199774.aspx and here: Problem with HTMLParser in Itextsharp 我还尝试了以下代码: http : //forums.asp.net/t/1199774.aspx以及此处: Itextsharp中的HTMLParser问题

Thanks in advance! 提前致谢!

This is my honest advice! 这是我的诚实建议! Don't waste your time on the HTMLWorker.ParseToList. 不要在HTMLWorker.ParseToList上浪费时间。 It has a very elementary HTML parser. 它具有非常基本的HTML解析器。 Try this packge and you will never look back! 试试这个包,您将永远不会回头! https://github.com/pruiz/WkHtmlToXSharp https://github.com/pruiz/WkHtmlToXSharp

ITextSharp only renders inline css, it is giving problem while adding CSS files. 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();

Use XMLWorker instead of HTMLWorker. 使用XMLWorker而不是HTMLWorker。 works like charm. 就像魅力。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM