add refernce of itext dll file to your project
pdf.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="pdf.aspx.cs" Inherits="pdf" %>
<!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>Grdview with Arraylist</title>
<style type="text/css">
.Gridview
{
font-family:Verdana;
font-size:10pt;
font-weight:normal;
color:black;
width:300px;
}
.style1
{
width: 100%;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="gvdata" runat="server" CssClass="Gridview"
AutoGenerateColumns="False" HeaderStyle-BackColor="#7779AF"
HeaderStyle-ForeColor="White">
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID"/>
<asp:BoundField DataField ="Name" HeaderText="Name" />
</Columns>
<HeaderStyle BackColor="#7779AF" ForeColor="White"></HeaderStyle>
</asp:GridView>
<table class="style1">
<tr>
<td>
</td>
<td>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Search" />
</td>
</tr>
<tr>
<td>
</td>
<td>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
</td>
<td>
<asp:ImageButton ID="btnPdf" runat="server" ImageUrl="~/2.jpg"
onclick="btnExcel_Click" /> </td>
</tr>
<tr>
<td>
</td>
<td>
<asp:GridView ID="gvdata0" runat="server" CssClass="Gridview" AutoGenerateColumns="False"
HeaderStyle-BackColor="#7779AF" HeaderStyle-ForeColor="White">
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID"/>
<asp:BoundField DataField ="EmpName" HeaderText="EmpName" />
</Columns>
<HeaderStyle BackColor="#7779AF" ForeColor="White"></HeaderStyle>
</asp:GridView>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
pdf.aspx.cs
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html.simpleparser;
using System.Data.SqlClient;
using System.IO;
using System.Configuration;
public partial class pdf : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
public override void VerifyRenderingInServerForm(Control control)
{
/* Verifies that the control is rendered */
}
protected void Button1_Click(object sender, EventArgs e)
{
BindGridviewData();
BindGridviewData1();
}
protected void BindGridviewData()
{
SqlConnection con = new SqlConnection(Connect.cnst);
con.Open();
SqlCommand cmd = new SqlCommand("select [ID],[Name] from exm where ID = '" + TextBox1.Text + "'", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
if (ds.Tables[0].Rows.Count == 0)
{
ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());
gvdata.DataSource = ds;
gvdata.DataBind();
int columncount = gvdata.Rows[0].Cells.Count;
gvdata.Rows[0].Cells.Clear();
gvdata.Rows[0].Cells.Add(new TableCell());
gvdata.Rows[0].Cells[0].ColumnSpan = columncount;
gvdata.Rows[0].Cells[0].Text = "No Records Found";
}
else
{
gvdata.DataSource = ds;
gvdata.DataBind();
}
con.Close();
}
protected void BindGridviewData1()
{
SqlConnection con = new SqlConnection(Connect.cnst);
con.Open();
SqlCommand cmd = new SqlCommand("select [ID],[EmpName] from tblLogin where ID = '" + TextBox1.Text + "'", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
if (ds.Tables[0].Rows.Count == 0)
{
ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());
gvdata0.DataSource = ds;
gvdata0.DataBind();
int columncount = gvdata.Rows[0].Cells.Count;
gvdata0.Rows[0].Cells.Clear();
gvdata0.Rows[0].Cells.Add(new TableCell());
gvdata0.Rows[0].Cells[0].ColumnSpan = columncount;
gvdata0.Rows[0].Cells[0].Text = "No Records Found";
}
else
{
gvdata0.DataSource = ds;
gvdata0.DataBind();
}
con.Close();
}
protected void btnPdf_Click(object sender, ImageClickEventArgs e)
{
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=UserDetails.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
//Form.RenderControl(hw);
gvdata.AllowPaging = false;
gvdata.DataBind();
this.BindGridviewData();
this.BindGridviewData1();
gvdata.RenderControl(hw);
gvdata.HeaderRow.Style.Add("width", "15%");
gvdata.HeaderRow.Style.Add("font-size", "10px");
gvdata.Style.Add("text-decoration", "none");
gvdata.Style.Add("font-family", "Arial, Helvetica, sans-serif;");
gvdata.Style.Add("font-size", "8px");
gvdata0.RenderControl(hw);
gvdata0.HeaderRow.Style.Add("width", "15%");
gvdata0.HeaderRow.Style.Add("font-size", "10px");
gvdata0.Style.Add("text-decoration", "none");
gvdata0.Style.Add("font-family", "Arial, Helvetica, sans-serif;");
gvdata0.Style.Add("font-size", "8px");
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);
pdfDoc.Open();
//pdfDoc.NewPage();
pdfDoc.Add(Chunk.NEWLINE);
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();
}
}
output
pdf.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="pdf.aspx.cs" Inherits="pdf" %>
<!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>Grdview with Arraylist</title>
<style type="text/css">
.Gridview
{
font-family:Verdana;
font-size:10pt;
font-weight:normal;
color:black;
width:300px;
}
.style1
{
width: 100%;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="gvdata" runat="server" CssClass="Gridview"
AutoGenerateColumns="False" HeaderStyle-BackColor="#7779AF"
HeaderStyle-ForeColor="White">
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID"/>
<asp:BoundField DataField ="Name" HeaderText="Name" />
</Columns>
<HeaderStyle BackColor="#7779AF" ForeColor="White"></HeaderStyle>
</asp:GridView>
<table class="style1">
<tr>
<td>
</td>
<td>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Search" />
</td>
</tr>
<tr>
<td>
</td>
<td>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
</td>
<td>
<asp:ImageButton ID="btnPdf" runat="server" ImageUrl="~/2.jpg"
onclick="btnExcel_Click" /> </td>
</tr>
<tr>
<td>
</td>
<td>
<asp:GridView ID="gvdata0" runat="server" CssClass="Gridview" AutoGenerateColumns="False"
HeaderStyle-BackColor="#7779AF" HeaderStyle-ForeColor="White">
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID"/>
<asp:BoundField DataField ="EmpName" HeaderText="EmpName" />
</Columns>
<HeaderStyle BackColor="#7779AF" ForeColor="White"></HeaderStyle>
</asp:GridView>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
pdf.aspx.cs
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html.simpleparser;
using System.Data.SqlClient;
using System.IO;
using System.Configuration;
public partial class pdf : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
public override void VerifyRenderingInServerForm(Control control)
{
/* Verifies that the control is rendered */
}
protected void Button1_Click(object sender, EventArgs e)
{
BindGridviewData();
BindGridviewData1();
}
protected void BindGridviewData()
{
SqlConnection con = new SqlConnection(Connect.cnst);
con.Open();
SqlCommand cmd = new SqlCommand("select [ID],[Name] from exm where ID = '" + TextBox1.Text + "'", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
if (ds.Tables[0].Rows.Count == 0)
{
ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());
gvdata.DataSource = ds;
gvdata.DataBind();
int columncount = gvdata.Rows[0].Cells.Count;
gvdata.Rows[0].Cells.Clear();
gvdata.Rows[0].Cells.Add(new TableCell());
gvdata.Rows[0].Cells[0].ColumnSpan = columncount;
gvdata.Rows[0].Cells[0].Text = "No Records Found";
}
else
{
gvdata.DataSource = ds;
gvdata.DataBind();
}
con.Close();
}
protected void BindGridviewData1()
{
SqlConnection con = new SqlConnection(Connect.cnst);
con.Open();
SqlCommand cmd = new SqlCommand("select [ID],[EmpName] from tblLogin where ID = '" + TextBox1.Text + "'", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
if (ds.Tables[0].Rows.Count == 0)
{
ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());
gvdata0.DataSource = ds;
gvdata0.DataBind();
int columncount = gvdata.Rows[0].Cells.Count;
gvdata0.Rows[0].Cells.Clear();
gvdata0.Rows[0].Cells.Add(new TableCell());
gvdata0.Rows[0].Cells[0].ColumnSpan = columncount;
gvdata0.Rows[0].Cells[0].Text = "No Records Found";
}
else
{
gvdata0.DataSource = ds;
gvdata0.DataBind();
}
con.Close();
}
protected void btnPdf_Click(object sender, ImageClickEventArgs e)
{
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=UserDetails.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
//Form.RenderControl(hw);
gvdata.AllowPaging = false;
gvdata.DataBind();
this.BindGridviewData();
this.BindGridviewData1();
gvdata.RenderControl(hw);
gvdata.HeaderRow.Style.Add("width", "15%");
gvdata.HeaderRow.Style.Add("font-size", "10px");
gvdata.Style.Add("text-decoration", "none");
gvdata.Style.Add("font-family", "Arial, Helvetica, sans-serif;");
gvdata.Style.Add("font-size", "8px");
gvdata0.RenderControl(hw);
gvdata0.HeaderRow.Style.Add("width", "15%");
gvdata0.HeaderRow.Style.Add("font-size", "10px");
gvdata0.Style.Add("text-decoration", "none");
gvdata0.Style.Add("font-family", "Arial, Helvetica, sans-serif;");
gvdata0.Style.Add("font-size", "8px");
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);
pdfDoc.Open();
//pdfDoc.NewPage();
pdfDoc.Add(Chunk.NEWLINE);
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();
}
}
output
0 comments:
Post a Comment