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; }
}