Exporting a daily report in PDF is a daily need for any reporting software or application. The same happened when I was working on a project; the project was about to finish but suddenly a requirement somehow was received to export the entire data/report being displayed in a GridView in to PDF.
I was shocked; how to add it when it's about to finish, but after many attempts I got the reply that is here.
Requirement
The only requirement is to add the DLL of "ITextSharp" into the software to export the GridView in PDF.
Code
In code behind add the following namespaces:
- using iTextSharp.text;
- using iTextSharp.text.pdf;
- using iTextSharp.text.html.simpleparser;
Double-click on the button "Export in PDF" and write the following code there:
- Response.ContentType = "application/pdf";
- Response.AddHeader("content-disposition", "attachment;filename=report_grid.pdf");
- Response.Cache.SetCacheability(HttpCacheability.NoCache);
- StringWriter s_w = new StringWriter();
- HtmlTextWriter h_w = new HtmlTextWriter(s_w);
- gvdetails.AllowPaging = false;
- gvdetails.DataBind();
- gvdetails.RenderControl(h_w);
- gvdetails.HeaderRow.Style.Add("width", "15%");
- gvdetails.HeaderRow.Style.Add("font-size", "10px");
- gvdetails.Style.Add("text-decoration", "none");
- gvdetails.Style.Add("font-family", "Arial, Helvetica, sans-serif;");
- gvdetails.Style.Add("font-size", "8px");
- StringReader sr = new StringReader(s_w.ToString());
- Document pdfDoc = new Document(PageSize.A2, 7f, 7f, 7f, 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 control)
- {
- /* Verifies that the control is rendered */
- }

Pramod PeyyalaPosted Aug 10, 2018, 1:21 AM
How to Download Pdf Insted of open?
Atul RawatPosted Jul 29, 2015, 4:52 AM
i think u need bind the gridview after Response.End();
Lewis ObirePosted Feb 17, 2015, 4:27 PM
I having the following error when I tried the codes. "The document has no pages. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.IO.IOException: The document has no pages. Source Error: Line 110: pdfDoc.Open(); Line 111: htmlparser.Parse(sr); Line 112: pdfDoc.Close(); Line 113: Response.Write(pdfDoc); Line 114: Response.End(); Source File: C:\Users\Lewis\Documents\Visual Studio 2010\Projects\XpressAccounts\XpressAccounts\Reports\TrialBalance.aspx.cs Line: 112 "
richard cabatanaPosted Feb 4, 2014, 5:47 AM
HI , I'll try this article in myself but its not working. it will display Null in PDF. what should i do. can anyone help.
Elango ThanumalayanPosted Jun 17, 2013, 8:02 AM
Hi Neha.. Nice article. 4 me its not working. Error in "htmlparser.Parse(sr);" as "The best overloaded method match for 'iTextSharp.text.html.simpleparser.HTMLWorker.Parse(System.IO.StreamReader)' has some invalid arguments"... Can u help me to get rid of this...!!!!!
Rajesh KannanPosted May 10, 2013, 9:00 AM
really nice article....
Neha SharmaPosted May 8, 2013, 1:34 AM
http://www.c-sharpcorner.com/UploadFile/9f0ae2/gridview-edit-delete-and-update-in-Asp-Net/
Abhimanyu SinghPosted May 8, 2013, 12:19 AM
Hii Neha, this article is really nice but i do not want gridview display... Means that i want to take printout only directly from Database on button event...when we search the data in textbox or through Dropdownlist...then directly it should display print preview as we will hit on that searching button
Neha SharmaPosted Apr 9, 2013, 1:31 AM
Thx..!!
Sharad GuptaPosted Apr 8, 2013, 6:03 AM
nice article..