Monday, May 6, 2013

how to export gridview data to pdf in asp.net

Title: How to export grid view data to PDF in asp.net

Description:
Most the report are send to customers as in excel format.But some scenarios they might ask PDF format.

Example:
Earlier we learned how to export grid view data to word document,Import excel data to SQL Server.In this post i will show how to export grid view data to PDF file in asp.net.While exporting to pdf i have created a pdf document with desired size .Then conversion has done using HTML parser
Note:itextsharp tool has been used.so we need add those DLL into our application
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ExportToPDF.aspx.cs" Inherits="_ExportToPDF" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Export Gridview To PDF documnet</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GvExportDetails" runat="server"
AutoGenerateColumns="False" DataSourceID="SqlDataSource1" EnableModelValidation="True"
>
<Columns>
<asp:BoundField DataField="ExportOrderID"
                 HeaderText="ExportOrderID" SortExpression="ExportOrderID"/>
<asp:BoundField DataField="ExportOrderName"
                HeaderText="ExportOrderName" SortExpression="ExportOrderName"/>
<asp:BoundField DataField="ExportPhone" HeaderText="ExportPhone" SortExpression="ExportPhone" />
<asp:BoundField DataField="ExportAddress" HeaderText="ExportAddress" SortExpression="ExportAddress" />
<asp:BoundField DataField="ExportAmount" HeaderText="ExportAmount" SortExpression="ExportAmount" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:TestConnectionString %>"
SelectCommand="SELECT * FROM [Orders]"></asp:SqlDataSource>
</div>
<asp:Button ID="btnExportOrdersToPdf" runat="server"
             OnClick="btnExportOrdersToPdf_Click"
             Text="OrderDetails to PDF" />
</form>
</body>
</html>
Code behind:
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data;
using System.IO;
using System.Data.SqlClient;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html.simpleparser;

public partial class _ExportToPDF : System.Web.UI.Page 
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnExportOrdersToPdf_Click(object sender, EventArgs e)
{
Response.Clear();
Response.AddHeader("content-disposition", "attachment; filename=OrderDetailsReport.pdf");
Response.Charset = string.Empty;
Response.ContentType = "application/vnd.pdf";
StringWriter swr = new StringWriter();
HtmlTextWriter hwr = new HtmlTextWriter(swr);
GvExportDetails.RenderControl(hwr);
StringReader srd = new StringReader(swr.ToString());
Document OrderDetialsReport = new Document(PageSize.A1, 12f, 12f, 12f, 10f);
HTMLWorker _htmlparser = new HTMLWorker(OrderDetialsReport);
PdfWriter.GetInstance(OrderDetialsReport, Response.OutputStream);
OrderDetialsReport.Open();
_htmlparser.Parse(srd);
OrderDetialsReport.Close();
Response.Write(OrderDetialsReport);
Response.End();
}
public override void VerifyRenderingInServerForm(Control control)
{
}
  
}
Note:Errors might be come like "registerforeventvalidation can only be called during render"

1 comment:

Anonymous said...

Great post with simple example.Thanks

Bel