简体   繁体   中英

Exporting an aspx panel to pdf

I am trying to export the contents in an aspx web panel to pdf. The aspx panel contains table, text, chart, and most importantly, dynamic google maps. Can anyone give me some idea about how to do that?

I have tried this by first converting the whole web page to bitmap image, then converting the bitmap into pdf using iTextSharp. This approach works to some extent. However, I do not want everything in the aspx page in the pdf, just the contents in a particular aspx panel.

Thank you very much for your kind help.

Render the contents of the Panel control to an HtmlTextWriter and then write to the PDF as usual, like this:

protected void YourButton_Click(object sender, EventArgs e)
{
    Response.ContentType = "application/pdf";
    Response.AddHeader("content-disposition", "attachment;filename=YourPane.pdf");
    Response.Cache.SetCacheability(HttpCacheability.NoCache);

    StringWriter sw = new StringWriter();
    HtmlTextWriter hw = new HtmlTextWriter(sw);

    // Call your panel by ID to render the contents to HTML
    YourPanel.RenderControl(hw);

    StringReader sr = new StringReader(sw.ToString());

    Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 100f, 0f);

    HTMLWorker htmlparser = new HTMLWorker(pdfDoc);

    PdfWriter.GetInstance(pdfDoc, Response.OutputStream);

    pdfDoc.Open();

    htmlparser.Parse(sr);

    pdfDoc.Close();

    Response.Write(pdfDoc);
    Response.End();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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