Description:-
In this Example we Explain that How to Export Gridview data
to PDF and display same Look as in Gridview in Asp or C#.Net.
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.
Exporting Gridview Daata to PDF you have to First of all download the iTextSharp and add the reference of following dlls into your application.
You can also Export you Data to Word or excel to show this Example Click Below Link
Export Gridview to Word :- Gridview to Word
Export Gridview to Excel :- Gridview to Excel
Implement Remember Me functionality using CheckBox ASP.Net
set WaterMark Text in PDF using itextsharp in C#
How to set Default Button in MVC Web Form Application
How to Bind XML File data to Treeview in asp.net
JQuery datepicker calender with Dropdown month and year in asp.net.
Exporting Gridview Daata to PDF you have to First of all download the iTextSharp and add the reference of following dlls into your application.
You can also Export you Data to Word or excel to show this Example Click Below Link
Export Gridview to Word :- Gridview to Word
Export Gridview to Excel :- Gridview to Excel
Implement Remember Me functionality using CheckBox ASP.Net
set WaterMark Text in PDF using itextsharp in C#
How to set Default Button in MVC Web Form Application
How to Bind XML File data to Treeview in asp.net
JQuery datepicker calender with Dropdown month and year in asp.net.
itextsharp.dll
itextsharp.pdfa.dll
itextsharp.pdfa.dll
And add the namespaces in your aspx.cs means CodeBehind page.
using iTextSharp.text;
using iTextSharp.text.html.simpleparser;
using iTextSharp.text.pdf;
word.aspx:-
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="word.aspx.cs" Inherits="word" EnableEventValidation="false" %>
<!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 id="Head1" runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table border="1" id="tb" runat="server">
<tr>
<td>
<asp:ImageButton ID="btnWord" runat="server" ImageUrl="~/WordImage.jpg"
onclick="btnWord_Click" />
</td>
<td>
<asp:ImageButton ID="btnPDF" runat="server" ImageUrl="~/index.jpg" Width="20"
onclick="btnpdf_Click" />
</td>
</tr>
<tr>
<td>
<asp:GridView runat="server" ID="gvdetails" 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" />
<Columns>
<asp:BoundField DataField="id" HeaderText="UserId" />
<asp:BoundField DataField="FirstName" HeaderText="UserName" />
<asp:BoundField DataField="LastName" HeaderText="LastName" />
<asp:BoundField DataField="age" HeaderText="age" />
</Columns>
</asp:GridView>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
word.aspx.cs:-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.IO;
using System.Web.UI.HtmlControls;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html.simpleparser;
public partial class word : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
grid();
}
}
public void grid()
{
DataSet ds = new DataSet();
string con = @"Data
Source=SQLDB;Initial Catalog=Demo;User ID=Demoh;Password=Demo1@";
string q = "select * from
tierword";
SqlConnection conn = new SqlConnection(con);
SqlCommand cmd = new SqlCommand(q, conn);
SqlDataAdapter sa = new SqlDataAdapter();
conn.Open();
cmd.ExecuteNonQuery();
sa.SelectCommand = cmd;
sa.Fill(ds);
gvdetails.DataSource = ds;
gvdetails.DataBind();
}
public override void VerifyRenderingInServerForm(Control control)
{
/*
Verifies that the control is rendered */
}
}
protected void btnpdf_Click(object sender, ImageClickEventArgs 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
gvdetails.AllowPaging = false;
gvdetails.DataBind();
//Start
the rendering of control here
gvdetails.RenderBeginTag(hw);
gvdetails.HeaderRow.RenderControl(hw);
foreach (GridViewRow row in gvdetails.Rows)
{
row.RenderControl(hw);
}
gvdetails.FooterRow.RenderControl(hw);
gvdetails.RenderEndTag(hw);
//Apply
some style settimgs
gvdetails.Caption = "Your caption";
gvdetails.Style.Add("width", "400px");
gvdetails.HeaderRow.Style.Add("font-size", "12px");
gvdetails.HeaderRow.Style.Add("font-weight", "bold");
gvdetails.Style.Add("border", "1px solid black");
gvdetails.Style.Add("text-decoration", "none");
gvdetails.Style.Add("font-family", "Arial, Helvetica, sans-serif;");
gvdetails.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();
}
Export GridView to PDF in C#
ReplyDeleteHi
ReplyDeleteGreat job.It works pretty fine for me,BTW I got some useful page from google when I was trying to Export data form listview to PDF using c#.And smple codes and demos are available here.Hope it can contribute.