In this example i'm explaining how to Generate Or Create PDF From GridView In Asp.Net Using ITextSharp and C# VB
Create BIN Folder by right clicking in solution explorer and selecting Add Asp.Net Folder option and Put itextsharp.dllin it.
Place one GridView on aspx page and populate it from database using SQLDataSource
Place One Button on page to Create PDF in Click Event of it.
Add itextsharp namespace refrences in code behind of page
1
using
iTextSharp.text;
2
using
iTextSharp.text.pdf;
HTML SOURCE OF PAGE
1: <asp:GridView ID="GridView1" runat="server"
2: AutoGenerateColumns="False"
3: AllowPaging="true" PageSize="5"
4: DataSourceID="SqlDataSource1">
5: <Columns>
6: <asp:BoundField DataField="Name" HeaderText="Name"/>
7: <asp:BoundField DataField="Location"
8: HeaderText="Location"/>
9: </Columns>
10: </asp:GridView>
11:
12: <asp:SqlDataSource ID="SqlDataSource1" runat="server"
13: ConnectionString
14: ="<%$ ConnectionStrings:ConnectionString %>"
15: SelectCommand="SELECT [Name], [Location]
16: FROM [Test]">
17: </asp:SqlDataSource>
18:
19: <asp:Button ID="btnPdf" runat="server"
20: Text="Create PDF"
21: onclick="btnPdf_Click" />
Place one button to create PDF from Gridview and write below mentioned code in Click Event of Button.
C# CODE
01
using
iTextSharp.text;
02
using
iTextSharp.text.pdf;
03
protected
void
btnPdf_Click(
object
sender, EventArgs e)
04
{
05
int
columns = GridView1.Columns.Count;
06
int
rows = GridView1.Rows.Count;
07
int
tableRows = rows + 3;
08
iTextSharp.text.Table gvTable =
new
iTextSharp.text.Table(columns, tableRows);
09
gvTable.BorderWidth = 1;
10
gvTable.BorderColor =
new
Color(0, 0, 255);
11
gvTable.Cellpadding = 5;
12
gvTable.Cellspacing = 5;
13
Cell c1 =
new
Cell(
"Create PDF From GridView Example In Asp.Net"
);
14
c1.Header =
true
;
15
c1.Colspan = 2;
16
gvTable.AddCell(c1);
17
Cell c2 =
new
Cell(
"By www.CsharpAspNetArticles.com"
);
18
c2.Colspan = 2;
19
gvTable.AddCell(c2);
20
gvTable.AddCell(
"Name"
);
21
gvTable.AddCell(
"Location"
);
22
23
for
(
int
rowCounter = 0; rowCounter < rows; rowCounter++)
24
{
25
for
(
int
columnCounter = 0; columnCounter < columns; columnCounter++)
26
{
27
string
strValue = GridView1.Rows[rowCounter].Cells[columnCounter].Text;
28
gvTable.AddCell(strValue);
29
}
30
}
31
Document Doc =
new
Document();
32
PdfWriter.GetInstance(Doc, Response.OutputStream);
33
Doc.Open();
34
Doc.Add(gvTable);
35
Doc.Close();
36
Response.ContentType =
"application/pdf"
;
37
Response.AddHeader(
"content-disposition"
,
"attachment; filename=GridView.pdf"
);
38
Response.End();
39
}
01
Protected
Sub
btnPdf_Click(sender
As
Object
, e
As
EventArgs)
02
Dim
columns
As
Integer
= GridView1.Columns.Count
03
Dim
rows
As
Integer
= GridView1.Rows.Count
04
Dim
tableRows
As
Integer
= rows + 3
05
Dim
gvTable
As
New
iTextSharp.text.Table(columns, tableRows)
06
gvTable.BorderWidth = 1
07
gvTable.BorderColor =
New
Color(0, 0, 255)
08
gvTable.Cellpadding = 5
09
gvTable.Cellspacing = 5
10
Dim
c1
As
New
Cell(
"Create PDF From GridView Example In Asp.Net"
)
11
c1.Header =
True
12
c1.Colspan = 2
13
gvTable.AddCell(c1)
14
Dim
c2
As
New
Cell(
"By www.CsharpAspNetArticles.com"
)
15
c2.Colspan = 2
16
gvTable.AddCell(c2)
17
gvTable.AddCell(
"Name"
)
18
gvTable.AddCell(
"Location"
)
19
20
For
rowCounter
As
Integer
= 0
To
rows - 1
21
For
columnCounter
As
Integer
= 0
To
columns - 1
22
Dim
strValue
As
String
= GridView1.Rows(rowCounter).Cells(columnCounter).Text
23
gvTable.AddCell(strValue)
24
Next
25
Next
26
Dim
Doc
As
New
Document()
27
PdfWriter.GetInstance(Doc, Response.OutputStream)
28
Doc.Open()
29
Doc.Add(gvTable)
30
Doc.Close()
31
Response.ContentType =
"application/pdf"
32
Response.AddHeader(
"content-disposition"
,
"attachment; filename=GridView.pdf"
)
33
Response.[
End
]()
34
End
Sub
No comments:
Post a Comment