How to Export Gridview Data to PDF in - Free Asp.Net,MVC,AngularJs,Jquery,Javascript,Sql Server,WCF,Entity framework snippets and tutorial

How to Export Gridview Data to PDF in

Introduction: in this article I will explain how we can Export the Gridview Data to PDF in
Data Export feature is unavailable in so we use the 3rd party Library. Here we use the iTextSharp DLL and reference. To download iTextSharp DLL Click Here.
Take a new website. Put the downloaded iTextSharp DLL in Bin folder and build the project/website. Add a webform to website and design .aspx page as shown below:

    <form id="form1" runat="server">
    <tr><td>&nbsp;</td><td align="right">
        <asp:Button ID="Button1" runat="server" Text="Export To PDF"
            onclick="Button1_Click" /></td></tr>
    <tr><td>&nbsp;</td><td> <asp:GridView ID="grdstudent" runat="server" AutoGenerateColumns="false" DataKeyNames="STUDENT_ID" DataSourceID="SqlDataSource1">
        <asp:BoundField DataField="STUDENT_NAME" HeaderText="STUDENT NAME" />
        <asp:BoundField DataField="STUDENT_ADDRESS" HeaderText="STUDENT ADDRESS" />
        <asp:BoundField DataField="STUDENT_CLASS" HeaderText="STUDENT CLASS" />
        <asp:SqlDataSource ID="SqlDataSource1" runat="server"
            ConnectionString="<%$ ConnectionStrings:TestBlogConnectionString %>"
            SelectCommand="SELECT * FROM [STUDENT_DETAIL]"></asp:SqlDataSource>

Now go to .aspx.cs page and write the below given code:

using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html.simpleparser;
using System.IO;

public override void VerifyRenderingInServerForm(Control control)
       //To add control to form
    protected void Button1_Click(object sender, EventArgs e)
        Response.ContentType = "application/pdf";
        Response.AddHeader("content-disposition", "attachment;filename=Student'sDetail.pdf");
        StringWriter sw = new StringWriter();
        HtmlTextWriter ht = new HtmlTextWriter(sw);
        grdstudent.AllowPaging = false;
        grdstudent.HeaderRow.Style.Add("width", "13%");
        grdstudent.HeaderRow.Style.Add("font-size", "11px");
        grdstudent.Style.Add("text-decoration", "none");
        grdstudent.Style.Add("font-family", "Arial, Helvetica, sans-serif;");
        grdstudent.Style.Add("font-size", "10px");
        StringReader sr = new StringReader(sw.ToString());
        Document PDF = new Document(PageSize.A2, 7f, 7f, 7f, 0f);
        HTMLWorker htmlparser = new HTMLWorker(PDF);
        PdfWriter.GetInstance(PDF, Response.OutputStream);

In VB (.aspx.vb)

Imports iTextSharp.text
Imports iTextSharp.text.pdf
Imports iTextSharp.text.html.simpleparser
Imports System.IO

Public Overrides Sub VerifyRenderingInServerForm(ByVal control As Control)
        'To add control to form
    End Sub
    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Response.ContentType = "application/pdf"
        Response.AddHeader("content-disposition", "attachment;filename=Student'sDetail.pdf")
        Dim sw As New StringWriter()
        Dim ht As New HtmlTextWriter(sw)
        grdstudent.AllowPaging = False
        grdstudent.HeaderRow.Style.Add("width", "13%")
        grdstudent.HeaderRow.Style.Add("font-size", "11px")
        grdstudent.Style.Add("text-decoration", "none")
        grdstudent.Style.Add("font-family", "Arial, Helvetica, sans-serif;")
        grdstudent.Style.Add("font-size", "10px")
        Dim sr As New StringReader(sw.ToString())
        Dim PDF As New Document(PageSize.A2, 7.0F, 7.0F, 7.0F, 0.0F)
        Dim htmlparser As New HTMLWorker(PDF)
        PdfWriter.GetInstance(PDF, Response.OutputStream)

    End Sub

If you get any error "Control 'grdstudent' of type 'GridView' must be placed inside a form tag with runat=server" read this article:

What do you think about this article?

If you found this article useful, please share and follow on Facebook, Twitter, Google Plus and other social media websites. To get free updates subscribe to newsletter. Please put your thoughts and feedback in comments section.

Share this

Share on FacebookTweet on TwitterPlus on Google+