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
1using iTextSharp.text;2using 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
01using iTextSharp.text;02using iTextSharp.text.pdf;03protected 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 }01Protected Sub btnPdf_Click(sender As Object, e As EventArgs)02 Dim columns As Integer = GridView1.Columns.Count03 Dim rows As Integer = GridView1.Rows.Count04 Dim tableRows As Integer = rows + 305 Dim gvTable As New iTextSharp.text.Table(columns, tableRows)06 gvTable.BorderWidth = 107 gvTable.BorderColor = New Color(0, 0, 255)08 gvTable.Cellpadding = 509 gvTable.Cellspacing = 510 Dim c1 As New Cell("Create PDF From GridView Example In Asp.Net")11 c1.Header = True12 c1.Colspan = 213 gvTable.AddCell(c1)14 Dim c2 As New Cell("By www.CsharpAspNetArticles.com")15 c2.Colspan = 216 gvTable.AddCell(c2)17 gvTable.AddCell("Name")18 gvTable.AddCell("Location")19 20 For rowCounter As Integer = 0 To rows - 121 For columnCounter As Integer = 0 To columns - 122 Dim strValue As String = GridView1.Rows(rowCounter).Cells(columnCounter).Text23 gvTable.AddCell(strValue)24 Next25 Next26 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]()34End Sub
No comments:
Post a Comment