Exporting grid data to PDF using asp.net

Here I will explain how to export gridview data to PDF using asp.net.


In my previous articles I explained clearly “Exporting grid data to Excel and word”. Now I will explain how to export gridview data to PDF using asp.net. In asp.net we don’t have direct feature to export gridview data to PDF for that reason here I am using third party library ITextSharp reference. The dll is available here ITextSharp first download dll from this site after that create one new website in visual studio and add ITextsharp dll reference to newly created website after that design aspx page like this

 <html xmlns=”http://www.w3.org/1999/xhtml”&gt;

<head id=”Head1″ runat=”server”>




<form id=”form1″ runat=”server”>




<td align=”right”>

<asp:ImageButton ID=”btnPDF” runat=”server” ImageUrl=”~/PDF.jpg” Width=”32px”

Height=”32px” onclick=”btnPDF_Click”/>





<asp:GridView runat=”server” ID=”gvdetails” DataSourceID=”dsdetails”  

AllowPaging=”true” AllowSorting=”true” AutoGenerateColumns=”false”>

<RowStyle BackColor=”#EFF3FB” />

<FooterStyle BackColor=”#507CD1″ Font-Bold=”True” ForeColor=”White” />

<PagerStyle BackColor=”#2461BF” ForeColor=”White” HorizontalAlign=”Center” />

<HeaderStyle BackColor=”#507CD1″ Font-Bold=”True” ForeColor=”White” />

<AlternatingRowStyle BackColor=”White” />


<asp:BoundField DataField=”UserId” HeaderText=”UserId” />

<asp:BoundField DataField=”UserName” HeaderText=”UserName” />

<asp:BoundField DataField=”LastName” HeaderText=”LastName” />

<asp:BoundField DataField=”Location” HeaderText=”Location” />






<asp:SqlDataSource ID=”dsdetails” runat=”server”ConnectionString=”<%$ConnectionStrings:dbconnection %>

SelectCommand=”select * from UserInformation”/>





Here don’t forgot to set the connection string in web.config file here I am getting database connection from web.config file for that reason you need to set the connectionstring in web.config file like this 


<add name=dbconnection connectionString=Data Source=SD;Integrated Security=true;Initial Catalog=MySampleDB/>


After set the connection string in web.config file now we are able to get the data from database and we can bind that data to gridview by using sqldatasource. Now if you run application gridview appears like this

Now in code behind add these references

using iTextSharp.text;

using iTextSharp.text.pdf;

using iTextSharp.text.html.simpleparser;

After that write the following code in code behind

public override void VerifyRenderingInServerForm(Control control)


/* Verifies that the control is rendered */


protected void btnPDF_Click(object sender, ImageClickEventArgs e)


Response.ContentType = “application/pdf”;



StringWriter sw = new StringWriter();

HtmlTextWriter hw = new HtmlTextWriter(sw);

gvdetails.AllowPaging = false;






gvdetails.Style.Add(“font-family”“Arial, Helvetica, sans-serif;”);


StringReader sr = new StringReader(sw.ToString());

Document pdfDoc = new Document(PageSize.A2, 7f, 7f, 7f, 0f);

HTMLWorker htmlparser = new HTMLWorker(pdfDoc);

PdfWriter.GetInstance(pdfDoc, Response.OutputStream);







Demo for PDF document

If you observe above code I added one function that is VerifyRenderingInServerForm this function is used to avoid the error like “control must be placed in inside of form tag”. If we setVerifyRenderingInServerForm function then compiler will think that controls rendered before exporting and our functionality will work perfectly

Download : sample code attached
Now if you’re getting any error message please feel free to let me know ?

8 responses to this post.

  1. Magnificent beat ! I would like to apprentice while you amend your web site, how could i subscribe for a blog website? The account aided me a acceptable deal. I had been tiny bit acquainted of this your broadcast offered bright clear concept

    • Posted by krishna on March 7, 2013 at 4:35 pm

      with sqldatasouce is working fine.In my code i am using “Storedprocedure” here i am getting “System.IO.IOException: The document has no pages.” please resolve this error

  2. Posted by John on August 16, 2012 at 7:58 pm

    I’m getting an IOException error that says “The document has no pages”….also, I converted the sample to vb, so maybe this is part of the problem?

  3. Posted by John on August 16, 2012 at 10:30 pm

    disregard the IOException…it was actually a JScript runtime error:
    Sys.WebForms.PageRequestManagerParserErrorException: The message received from the server could not be parsed. Common causes for this error are when the response is modified by calls to Response.Write(), response filters, HttpModules, or server trace is enabled.
    Details: Error parsing near ‘%PDF-1.4
    %��3 0 obj

  4. Posted by John on August 17, 2012 at 1:03 am

    it was a problem with the update panel…which I fixed by adding another update panel just for the export button…

  5. Internet is a source of information and knowledge. But there are so many sites in net which are nothing but gibberish. But when I visit your site – I felt that – in net there are lots of sites (like this) from where I can get lot of information too. And if I say truly – your site is fantastic. I’ve bookmarked your site in my browser; I hope in future days I’ll get more valuable information from your site.

  6. Posted by Kamaldeep on October 31, 2012 at 12:29 am

    I am getting this exception : IO exception was unhandled by user. The document has no pages.

  7. We’re a group of volunteers and opening a new scheme in our community. Your website offered us with valuable information to work on. You’ve done a formidable job and our entire community will be grateful to you.

Comments are closed.

%d bloggers like this: