How to Export Grid view to PDF?
Gridview data is Export to PDF document with help of free itextsharp reference.
Download itextsharp.dll file from below link
After download add this itextsharp.dll reference into your project
Select Project Name in solution explorer ->Right click select add reference ->select Browse tab->and choose your itextsharp.dll location click Ok button. Then write below code in your web page.
Client Side
I placed one Grid View and button under form tag
<asp:GridView ID="GridView1" runat="server">
</asp:GridView><br />
<asp:Button ID="btnExport" runat="server" Text="Export to PDF"
onclick="btnExport_Click"/>
Server Side
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.Data;
using System.IO;
using System.Data.SqlClient;
public partial class Export_PDF : System.Web.UI.Page
{
//Server connection detail
SqlConnection sqlcon = new SqlConnection(@"Server=RAVI\SQLEXPRESS;database=test;uid=xxxx;pwd=yyyy;");
SqlCommand sqlcmd = new SqlCommand();
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter();
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
BindGrid();
}
}
void BindGrid()
{
sqlcmd = new SqlCommand("select * from emp1", sqlcon);
sqlcon.Open();
da = new SqlDataAdapter(sqlcmd);
da.Fill(dt);
//store this data table value in session for export as pdf in future
Session["Export_Table"] = dt;
GridView1.DataSource = dt;
GridView1.DataBind();
sqlcon.Close();
}
protected void btnExport_Click(object sender, EventArgs e)
{
if (Session["Export_Table"] != null)
{
ExportToPdf(Session["Export_Table"] as DataTable);
}
else {
Response.Write("No data to Export!");
}
}
public void ExportToPdf(DataTable ExDataTable)
{
//Here set page size as A4
Document pdfDoc = new Document(PageSize.A4, 10, 10, 10, 10);
try
{
PdfWriter.GetInstance(pdfDoc, System.Web.HttpContext.Current.Response.OutputStream);
pdfDoc.Open();
//Header Logo path
string imageFilePath = Server.MapPath("~/images/Header.jpg");
iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(imageFilePath);
//Set Height and width for your Image Header
image.ScaleToFit(80f, 60f);
//Here I give space before image
image.SpacingBefore = 0f;
//Here I give space after image
image.SpacingAfter = 1f;
image.Alignment = Element.HEADER;
//Add Header image to PDF Document
pdfDoc.Add(image);
//Set Font Properties for PDF File
Font fnt = FontFactory.GetFont("Times New Roman", 12);
DataTable dt = ExDataTable;
if (dt != null)
{
PdfPTable PdfTable = new PdfPTable(dt.Columns.Count);
PdfPCell PdfPCell = null;
//Here we create PDF file tables
for (int rows = 0; rows < dt.Rows.Count; rows++)
{
for (int column = 0; column < dt.Columns.Count; column++)
{
PdfPCell = new PdfPCell(new Phrase(new Chunk(dt.Rows[rows][column].ToString(), fnt)));
PdfTable.AddCell(PdfPCell);
}
}
// Finally Add pdf table to the document
pdfDoc.Add(PdfTable);
}
pdfDoc.Close();
Response.ContentType = "application/pdf";
//Set default file Name as current datetime
Response.AddHeader("content-disposition", "attachment; filename=" + DateTime.Now.ToString("yyyyMMdd") + ".pdf");
System.Web.HttpContext.Current.Response.Write(pdfDoc);
Response.Flush();
Response.End();
}
catch (Exception ex)
{
Response.Write(ex.ToString());
}
}
}