Categories
Asp, Asp.net

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: http://sourceforge.net/projects/itextsharp/

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="http://www.w3.org/1999/xhtml">
<head runat="server">
<style type="text/css">
body {
width: 980px;
margin: 0px auto;
text-align: center;
padding-top: 50px;
font-size: 20px;
}
</style>
<title>How to Export GridView to PDF in Asp.Net C# Using iTextSharp</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h1>How to Export GridView to PDF in Asp.Net C# Using iTextSharp</h1> 

<asp:GridView ID="gv" runat="server" AutoGenerateColumns="false" Width="100%">
            <Columns>
            <asp:BoundField DataField="UID" HeaderText="User ID" />
            <asp:BoundField DataField="UserName" HeaderText="User Name" />
            <asp:BoundField DataField="UEmail" HeaderText="User Email" />
            </Columns>
</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="http://www.hightechnology.in">www.Hightechnology.in</a> |
Hosting partner <a href="http://www.grootstech.com" target="_blank">Grootstech</a>  
</div>
</form>
</body>
</html>

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;
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)
        {
            binddata();
        }
    }
    public void binddata()
    {
        SqlConnection con = new SqlConnection(strcon);
        try
        {
            con.Open();
        }
        catch { }
        string str = "select top(5) * from users";
        SqlCommand cmd = new SqlCommand(str, con);
        SqlDataReader reader;
        reader = cmd.ExecuteReader();
        gv.DataSource = reader;
        gv.DataBind();
        con.Close();
    }
    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;
        binddata();
        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"));
            table.AddCell(cell);
        }

        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);
                    table.AddCell(cell);
                }
            }
        }
 
        Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
        PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
        pdfDoc.Open();
        pdfDoc.Add(table);
        pdfDoc.Close();
        Response.ContentType = "application/pdf";
        Response.AddHeader("content-disposition", "attachment;" +"filename=GridView.pdf");
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Response.Write(pdfDoc);
        Response.End();
   
    }
}

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