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;

- "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";//not recommended this i have wrtten just for example,write stored procedure for security
- com = new SqlCommand(query, con);
- SqlDataReader dr = com.ExecuteReader();
- GridView1.DataSource = dr;
- GridView1.DataBind();
- con.Close();
- }
- protected void Button1_Click(object sender, EventArgs e)
- {
- ExportGridToword();
- }

- public override void VerifyRenderingInServerForm(Control control)
- {
- //required to avoid the runtime error "
- //Control 'GridView1' of type 'GridView' must be placed inside a form tag with runat=server."
- }

- 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();
- }
- 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)
- {
- //required to avoid the runtime error "
- //Control 'GridView1' of type 'GridView' must be placed inside a form tag with runat=server."
- }
- private void Bindgrid()
- {
- connection();
- query = "select *from Employee";//not recommended this i have wrtten just for example,write stored procedure for security
- 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();
- }
- }


- 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


Anil AjmeraPosted Sep 8, 2023, 12:50 AM EDT
In PDF report, the background colour and the text colours are not being inherited
Don AugastinPosted Jul 23, 2020, 6:55 AM EDT
///VerifyRenderingInServerForm(Control control)/// working for export 1 page of grid data. But its not working to export more than 10 page. I need to export more than 10 page (10 datas per page). Please help...
Subin ThomasPosted Sep 25, 2019, 9:42 PM EDT
Working fine but can you tell me how to add some text in this , is it possible?
Pramod PeyyalaPosted Aug 9, 2018, 9:15 PM EDT
How to download pdf insted of open?
Rahul KondhalkarPosted Jul 30, 2018, 11:51 PM EDT
Getting Unable to cast object of type 'iTextSharp.text.html.simpleparser.CellWrapper' to type 'iTextSharp.text.Paragraph'. error at click on download button...
Francisco VargasPosted Mar 1, 2018, 2:41 PM EST
Me da error : No se puede convertir un objeto de tipo 'iTextSharp.text.html.simpleparser.CellWrapper' al tipo 'iTextSharp.text.Paragraph'.
ShivanyaPosted Sep 17, 2017, 10:28 PM EDT
Thank You Very Much Sir, It is very useful...
Darwin SutharPosted Aug 9, 2017, 8:28 AM EDT
By this code the data exported to pdf and downloaded, now i want to pdf should open in another tab in the same browser.. please help me....
Darwin SutharPosted Aug 9, 2017, 8:25 AM EDT
Working good. Thanks a lot
chaitanya pathakPosted Dec 20, 2016, 10:22 PM EST
Excellent Article for beginer
Mayur KoshtiPosted Nov 16, 2016, 7:21 PM EST
Hi am getting this error even my gridview is in form tag and i already given runat=server still it is showing error would you like to help me out for this An exception of type 'System.Web.HttpException' occurred in System.Web.dll but was not handled in user codeAdditional information: Control 'ContentPlaceHolder1_GridView1' of type 'GridView' must be placed inside a form tag with runat=server.
Vithal WadjeTOP 100Posted Mar 22, 2016, 6:47 AM EDT
Use iTextSharp properties to apply css
shanigarapu rakeshPosted Mar 21, 2016, 10:12 PM EDT
the above code is not working in my project. it is generating only data. styles not apply..
Shridhar SharmaPosted Mar 21, 2016, 2:47 AM EDT
nice share sir. :)
Vithal WadjeTOP 100Posted Feb 28, 2016, 6:38 PM EST
Download latest iTextSharp dll
Mohammad AmanPosted Feb 27, 2016, 2:24 AM EST
On local it is working . when i am uploading on server it is not generating pdf,, in my code HTMLWorker is giving warning "iTextSharp.text.html.simpleparser.HTMLWorker is obsolete"
Vithal WadjeTOP 100Posted Sep 3, 2015, 9:19 AM EDT
Download sample code
Santiago BevilacquaPosted Sep 2, 2015, 10:07 PM EDT
hello, In the exported pdf there is no column header. How can I fix it?
Promita DuttaPosted Aug 21, 2015, 2:23 AM EDT
how do I add author name in pdf document and another problem is when i click page index and then click export button its give error... I want pdf page by page... plz help me out,,
Venkat SPosted Jun 19, 2015, 1:14 AM EDT
if it is possible export values to pdf,without using iTextSharp....
Khargesh RajputPosted May 26, 2015, 8:38 PM EDT
sir i am getting error The URI prefix is not recognized on line htmlparser.Parse(sr);
Nayan PatelPosted Apr 21, 2015, 8:51 PM EDT
no data in pdf
Vithal WadjeTOP 100Posted Nov 19, 2014, 2:17 AM EST
Add the reference of Itextsharp in your project
santhosh kumarPosted Nov 19, 2014, 1:45 AM EST
Hai, frinds i need the same for windows application in c#.net from the datagridview . when is use this i get errors, can anyone help me for finding the exact code for exporting the datagridview to a pdf file.
Vithal WadjeTOP 100Posted Nov 9, 2014, 10:47 PM EST
sure i will check
Poonam MalvePosted Nov 9, 2014, 9:40 PM EST
actually the i tried same procedure u explained... its working fine for it... but when i applied in my project its not working overthere... please refer the snapshot -> http://i.imgur.com/DCW2ios.png - for sample project(working fine) and http://i.imgur.com/n1ymZCn.png - for my project
Vithal WadjeTOP 100Posted Nov 9, 2014, 9:32 PM EST
its may be a popup blocker in your browser,send me screen shot if you have possible
Poonam MalvePosted Nov 9, 2014, 8:27 PM EST
tried in both firefox and crome
Vithal WadjeTOP 100Posted Nov 9, 2014, 2:51 AM EST
which browser you have using
Poonam MalvePosted Nov 9, 2014, 2:07 AM EST
After clicking on Export Button , popup didn't appeared.... please suggest where m going wrong?
Former memberPosted Jun 12, 2014, 11:54 PM EDT
This is really nice article http://www.webcodeexpert.com/2013/06/bind-and-export-gridview-data-to-pdf.html
Vithal WadjeTOP 100Posted Jun 8, 2014, 6:09 AM EDT
yes, i think so,but try it may be supported
super developerPosted Jun 8, 2014, 4:08 AM EDT
Not support arabic text
Vithal WadjeTOP 100Posted Apr 14, 2014, 10:04 AM EDT
yes,their is lots of free dll you can google it,also we can write C# custom code but its very time consuming
Prasanna Kumar MuduliPosted Apr 10, 2014, 6:02 AM EDT
Is there any way available,that export of gridview to PDF without using iTextSharp dll?If yes,please explain..
Sompal AryaPosted Mar 13, 2014, 10:47 PM EDT
I have given gridview column width & color but when i did export to pdf there was no color & width of columns