How to Export GridView to PDF in Asp.Net C# Using iTextSharp

How to Export GridView to PDF in Asp.Net C# Using iTextSharp

In this post we will learn How to Export GridView to PDF in Asp.Net C# Using iTextSharp. Here I will show you, how to export GridView data to PDF document using iTextSharp. First we have to download iTextSharp.dll class library and include to our project.

You could download iTextSharp.dll class library here:

Then open your project and add iTextSharp DLL to your project, by right click on your project name > Add Reference > Browse > select iTextSharp Dll >OK.

Design View:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html>

<html xmlns="">
<head runat="server">
<style type="text/css">
body {
width: 980px;
margin: 0px auto;
text-align: center;
padding-top: 50px;
font-size: 20px;
<title>How to Export GridView to PDF in Asp.Net C# Using iTextSharp</title>
<form id="form1" runat="server">
<h1>How to Export GridView to PDF in Asp.Net C# Using iTextSharp</h1> 

<asp:GridView ID="gv" runat="server" AutoGenerateColumns="false" Width="100%">
            <asp:BoundField DataField="UID" HeaderText="User ID" />
            <asp:BoundField DataField="UserName" HeaderText="User Name" />
            <asp:BoundField DataField="UEmail" HeaderText="User Email" />
</asp:GridView><br /><br />

<asp:Button ID="exgv" runat="server" Text="Export Gridview To PDF" OnClick="exgv_Click" />

<br /><br />
<br /><br />
All rights reserved by <a href=""></a> |
Hosting partner <a href="" target="_blank">Grootstech</a>  


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using iTextSharp.text.pdf;
using iTextSharp.text.html.simpleparser;
using iTextSharp.text;
using System.IO;
using System.Net;

public partial class _Default : System.Web.UI.Page
    String strcon = ConfigurationManager.ConnectionStrings["connnn"].ConnectionString;
    protected void Page_Load(object sender, EventArgs e)
        if (!Page.IsPostBack)
    public void binddata()
        SqlConnection con = new SqlConnection(strcon);
        catch { }
        string str = "select top(5) * from users";
        SqlCommand cmd = new SqlCommand(str, con);
        SqlDataReader reader;
        reader = cmd.ExecuteReader();
        gv.DataSource = reader;
    protected void exgv_Click(object sender, EventArgs e)
        iTextSharp.text.Table table = new iTextSharp.text.Table(gv.Columns.Count);
        table.Cellpadding = 2;
        table.Width = 100;
        for (int i = 0; i < gv.Columns.Count; i++)
            string cellText = Server.HtmlDecode(gv.Columns[i].HeaderText); 
            iTextSharp.text.Cell cell = new iTextSharp.text.Cell(cellText);
            cell.BackgroundColor = new Color(System.Drawing.ColorTranslator.FromHtml("#cccccc"));

        for (int i = 0; i < gv.Rows.Count; i++)
            if (gv.Rows[i].RowType == DataControlRowType.DataRow)
                for (int j = 0; j < gv.Columns.Count; j++)
                    string cellText = Server.HtmlDecode(gv.Rows[i].Cells[j].Text);
                    iTextSharp.text.Cell cell = new iTextSharp.text.Cell(cellText);
        Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
        PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
        Response.ContentType = "application/pdf";
        Response.AddHeader("content-disposition", "attachment;" +"filename=GridView.pdf");

How to Export GridView to PDF in Asp.Net C# Using iTextSharp