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

how to export gridview data to pdf in asp.net 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

HTML

<body>
  <form id="form1" runat="server">
  <div>
  <asp:GridView ID="gvClientList" AutoGenerateColumns="false" CellPadding="5"
  runat="server" BackColor="#E4E4D6">
  <Columns>
  <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" />
  </Columns>
  <HeaderStyle BackColor="White" Font-Bold="true" ForeColor="Black" />
  <FooterStyle BackColor="#3366CC" />
  </asp:GridView>
  </div>
  <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 />
  </form>
</body>
 

NAMESPACES

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;

CODE BEHIND

protected void Page_Load(object sender, EventArgs e)
  {
  if (!IsPostBack)
  {
  AllClientList();
  }
  }
 
  /* 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;
  gvClientList.DataBind();
 
 
 
 
 
  }
 
  protected void btnPdf_Click(object sender, EventArgs e)
  {
 
 
  Response.ContentType = "application/pdf";
  Response.AddHeader("content-disposition", "attachment;filename=ClientList.pdf");
  Response.Cache.SetCacheability(HttpCacheability.NoCache);
  StringWriter sw = new StringWriter();
  HtmlTextWriter hw = new HtmlTextWriter(sw);
  HtmlForm hf = new HtmlForm();
  gvClientList.Parent.Controls.Add(hf);
  hf.Attributes["runat"] = "server";
  hf.Controls.Add(gvClientList);
  hf.RenderControl(hw);
  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);
  pdfDoc.Open();
  htmlparser.Parse(sr);
 
  pdfDoc.Close();
  Response.Write(pdfDoc);
  Response.End();
 
 
  }
 
  //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

how-to-export-gridview-data-to-pdf-format-output

 

Output fdf format

how-to-export-gridview-data-to-pdf-format-output

Download for you




Realted Article Headline

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

Article Category

How to create asp.net control dynamically
Learn HTML for beginner
DataList example in C Sharp
Mail sending in asp.net 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#
asp.net c # basic tutorial
How to use ajax toolkit in asp.net C#
How to use different types of validation control using asp.net c#
How to use grid view in asp.net c#
Protected by Copyscape Online Plagiarism Detection