How to Export Gridview Data to PDF in Asp.Net C#

In this Tutoruial i am going to share with you how to export Gridview Data to  PDF in Asp.Net C#. Generally, While we working on Asp.Net project In many cases we need to export whole data of Gridview to PDF. I got the same requirement to export gridview data to PDF. 
Export Gridview to PDF in Asp.Net C#

For exporting the data from Gridview to PDF We need to use third party tool such as iTextSharp, iText is a PDF library which is allows you to Create, Inspect, Adapt and Maintain the documents in the Portable Document Format which is called PDF. I am using the iTextSharp. It is third party dll which we will use in our tutorial.

You need to Download iTextSharp (.dll file) and Add Reference of following dll file into your project.

For Add New Reference go to solution Explorer >> Right Click on Your Project Name >> Add >> Reference >> Click on Browse Button and Select dll file >> Click OK.

Add Reference

So, Let's Create one Web Page for Demonstration Purpose.

Add Following Reference:
itextsharp.dll
itextsharp.pdfa.dll
Now, Create Database and Create one Data Table.

Create Table

After Create Table Insert some Dummy Records.

Insert Record

Now, add the Namespace in Your .aspx.cs page.

HTML Code:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
 
<!DOCTYPE html>
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        .auto-style2 {
            height45px;
            width323px;
        }
        .auto-style3 {
            color#FF9900;
        }
        .auto-style4 {
            color#FFFFFF;
        }
        .auto-style5 {
            width323px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <table border="0" id="tabl" runat="server">
            <tr>
                <td class="auto-style2" style="text-aligncenterbackground-color#FF9900"><span class="auto-style4"><strong>Export Gridview Data to PDF in Asp.Net C#</strong></span>.</td>
            </tr>
            <tr>
                <td class="auto-style5">
                    <center>
                    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="Enroll_No" DataSourceID="SqlDataSource1">
                        <Columns>
                            <asp:BoundField DataField="Enroll_No" HeaderText="Enroll_No" InsertVisible="False" ReadOnly="True" SortExpression="Enroll_No" />
                            <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
                            <asp:BoundField DataField="Branch" HeaderText="Branch" SortExpression="Branch" />
                            <asp:BoundField DataField="Mobile" HeaderText="Mobile" SortExpression="Mobile" />
                        </Columns>
                    </asp:GridView>
                        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT * FROM [Student]"></asp:SqlDataSource>
                    </center>
                </td>
            </tr>
            <tr>
                <td style="text-aligncenter" class="auto-style5">
                    <asp:Label ID="Label1" runat="server" ForeColor="Green"></asp:Label>
                </td>
            </tr>
            <tr>
                <td style="text-aligncenter" class="auto-style5">
                    <asp:Button ID="PDF" runat="server" Text="PDF" OnClick="PDF_Click" BackColor="#FF3300" BorderStyle="None" ForeColor="White" Height="28px" Width="84px" />
                </td>
            </tr>
            <tr>
                <td class="auto-style5" style="text-aligncenterbackground-color#000000"><span class="auto-style3">Copyright © 2016 Asppoint All Right Reserved.</span>.</td>
            </tr>
        </table>
    </div>
    </form>
</body>
</html>

C# Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.IO;
using System.Web.UI.HtmlControls;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html.simpleparser;
 
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
 
    }
    protected void PDF_Click(object sender, EventArgs e)
    {
        Response.ContentType = "application/pdf";
        Response.AddHeader("content-disposition""attachment;filename=Student_Data.pdf");
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        StringWriter sw = new StringWriter();
        HtmlTextWriter hw = new HtmlTextWriter(sw);
        //Set AllowPaginf false to export the full data
        GridView1.AllowPaging = false;
        GridView1.DataBind();
        //Start the rendering of control here
        GridView1.RenderBeginTag(hw);
        GridView1.HeaderRow.RenderControl(hw);
        foreach (GridViewRow row in GridView1.Rows)
        {
            row.RenderControl(hw);
        }
        GridView1.FooterRow.RenderControl(hw);
        GridView1.RenderEndTag(hw);
        //Apply some style settimgs
        GridView1.Caption = "Your caption";
        GridView1.Style.Add("width""400px");
        GridView1.HeaderRow.Style.Add("font-size""12px");
        GridView1.HeaderRow.Style.Add("font-weight""bold");
        GridView1.Style.Add("border""1px solid black");
        GridView1.Style.Add("text-decoration""none");
        GridView1.Style.Add("font-family""Arial, Helvetica, sans-serif;");
        GridView1.Style.Add("font-size""8px");
        StringReader sr = new StringReader(sw.ToString());
        //creating new pdf document
        Document pdfDoc = new Document(PageSize.A4, 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();
        Response.Clear();
        Label1.Text = "PDF Created Sucsessfully...!";
    }
}

Demo:

Export Gridview Data to PDF in Asp.Net C#


Previous
Next Post »

If you have any kind of question about any post, Feel free to ask.You can simply drop a comment below post. Your feedback and suggestions will be highly appreciated. ConversionConversion EmoticonEmoticon