将aspx页面导出为pdf [英] Exporting aspx page to pdf

查看:155
本文介绍了将aspx页面导出为pdf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在尝试下载一个aspx页面作为pdf.I我正在使用以下代码。它正在创建一个pdf文档。但我无法打开它。任何人都可以帮助我吗?



Hi,
I am trying to download an aspx page as a pdf.I am using the following code.It is creating one pdf document.But I couldn't open that. Can anyone help me?

protected void imgbtnprint_Click(object sender, ImageClickEventArgs e)
    {
        Response.Clear();
        Response.Buffer = true;
        Response.ContentType = "application/pdf";               Response.AddHeader("content-disposition","attachment;filename=TestPage.pdf");
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        StringWriter sw = new StringWriter();
        HtmlTextWriter hw = new HtmlTextWriter(sw);
        this.Page.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);
        Response.Write(sw.ToString());
        pdfDoc.Open();
        Response.End();
        }

推荐答案

看起来你最后错过了几行,修改为:

Looks like you missed few lines at the end, modify it to:
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
// missed/correct way lines
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();





看看这个样本:

使用ITextSharp 将Gridview数据导出为PDF [ ^ ]



要导出整个页面而不仅仅是网格,您需要更改此行:



Have a look at this sample:
Export Gridview data to PDF using ITextSharp[^]

To export whole page instead of just grid, you would need to change this line:

gvdetails.RenderControl(hw);



to


to

this.RenderControl(hw);


最后我将该aspx页面中的UpdatePanel导出为pdf文件,而不是导出整个页面如果我正在编写this.Page而不是UpdatePanel1,则会导致异常无法转换类型为'的对象iTextSharp.text.html.simpleparser.CellWrapper '输入' iTextSharp.text.Paragraph



At last i have exported the UpdatePanel in that aspx page to pdf file instead of exporting the whole page to pdf .If I am writing "this.Page" instead of "UpdatePanel1" it results in an exception Unable to cast object of type 'iTextSharp.text.html.simpleparser.CellWrapper' to type'iTextSharp.text.Paragraph'.

protected void imgbtnprint_Click(object sender, ImageClickEventArgs e)
    {
        Response.Clear();
        Response.Buffer = true;
        Response.ContentType = "application/pdf";
        Response.AddHeader("content-disposition", "attachment;filename=UserDetails.pdf");
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        StringWriter sw = new StringWriter();
        HtmlTextWriter hw = new HtmlTextWriter(sw);
        UpdatePanel1.DataBind();
        UpdatePanel1.RenderControl(hw);
        StringReader sr = new StringReader(sw.ToString());
        Document pdfDoc = new Document(PageSize.A2, 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();
    }
    public override void VerifyRenderingInServerForm(Control txt_salutaion)
    {
        /* Verifies that the control is rendered */
    }


这篇关于将aspx页面导出为pdf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆