How to convert Gridview to PDF document using

This is very simple ASP dot Net web application project. This Application is used to convert Gridview to PDF document using itext itextsharp(Its a free tool).
Download the itextsharp from link given below and add this dll in the bin directory.
http://sourceforge.net/projects/itextsharp/
or
http://nodevice.in/dll/itextsharp_dll/item21198.html
After placing the dll in the bin directory write the below code in the aspx.vb page.

In your code-behind, try just adding

Imports iTextSharp.text.pdf
Imports iTextSharp.text

Just Drag and drop one button control and one gridivew
[code]
In ASPX.VB Page
Imports iTextSharp.text.pdf
Imports iTextSharp.text
Imports System.IO
Imports System.Data
Imports System.Data.SqlClient
Partial Class _Default
Inherits System.Web.UI.Page
Dim ds As New DataSet

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then

ShowGridview()
End If
End Sub

Sub ShowGridview()
'bind the database values to Gridveiw using dataset control
Dim con As SqlClient.SqlConnection
Dim da As SqlDataAdapter

Dim str As String
str = "USER=UserID;PASSWORD=PASSWORD;SERVER=SERVER-Name;DATABASE=DATABASE-Name"
con = New SqlConnection(str)
da = New SqlDataAdapter("select FieldName1,FieldName2 from Table_Name", con)
da.Fill(ds, "Employee")
ViewState("DataSetValue") = ds
grvGrid.DataSource = ds
grvGrid.DataBind()
con.Close()

End Sub
Private Sub ConvertGridviewToPdfDocument(ByVal dstHeader2 As DataSet)
Dim doc As Document = New Document
PdfWriter.GetInstance(doc, New FileStream(Request.PhysicalApplicationPath + "\3.pdf", FileMode.Create))
doc.Open()
Dim table As New PdfPTable(dstHeader2.Tables(0).Columns.Count)
Dim widths As Integer() = {25, 25}
table.WidthPercentage = "100"
For i As Integer = 0 To dstHeader2.Tables(0).Columns.Count - 1
Dim ColumnHeader As New Paragraph(dstHeader2.Tables(0).Columns(i).ColumnName.ToString(), FontFactory.GetFont("verdana", 11))
ColumnHeader.Font.SetStyle(Font.BOLD)
Dim cell As New PdfPCell(New Phrase(ColumnHeader))
cell.HorizontalAlignment = Element.ALIGN_CENTER
table.AddCell(cell)
Next

For k As Integer = 0 To dstHeader2.Tables(0).Rows.Count - 1
For j As Integer = 0 To dstHeader2.Tables(0).Columns.Count - 1
Dim ColumnValue As New Paragraph(dstHeader2.Tables(0).Rows(k)(j).ToString(), FontFactory.GetFont("verdana", 10))
table.AddCell(ColumnValue)
Next
Next
table.SetWidths(widths)
doc.Add(table)
doc.Add(New Paragraph(vbLf))
doc.Close()
Response.Redirect("~/3.pdf")
End Sub

Protected Sub btnConvertGridviewToPDF_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnConvertGridviewToPDF.Click
ConvertGridviewToPdfDocument(ViewState("DataSetValue"))
End Sub
End Class

[/code]
In ASPX Page:-

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" 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 runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<tr>
<td>
<asp:GridView ID="grvGrid" RowStyle-Wrap="true" runat="server">
</asp:GridView>
</td>
</tr>
</table>
<asp:Button ID="btnConvertGridviewToPDF" runat="server" Text="ConvertGridviewToPDF" />

</div>
</form>
</body>
</html>

No comments:

Post a Comment

Plz Share your comments...