Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
hi sir
please give me some suggestion how we convert the complete asp.net web page(.aspx page not only gridview )into pdf file using c# code.
Posted

C#
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);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();


use this code on button click()
everything will fine
good luck
 
Share this answer
 
v2
Comments
fjdiewornncalwe 29-Mar-12 13:16pm    
Good answer, but we usually don't like to have old questions reopened like this.
dineshdena 10-Apr-12 8:20am    
sir,
i tried the above code but get error like this

RegisterForEventValidation can only be called during Render();

and its pointing this area

this.Page.RenderControl(hw);

how to solve this plz help me.
thanks in advance
dineshdena 10-Apr-12 9:29am    
i get solved the above problem by using EnableEventValidation = "false" in my design page

but now im facing another problem the page is getting created in pdf but in that pdf some dropdown box r shown in $$$$ symbol and other symbol how to get solve this prob
amolnarkhede 26-Feb-13 4:15am    
i got errror as "Font size too small: 0" at htmlparser.Parse(sr);
Member 12287325 2-Feb-16 6:22am    
I have tried but not work
You may take help from iTextSharp[^]

Thanks,
Imdadhusen
 
Share this answer
 

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