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