Tuesday, March 12, 2013

Export Gridview to PDF in ASP.Net

In web development, Gridview is the most frequently used control for data display in ASP.Net. Sometimes we have a requirement to export the Gridview data into PDF file. Here, I am show you how to export Gridview data into PDF file?.

In this post , I am not concerning the formatting of data into PDF. For exporting the data, I am using the iTextSharp (third party dll) in this post.

In previous post, I explained Nested Gridview with Expand/Collapse FunctionalityHighlight gridview row on mouse over in asp net web page and some other articles related to  Gridview, ASP.Net and others.

Exporting Gridview to PDF

First of all download the iTextSharp and add the reference of following dlls into your application.

itextsharp.dll
itextsharp.pdfa.dll

Now add the Gridview and button control into your ASPX page. Configure the data source for Gridview. ASPX will be look as shown below.

<asp:GridView ID="grdSample" runat="server" AutoGenerateColumns="False"
            DataSourceID="SqlDataSource1" EnableModelValidation="True" Width="400px">
     <Columns>           
                <asp:BoundField DataField="ID" HeaderText="ID" SortExpression="ID" />
                <asp:BoundField DataField="FirstName" HeaderText="First Name"
                    SortExpression="FirstName" />
                <asp:BoundField DataField="Lastname" HeaderText="Last Name"
                    SortExpression="Lastname" />
                <asp:BoundField DataField="Age" HeaderText="Age" SortExpression="Age" />
                <asp:BoundField DataField="Phone" HeaderText="Phone" SortExpression="Phone" />
                <asp:BoundField DataField="Email" HeaderText="Email" SortExpression="Email" />
            </Columns>
        </asp:GridView>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server"
            ConnectionString="<%$ ConnectionStrings:SampleDBConnectionString %>"
            SelectCommand="SELECT * FROM [SampleInfoTable]"></asp:SqlDataSource>
        <br />
        <asp:Button ID="btnExport" runat="server" Text="Expot to PDF"
            onclick="btnExport_Click" />

Now add the following namespaces in your page.

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

After adding the namespaces, write the following code in Click event of the button.

protected void btnExport_Click(object sender, EventArgs e)
{
 Response.ContentType = "application/pdf";
 Response.AddHeader("content-disposition", "attachment;filename=Sample.pdf");
 Response.Cache.SetCacheability(HttpCacheability.NoCache);
 StringWriter sw = new StringWriter();
 HtmlTextWriter hw = new HtmlTextWriter(sw);
 //Set AllowPaginf false to export the full data
 grdSample.AllowPaging = false;
 grdSample.DataBind();
 //Start the rendering of control here
 grdSample.RenderBeginTag(hw);
 grdSample.HeaderRow.RenderControl(hw);
 foreach (GridViewRow row in grdSample.Rows)
 {
   row.RenderControl(hw);
 }
 grdSample.FooterRow.RenderControl(hw);
 grdSample.RenderEndTag(hw);
 //Apply some style settimgs
 grdSample.Caption = "Your caption";
 grdSample.Style.Add("width", "400px");
 grdSample.HeaderRow.Style.Add("font-size", "12px");
 grdSample.HeaderRow.Style.Add("font-weight", "bold");
 grdSample.Style.Add("border", "1px solid black");
 grdSample.Style.Add("text-decoration", "none");
 grdSample.Style.Add("font-family", "Arial, Helvetica, sans-serif;");
 grdSample.Style.Add("font-size", "8px");
 StringReader sr = new StringReader(sw.ToString());
 //creating new pdf document
 Document pdfDoc = new Document(PageSize.A4, 7f, 7f, 7f, 0f);
 HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
 PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
 pdfDoc.Open();
 htmlparser.Parse(sr);
 pdfDoc.Close();
 Response.Write(pdfDoc);
 Response.End();
 Response.Clear();
}

Output Screens
Gridview To PDF Page

Gridview to Pdf Output
Gridview to PDF
 Happy coding!!

No comments:

Post a Comment

If You Enjoyed This Post Please Take 5 Seconds To Share It.

^ Scroll to Top hgpromo