Many times there is a need in a project's reporting module to Export Gridview into PDF, So by considering the above requirement I decided to write this article especially focusing on beginners and those who want to learn how Export GridView to PDF Using Asp.net C# with the help itextsharp dll.
- To Export the Grid view, we need to use the reference of itextsharp.dll.
- Download the itextsharp.dll from the Internet.
The itextsharp.dll is the free dll available to download which provides some methods to Export Grid view into the pdf file, after adding the reference, use the following references of itextsharp.dll as
- using iTextSharp.text;
- using iTextSharp.text.pdf;
- using iTextSharp.text.html.simpleparser;
I hope, you have done it.
Now before creating the application, let us create a table named employee in a database to from we show the records in a Gridview, the table having the following fields (shown in the following image):
I hope you have created the same type of table.
Now create the project as:
- "Start" - "All Programs" - "Microsoft Visual Studio 2010".
- "File" - "New Project" - "C#" - "Empty Project" (to avoid adding a master page).
- Give the Project name such as ExportGridToPDF or another as you wish and specify the location.
- Then right-click on Solution Explorer - "Add New Item" - Default.aspx page.
- one Buttons, one label and a grid view.
Now let us create function to bind the records to Grid view from Database, If you are beginner and don’t know in details how to bind Grid view from database then refer my following article.
now, for this article create the following function into the default.aspx.cs page to bind the Grid view
- private void Bindgrid()
- {
- connection();
- query = "select *from Employee";
- com = new SqlCommand(query, con);
- SqlDataReader dr = com.ExecuteReader();
- GridView1.DataSource = dr;
- GridView1.DataBind();
- con.Close();
-
- }
now,call this function on page load as
- protected void Button1_Click(object sender, EventArgs e)
- {
- ExportGridToword();
- }
now run the application,then we can see the following records in Grid view as
Now, we have the record to export into MS office word,let us start coding for our actual requirement..,add the VerifyRenderingInServerForm event after the page load which is required while exporting Grid view to Excel, Word, and PDF to avoid the run time error which is occurred like as "GridView' must be placed inside a form tag with runat=server."
- public override void VerifyRenderingInServerForm(Control control)
- {
-
-
- }
Now before creating the function to Export the grid view to PDF add the reference of itextsharp.dll by right clicking of solution explorer,after adding the reference the solution explorer will be look like as
now create the following function to Export the Grid view to PDF as
- private void ExportGridToPDF()
- {
-
- Response.ContentType = "application/pdf";
- Response.AddHeader("content-disposition", "attachment;filename=Vithal_Wadje.pdf");
- Response.Cache.SetCacheability(HttpCacheability.NoCache);
- StringWriter sw = new StringWriter();
- HtmlTextWriter hw = new HtmlTextWriter(sw);
- GridView1.RenderControl(hw);
- StringReader sr = new StringReader(sw.ToString());
- Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
- HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
- PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
- pdfDoc.Open();
- htmlparser.Parse(sr);
- pdfDoc.Close();
- Response.Write(pdfDoc);
- Response.End();
- GridView1.AllowPaging = true;
- GridView1.DataBind();
- }
Now double click on Export button and call the above function on "onclick" as
- protected void Button1_Click(object sender, EventArgs e)
- {
- ExportGridToPDF();
- }
Now the whole code of Default.aspx.cs page will be as follows
- using System;
- using System.Web.UI;
- using System.Configuration;
- using System.Data.SqlClient;
- using System.IO;
- using System.Web;
- using iTextSharp.text;
- using iTextSharp.text.pdf;
- using iTextSharp.text.html.simpleparser;
-
- public partial class _Default : System.Web.UI.Page
- {
- private SqlConnection con;
- private SqlCommand com;
- private string constr, query;
- private void connection()
- {
- constr = ConfigurationManager.ConnectionStrings["getconn"].ToString();
- con = new SqlConnection(constr);
- con.Open();
-
- }
- protected void Page_Load(object sender, EventArgs e)
- {
- if (!IsPostBack)
- {
- Bindgrid();
-
- }
- }
-
- public override void VerifyRenderingInServerForm(Control control)
- {
-
-
- }
-
- private void Bindgrid()
- {
- connection();
- query = "select *from Employee";
- com = new SqlCommand(query, con);
- SqlDataReader dr = com.ExecuteReader();
- GridView1.DataSource = dr;
- GridView1.DataBind();
- con.Close();
-
- }
- protected void Button1_Click(object sender, EventArgs e)
- {
- ExportGridToPDF();
- }
- private void ExportGridToPDF()
- {
-
- Response.ContentType = "application/pdf";
- Response.AddHeader("content-disposition", "attachment;filename=Vithal_Wadje.pdf");
- Response.Cache.SetCacheability(HttpCacheability.NoCache);
- StringWriter sw = new StringWriter();
- HtmlTextWriter hw = new HtmlTextWriter(sw);
- GridView1.RenderControl(hw);
- StringReader sr = new StringReader(sw.ToString());
- Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
- HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
- PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
- pdfDoc.Open();
- htmlparser.Parse(sr);
- pdfDoc.Close();
- Response.Write(pdfDoc);
- Response.End();
- GridView1.AllowPaging = true;
- GridView1.DataBind();
- }
- }
Now run the application and click on Export Button, the following popup is appeared as
Now click on open with option, then you can see that all Grid view records will be exported into PDF File as
Note
- Download the zip file from the attachment for the full source code of an application.
- change connection string from web.config file as per your server location.
Summary
I hope this article is useful for all readers, if you have any suggestion then please contact me including beginners also.