简体   繁体   English

将aspx面板导出为pdf

[英]Exporting an aspx panel to pdf

I am trying to export the contents in an aspx web panel to pdf. 我正在尝试将aspx Web面板中的内容导出为pdf。 The aspx panel contains table, text, chart, and most importantly, dynamic google maps. aspx面板包含表格,文本,图表,最重要的是,动态Google地图。 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. 我首先通过将整个网页转换为位图图像,然后使用iTextSharp将位图转换为pdf进行了尝试。 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. 但是,我不希望pdf的aspx页面中的所有内容,而只是特定aspx面板中的内容。

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: Panel控件的内容呈现到HtmlTextWriter ,然后像往常一样写入PDF,如下所示:

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

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

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