Export Grid View Data to PDF Using ITextSharp

This article explains how to export ASP.Net grid data to a PDF with custom width using iTextSharp.

What ITextSharp is: iTextSharp is a free and open source assembly that helps to convert page output or HTML content in PDF file.

You can download it from here:

http://sourceforge.net/projects/itextsharp/

Now add that DLL to the application.

Getting Started

Start Visual Studio and create a new website in ASP.Net and add these 2 DLLs to the solution:

  • Itextsharp.dll
  • itextsharp.pdfa.dll

Now add these namespaces:

  1. using iTextSharp.text;  
  2. using iTextSharp.text.pdf;  
  3. using iTextSharp.text.html;  
  4. using iTextSharp.text.html.simpleparser;   

This is my GridView HTML:

  1. <div>  
  2.     <asp:gridview id="gvEmployees" runat="server" emptydatatext="No data found" autogeneratecolumns="False"  
  3.         datakeynames="EmployeeID" datasourceid="SqlDataSource1" allowpaging="False" backcolor="White"  
  4.         bordercolor="#DEDFDE" borderstyle="None" borderwidth="1px" cellpadding="4" forecolor="Black"  
  5.         gridlines="Vertical" height="165px" width="678px">  
  6. <AlternatingRowStyle BackColor="White" />  
  7. <Columns>  
  8. <asp:BoundField DataField="EmployeeID" HeaderText="EmployeeID" InsertVisible="False" ReadOnly="True" SortExpression="EmployeeID" />  
  9. <asp:BoundField DataField="LastName" HeaderText="LastName" SortExpression="LastName" />  
  10. <asp:BoundField DataField="FirstName" HeaderText="FirstName" SortExpression="FirstName" />  
  11. <asp:BoundField DataField="City" HeaderText="City" SortExpression="City" />  
  12. <asp:BoundField DataField="Region" HeaderText="Region" SortExpression="Region" />  
  13. <asp:BoundField DataField="PostalCode" HeaderText="PostalCode" SortExpression="PostalCode" />  
  14. <asp:BoundField DataField="Country" HeaderText="Country" SortExpression="Country" />  
  15. </Columns>  
  16. <FooterStyle BackColor="#CCCC99" />  
  17. <HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />  
  18. <PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" />  
  19. <RowStyle BackColor="#F7F7DE" />  
  20. <SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />  
  21. <SortedAscendingCellStyle BackColor="#FBFBF2" />  
  22. <SortedAscendingHeaderStyle BackColor="#848384" />  
  23. <SortedDescendingCellStyle BackColor="#EAEAD3" />  
  24. <SortedDescendingHeaderStyle BackColor="#575357" />  
  25. </asp:gridview>  
  26. </div>  
  27. <asp:sqldatasource id="SqlDataSource1" runat="server" connectionstring="<%$ ConnectionStrings:NorthwindConnectionString %>"  
  28.     selectcommand="SELECT [EmployeeID], [LastName], [FirstName], [Title], [BirthDate], [City], [Region], [PostalCode], [Country] FROM [Employees]"></asp:sqldatasource>  
  29. <asp:button id="btnExport" runat="server" text="Export To PDF" onclick="btnExport_Click" />   

Now let's work on the code behind part.

  1. protected void btnExport_Click(object sender, EventArgs e)  
  2. {  
  3.     Response.ContentType = "application/pdf";  
  4.     Response.AddHeader("content-disposition""attachment;filename=EmployeeList.pdf");  
  5.     Response.Cache.SetCacheability(HttpCacheability.NoCache);  
  6.     StringWriter stringWriter = new StringWriter();  
  7.     HtmlTextWriter htmlTextWriter = new HtmlTextWriter(stringWriter);  
  8.     gvEmployees.DataBind();  
  9.     gvEmployees.RenderControl(htmlTextWriter);  
  10.     gvEmployees.HeaderRow.Style.Add("width""10%");  
  11.     gvEmployees.HeaderRow.Style.Add("font-size""15px");  
  12.     gvEmployees.Style.Add("text-decoration""none");  
  13.     gvEmployees.Style.Add("font-family""Arial, Helvetica, sans-serif;");  
  14.     gvEmployees.Style.Add("font-size""8px");  
  15.     StringReader sr = new StringReader(stringWriter.ToString());  
  16.     Document doc = new Document(PageSize.A2, 7f, 7f, 7f, 0f);  
  17.     HTMLWorker htmlparser = new HTMLWorker(doc);  
  18.     PdfWriter.GetInstance(doc, Response.OutputStream);  
  19.     doc.Open();  
  20.     htmlparser.Parse(sr);  
  21.     doc.Close();  
  22.     Response.Write(doc);  
  23.     Response.End();  
  24. }  
  25. public override void VerifyRenderingInServerForm(Control control)  
  26. {  
  27.  

Now run the application. When the button is clicked the PDF will show in the grid view data like this:

Image1.jpg
Image 1.

Image2.jpg
Image 2.

For more help, download the attached sample application.