Click here to Skip to main content
15,999,229 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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?

C#
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();
        }
Posted
Updated 21-Mar-17 23:49pm
v2

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'.

C#
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 */
    }
 
Share this answer
 
v3
Looks like you missed few lines at the end, modify it to:
C#
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
// missed/correct way lines
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();


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:
C#
gvdetails.RenderControl(hw);

to
C#
this.RenderControl(hw);
 
Share this answer
 
Comments
Mohammad A Rahman 14-May-12 3:12am    
Straight.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900