********************************************************************************
/*Add Stylesheet in your application names StyleSheet.css*/
body {}
.gridView{width:100%;background:#ffffff;border:1px solid #476db6; border-collapse:separate;}
.gridView th{border:0px;background:#a7b7fe;color:#000000;text-align:center;font-size:18px;}
.gridView td{border:0px;padding-left:10px;}
.gridView tr:hover{cursor:pointer; background:#ffff00;}
.gridView tr:last-child:hover{ background:#000;background:#a7b7fe;}
.altRow{background:#d1daff;}
.gridPager{background:#a7b7fe;color:#000000;}
.gridPager table{margin:0px auto;}
.gridPager table td{width:20px;}
.gridPager table span{cursor:pointer;font-weight:bold;color:#1a1ac9;}
.gridPager table a{cursor:pointer;text-decoration:none;color:#000000;}
.gridPager table a:hover{text-decoration:underline;}
********************************************************************************
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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>Create PDF Report in Asp.Net Using iTextSharp </title>
<link href="StyleSheet.css" rel="stylesheet" type="text/css" />
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="butPDF" runat="server" Text="Generate PDF" onclick="butPDF_Click" /><br />
<a href="http://sourceforge.net/projects/itextsharp/">Download iTextsharp</a>
</div>
<div>
<asp:GridView ID="GridView1"
runat="server"
AutoGenerateColumns="false"
AllowSorting="true"
CssClass="gridView"
AlternatingRowStyle-CssClass="altRow"
PagerStyle-CssClass="gridPager">
<Columns>
<asp:BoundField DataField="Productid" HeaderText="Product Id"/>
<asp:BoundField DataField="Productname" HeaderText="Product"/>
<asp:BoundField DataField="Productdetails" HeaderText="Details"/>
<asp:BoundField DataField="Unitprice" HeaderText="Price" />
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
*********************************************************************************
using System;
using System.IO;
using System.Web;
using System.Web.UI.WebControls;
using PDF = iTextSharp.text.pdf;
using PDFDoc = iTextSharp.text;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Products products = new Products();
GridView1.DataSource = products.ReadAll();
GridView1.DataBind();
}
}
protected void butPDF_Click(object sender, EventArgs e)
{
PDFReport("Computers & Related Products", GridView1, "pdf1");
}
/// <summary>
/// Create PDF
/// </summary>
/// <param name="heading"></param>
/// <param name="grid"></param>
/// <param name="reportName"></param>
public void PDFReport(String heading, GridView grid, String reportName)
{
Int32 columns = grid.Columns.Count;
MemoryStream ms = new MemoryStream();
PDFDoc.Document document = new PDFDoc.Document();
PDF.PdfWriter.GetInstance(document, ms);
PDFDoc.Font font = new PDFDoc.Font(PDFDoc.Font.FontFamily.TIMES_ROMAN, 10, 0, new PDFDoc.BaseColor(0, 0, 0));
PDFDoc.Font hfont = new PDFDoc.Font(PDFDoc.Font.FontFamily.TIMES_ROMAN, 15, 0, new PDFDoc.BaseColor(0, 0, 0));
document.Open();
PDF.PdfPTable table = new PDF.PdfPTable(columns);//No. of columns.
//float[] widths = new float[] { 5f, 5f, 5f, 5f, 5f, 5f,5f,5f,5f };
//table.SetWidths(widths);
table.WidthPercentage = 100;//Width in persentage.
//Create Header
PDF.PdfPTable header = new PDF.PdfPTable(1);
PDF.PdfPCell cell = new PDF.PdfPCell(new PDFDoc.Phrase(heading, hfont));//Report Head.
cell.HorizontalAlignment = 1;//0=Left, 1=Centre, 2=Right
cell.Border = 0;
header.WidthPercentage = 100;
header.AddCell(cell);
document.Add(header);
document.Add(new PDFDoc.Phrase("\n"));//Enter new line
PDFDoc.Chunk reportCreationDate = new PDFDoc.Chunk(String.Format("Report Date : {0}", DateTime.Now), font);
document.Add(reportCreationDate);
document.Add(new PDFDoc.Phrase("\n"));//Enter new line
// Custumer Name
PDF.PdfPTable info = new PDF.PdfPTable(2);
info.WidthPercentage = 100;
PDF.PdfPCell Ccell = new PDF.PdfPCell(new PDFDoc.Phrase(String.Format("Customer : {0}", "Kaushik Mistry"), font));
Ccell.Border = 0;
Ccell.HorizontalAlignment = 0;
info.AddCell(Ccell);
PDF.PdfPCell DGcell = new PDF.PdfPCell(new PDFDoc.Phrase(String.Format("Durable goods report : {0}", "Computers & Related Products"), font));
DGcell.Border = 0;
DGcell.HorizontalAlignment = 2;
info.AddCell(DGcell);
document.Add(info);
//Create Line
PDF.PdfPTable line = new PDF.PdfPTable(1);
//Report Head.
PDF.PdfPCell lcell = new PDF.PdfPCell(new PDFDoc.Phrase(""));
lcell.BorderWidthTop = 0;
lcell.BorderWidthLeft = 0;
lcell.BorderWidthRight = 0;
lcell.BorderWidthBottom = 1;
line.WidthPercentage = 100;
line.AddCell(lcell);
document.Add(line);
document.Add(new PDFDoc.Phrase("\n"));//Enter new line
for (Int32 i = 0; i <= 1; )
{
for (Int32 j = 0; j <= grid.Columns.Count - 1; j++)
{
PDF.PdfPCell _cell = new PDF.PdfPCell(new PDFDoc.Phrase(HttpUtility.HtmlDecode(grid.Columns[j].ToString()), font));
_cell.BackgroundColor = new PDFDoc.BaseColor(245, 245, 245);//Background color of cell in RGB.
_cell.HorizontalAlignment = 1;//0=Left, 1=Centre, 2=Right
table.AddCell(_cell);
}
break;
}
for (Int32 i = 0; i <= grid.Rows.Count - 1; i++)
{
for (Int32 j = 0; j <= grid.Columns.Count - 1; j++)
{
PDF.PdfPCell _cell = new PDF.PdfPCell(new PDFDoc.Phrase(HttpUtility.HtmlDecode(grid.Rows[i].Cells[j].Text), font));
_cell.HorizontalAlignment = 1;//0=Left, 1=Centre, 2=Right
table.AddCell(_cell);
}
}
document.Add(table);
document.Close();
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", String.Format("attachment;filename=Report-{0}.pdf", reportName));
Response.Buffer = true;
Response.OutputStream.Write(ms.GetBuffer(), 0, ms.GetBuffer().Length);
Response.OutputStream.Flush();
Response.End();
Response.Clear();
}
}
********************************************************************************
using System;
using System.Web.Configuration;
public class Connection
{
public Connection(){}
/// <summary>
/// Get Connection string.
/// <summary>
public static String Cs
{
get
{
return WebConfigurationManager.ConnectionStrings["cs"].ConnectionString;
}
}
}
********************************************************************************
#region Namespaces
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
#endregion
/// <summary>
/// class Products
/// </summary>
public class Products
{
#region Properties
private Int32 _Productid;
/// <summary>
///Gets or Sets the Int32 value of Productid
/// </summary>
public Int32 Productid { get { return _Productid;} set { _Productid = value;} }
private String _Productname;
/// <summary>
///Gets or Sets the String value of Productname
/// </summary>
public String Productname { get { return _Productname;} set { _Productname = value;} }
private String _Productdetails;
/// <summary>
///Gets or Sets the String value of Productdetails
/// </summary>
public String Productdetails { get { return _Productdetails;} set { _Productdetails = value;} }
private Decimal _Unitprice;
/// <summary>
///Gets or Sets the Decimal value of Unitprice
/// </summary>
public Decimal Unitprice { get { return _Unitprice;} set { _Unitprice = value;} }
private Int32 _Quantity;
/// <summary>
///Gets or Sets the Int32 value of Quantity
/// </summary>
public Int32 Quantity { get { return _Quantity;} set { _Quantity = value;} }
#endregion
#region Private Meambers
private SqlCommand cmd;
private SqlConnection con;
private SqlDataReader table;
private String ConnectionString = String.Empty;
#endregion
#region Constructor
/// <summary>
///Default constructor
/// </summary>
public Products()
{
ConnectionString=Connection.Cs;
}
/// <summary>
///Parameterised constructor
/// </summary>
public Products(String connectionString)
{
ConnectionString=connectionString;
}
#endregion
#region Public Methods
/// <summary>
/// This meathod returns all records of Products table.
/// </summary>
/// <returns> All rows of Products table.</returns>
public List<Products> ReadAll()
{
using(con = new SqlConnection(ConnectionString))
{
try
{
List<Products> list = new List<Products>();
if (con.State == ConnectionState.Closed)
{
con.Open();
}
cmd = new SqlCommand("Select Top(1000)* From [dbo].[Products]",con);
using(table = cmd.ExecuteReader())
{
while (table.Read())
{
Products obj = new Products();
obj._Productid = table.GetInt32(0);
obj._Productname = table.GetString(1);
obj._Productdetails = table.GetString(2);
obj._Unitprice = table.GetDecimal(3);
obj._Quantity = table.GetInt32(4);
list.Add(obj);
}
//return (list.Count > 0) ? list : GetEmptyList(list);
return list;
}
}
finally
{
if (con.State == ConnectionState.Open)
{
con.Close();
}
}
}
}
#endregion
}
********************************************************************************
<add name="cs" connectionString="Data Source=Your Data Source;Initial Catalog=softwarekaffee;Persist Security Info=True;User ID=Your User Id;Password=Your Password"/>
</connectionStrings>
********************************************************************************
********************************************************************************
Go
create database SoftwareKaffee
Go
use SoftwareKaffee
Go
Create table Products
(
ProductId int identity primary key,
ProductName varchar(50),
ProductDetails nvarchar(200),
UnitPrice decimal(7,2),
Quantity int
)
Go
insert into Products values('Dell Laptop','Processor Intel(R) Cort(T) i5 CPU, Ra 4GB',55000,4)
insert into Products values( 'HCL Laptop','Processor Intel(RR) Cort(T2) i7 CPU, Ra 8GB',15000,30 )
insert into Products values( 'HP Laptop','Processor Intel(R) Cort(T) i5 CPU, Ra 4GB', 23000 ,4 )
insert into Products values( 'HCL Laptop','Processor Intel(R) Cort(T) i5 CPU, Ra 4GB',35000,20 )
insert into Products values( 'Linovo Laptop','Processor Intel(R) Cort(T) i5 CPU, Ra 2GB',25000,30 )
insert into Products values( 'Copac Laptop','Processor Intel(R) Cort(T) i5 CPU, Ra 4GB',30000,30 )
insert into Products values( 'Dell Laptop','Processor Intel(R) Cort(T) i5 CPU, Ra 4GB',45000,10 )
insert into Products values( 'HP Laptop','Processor Intel(R) Cort(T) i5 CPU, Ra 4GB',55000,30 )
insert into Products values( 'HCL Laptop','Processor Intel(R) Cort(T) i5 CPU, Ra 2GB',35000,20 )
insert into Products values( 'Linovo Laptop','Processor Intel(R) Cort(T) i5 CPU, Ra 4GB',25000,30 )
insert into Products values( 'Copac Laptop','Processor Intel(R) Cort(T) i5 CPU, Ra 4GB',30000,60 )
insert into Products values( 'Dell Laptop','Processor Intel(R) Cort(T) i3 CPU, Ra 2GB',35000,30 )
insert into Products values( 'HP Laptop','Processor Intel(R) Cort(T) i3 CPU, Ra 2GB',55000,30 )
insert into Products values( 'HCL Laptop','Processor Intel(R) Cort(T) i3 CPU, Ra 2GB',34000,20 )
insert into Products values( 'Linovo Laptop','Processor Intel(R) Cort(T) i3 CPU, Ra 2GB',35000,30 )
insert into Products values( 'Copac Laptop','Processor Intel(R) Cort(T) i3 CPU, Ra 2GB',40000,30 )
insert into Products values( 'Dell Laptop','Processor Intel(RR) Cort(T2) i7 CPU, Ra 8GB',65000,30 )
insert into Products values( 'HP Laptop','Processor Intel(RR) Cort(T2) i7 CPU, Ra 8GB',75000,1 )
insert into Products values( 'HCL Laptop','Processor Intel(RR) Cort(T2) i7 CPU, Ra 8GB',45000,30 )
insert into Products values( 'Linovo Laptop','Processor Intel(RR) Cort(T2) i7 CPU, Ra 8GB',45000,30 )
insert into Products values( 'Copac Laptop','Processor Intel(RR) Cort(T2) i7 CPU, Ra 8GB',37000,5 )
No comments:
Post a Comment