Posted by : Anwar Hossain
Category : How to use grid view in c#

how to export gridview data to pdf in c#

Dear viewers Now I will show how to export girdview data to pdf format .For this , We need to download iTextSharp it can be download from download ITextSharp it is open source and free to use and helps to convert html content into pdf.After downloading the file needs to included it’s two dll file Itextsharp.dll and itextsharp.pdfa.dll and needs to add this namespaces iTextSharp.text, iTextSharp.text.pdf, iTextSharp.text.html, iTextSharp.text.html.simpleparser.

Export gridview data into pdf format


  <form id="form1" runat="server">
  <asp:GridView ID="gvClientList" AutoGenerateColumns="false" CellPadding="5"
  runat="server" BackColor="#E4E4D6">
  <asp:BoundField HeaderText="Client Name" DataField="_ClientName" />
   <asp:BoundField HeaderText="Client Phone" DataField="_ClientPhone" />
  <asp:BoundField HeaderText="Adress " DataField="_ClientAddress" />
  <asp:BoundField HeaderText="Due Payment" DataField="_DuePayment" />
  <HeaderStyle BackColor="White" Font-Bold="true" ForeColor="Black" />
  <FooterStyle BackColor="#3366CC" />
  <br />
  <asp:Button ID="btnPdf" runat="server" Text="Export data to pdf format"
  OnClick="btnPdf_Click" BackColor="#6600CC"
  BorderStyle="Solid" BorderWidth="2px" Font-Bold="True" ForeColor="White"
  Height="30px" BorderColor="#333333" />
  <br />


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.IO;
using System.Drawing;
using iTextSharp.text;
using iTextSharp.text.html.simpleparser;
using iTextSharp.text.pdf;
using System.Web.UI.HtmlControls;


protected void Page_Load(object sender, EventArgs e)
  if (!IsPostBack)
  /* Confirms that an HtmlForm control is rendered for the specified ASP.NET
  server control at run time. */
  public override void VerifyRenderingInServerForm(Control control)
  //Create temporarydata but real time data will be loaded form database
  private void AllClientList()
  List<Client> ClientList = new List<Client>();
  ClientList.Add(new Client("Iqbal Hossain","01916751804","Noakhali",50000));
   ClientList.Add(new Client("Jabed Hossain", "01916751805", "Lakmipur", 80000));
  ClientList.Add(new Client("Fazlul Goni Mozumder", "01916751806", "Chandpur", 80000));
  ClientList.Add(new Client("Prince Hossain", "01916751807", "Rangpur", 90000));
  ClientList.Add(new Client("Alampana", "01916781864", "Narayangonj", 80000));
  ClientList.Add(new Client("Sumaya Akter", "01916751804", "Sirajgonj", 50000));
  ClientList.Add(new Client("Siful Islam", "01916751808", "Dhaka", 1000000));
  ClientList.Add(new Client("Mizanur Rahman", "01916751807", "Dhaka", 20000));
  gvClientList.DataSource = ClientList;
  protected void btnPdf_Click(object sender, EventArgs e)
  Response.ContentType = "application/pdf";
  Response.AddHeader("content-disposition", "attachment;filename=ClientList.pdf");
  StringWriter sw = new StringWriter();
  HtmlTextWriter hw = new HtmlTextWriter(sw);
  HtmlForm hf = new HtmlForm();
  hf.Attributes["runat"] = "server";
  StringReader sr = new StringReader(sw.ToString());
  Document pdfDoc = new Document(PageSize.A4, 10f, 250f, 10f, -10f);
  HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
  PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
  //Create class for temporarydata
  public class Client
  public Client(string ClientName, string ClientPhone, string ClientAddress, decimal DuePayment)
  _ClientName = ClientName;
  _ClientPhone = ClientPhone;
  _ClientAddress = ClientAddress;
  _DuePayment = DuePayment;
  public string _ClientName { get; set; }
  public string _ClientPhone { get; set; }
  public string _ClientAddress { get; set; }
  public decimal _DuePayment { get; set; }

Out Put



Output fdf format


Download for you

Realted Article Headline

Gridview datetime format example in ASP.Net c#
Displaying total column value in the Gridview Footer using c#.
How to create grid view paging in using c#
How to bind Multi-Dimensional Arrays Data to gird view in using C#
How to bind Two-Dimensional Arrays Data to gird view in using C#
How to bind Single-Dimensional Arrays Data to gird view in using C#
how to export gridview data to pdf in c#
how to export gridview data to word in using c#
how to export gridview data to excel in c#
How to delete row from grid view showing confirmation JavaScript using c#
How to delete data from grid view using row deleting event in c#
How to bind data into gridview using code behind in c#
How to scroll gird view overflow data in c#
How to Bind data into grid view control using c#

Article Category

How to create control dynamically
Learn HTML for beginner
DataList example in C Sharp
Mail sending in c#
State Management in ASP C #
Basic sql tutorial for Beginner
DataTable example in ASP.Net C#
How to use LINQ in ASP.NET C# c # basic tutorial
How to use ajax toolkit in C#
How to use different types of validation control using c#
How to use grid view in c#
Protected by Copyscape Online Plagiarism Detection