Export Our GridView Selected Record to a PDF

The following is my data table:

data table
                                                                              Image 1

  • My table in design mode:

    design mode
                                                                                  Image 2

  • Now my aspx code:
    1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>  
    2.   
    3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
    4. <html xmlns="http://www.w3.org/1999/xhtml">  
    5. <head runat="server">  
    6.     <title>Export Record To PDF using iTextSharp</title>  
    7. </head>  
    8. <body>  
    9.     <form id="form1" runat="server">  
    10.     <div>  
    11.         <table cellpadding="10" cellspacing="10" style="border: solid 10px red; background-color: Skyblue;"  
    12.             width="90%" align="center">  
    13.             <tr>  
    14.                 <td style="height: 35px; background-color: Yellow; font-weight: bold; font-size: 16pt;  
    15.                     font-family: Times New Roman; color: Red" align="center">  
    16.                     Export Record To PDF  
    17.                 </td>  
    18.             </tr>  
    19.             <tr>  
    20.                 <td>  
    21.                     <asp:GridView ID="GridViewRecord" HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White"  
    22.                         RowStyle-BackColor="#A1DCF2" runat="server" AutoGenerateColumns="False"   
    23.                         BackColor="White" BorderColor="#CC9966" BorderStyle="None" BorderWidth="1px"   
    24.                         CellPadding="4">  
    25. <RowStyle BackColor="White" ForeColor="#330099"></RowStyle>  
    26.                         <Columns>  
    27.                             <asp:TemplateField>  
    28.                                 <ItemTemplate>  
    29.                                     <asp:CheckBox ID="chkSelect" runat="server" />  
    30.                                 </ItemTemplate>  
    31.                             </asp:TemplateField>  
    32.                             <asp:BoundField DataField="EmployeeName" HeaderText="Employee Name" ItemStyle-Width="150px" />  
    33.                             <asp:BoundField DataField="CompanyName" HeaderText="Company Name" ItemStyle-Width="120px" />  
    34.                             <asp:BoundField DataField="ProjectName" HeaderText="Project Name" ItemStyle-Width="100px" />  
    35.                             <asp:BoundField DataField="JoiningDate" HeaderText="Joining Date" ItemStyle-Width="150px" />  
    36.                             <asp:BoundField DataField="Address" HeaderText="Address" ItemStyle-Width="100px" />  
    37.                         </Columns>  
    38.                         <FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />  
    39.                         <PagerStyle BackColor="#FFFFCC" ForeColor="#330099" HorizontalAlign="Center" />  
    40.                         <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" />  
    41.   
    42. <HeaderStyle BackColor="#990000" ForeColor="#FFFFCC" Font-Bold="True"></HeaderStyle>  
    43.                     </asp:GridView>  
    44.                 </td>  
    45.             </tr>  
    46.             <tr>  
    47.                 <td>  
    48.                     <asp:Button ID="btnExportGridRecord" runat="server" Text="Export To PDF" OnClick="btnExportGridRecord_Click" />  
    49.                 </td>  
    50.             </tr>  
    51.     </div>  
    52.     </form>  
    53. </body>  
    54. </html>  
  • Now my aspx.cs code:
    1. using System;  
    2. using System.Configuration;  
    3. using System.Data;  
    4. using System.Linq;  
    5. using System.Web;  
    6. using System.Web.Security;  
    7. using System.Web.UI;  
    8. using System.Web.UI.HtmlControls;  
    9. using System.Web.UI.WebControls;  
    10. using System.Web.UI.WebControls.WebParts;  
    11. using System.Xml.Linq;  
    12. using System.IO;  
    13. using System.Data.SqlClient;  
    14. using iTextSharp.text;  
    15. using iTextSharp.text.html.simpleparser;  
    16. using iTextSharp.text.pdf;  
    17.   
    18. public partial class _Default : System.Web.UI.Page  
    19. {  
    20.     protected void Page_Load(object sender, EventArgs e)  
    21.     {  
    22.         if (!IsPostBack)  
    23.         {  
    24.             this.BindGridData();  
    25.         }  
    26.     }  
    27.   
    28.     private void BindGridData()  
    29.     {  
    30.         SqlConnection con = new SqlConnection(@"Server=MyPC\SqlServer2k8;database=Test;Integrated Security=true;");  
    31.         SqlCommand cmd = new SqlCommand("SELECT TOP 15 * FROM Employee");  
    32.         SqlDataAdapter da = new SqlDataAdapter(cmd);  
    33.         cmd.Connection = con;  
    34.         DataTable dt = new DataTable();  
    35.         da.Fill(dt);  
    36.         GridViewRecord.DataSource = dt;  
    37.         GridViewRecord.DataBind();  
    38.     }  
    39.   
    40.     protected void btnExportGridRecord_Click(object sender, EventArgs e)  
    41.     {  
    42.         using (StringWriter sw = new StringWriter())  
    43.         {  
    44.             using (HtmlTextWriter hw = new HtmlTextWriter(sw))  
    45.             {  
    46.                 GridViewRecord.Columns[0].Visible = false;  
    47.                 foreach (GridViewRow row in GridViewRecord.Rows)  
    48.                 {  
    49.                     if (row.RowType == DataControlRowType.DataRow)  
    50.                     {  
    51.                         row.Visible = (row.FindControl("chkSelect"as CheckBox).Checked;  
    52.                     }  
    53.                 }  
    54.   
    55.                 GridViewRecord.RenderControl(hw);  
    56.                 StringReader sr = new StringReader(sw.ToString());  
    57.                 Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);  
    58.                 HTMLWorker htmlparser = new HTMLWorker(pdfDoc);  
    59.                 PdfWriter.GetInstance(pdfDoc, Response.OutputStream);  
    60.                 pdfDoc.Open();  
    61.                 htmlparser.Parse(sr);  
    62.                 pdfDoc.Close();  
    63.   
    64.                 Response.ContentType = "application/pdf";  
    65.                 Response.AddHeader("content-disposition""attachment;filename=Records.pdf");  
    66.                 Response.Cache.SetCacheability(HttpCacheability.NoCache);  
    67.                 Response.Write(pdfDoc);  
    68.                 Response.End();  
    69.             }  
    70.         }  
    71.     }  
    72.     public override void VerifyRenderingInServerForm(Control control)  
    73.     {  
    74.         /* Verifies that the control is rendered */  
    75.     }  
    76. }  
  • Now run the application:

    Export to PDF
                                                                                  Image 3

  • Select records here and click on the Export to PDF button:

    click on Export to PDF button
                                                                             Image 4

  • From here you can open or save the PDF file. Now view your PDF file:

    save pdf file
                                                                               Image 5

Up Next
    Ebook Download
    View all
    Learn
    View all