How to Convert Gridview to Pdf
How to Convert Gridview to Pdf
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.
sourceforge.net/projects/itextsharp/
or
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
Here with i had attached the sample project.
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"))
Dim str As String = ""
Dim doc As Document = New Document
PdfWriter.GetInstance(doc, New FileStream(FileUpload1.PostedFile.FileName, FileMode.Create))
'string strFileNameWithPath = FileUpload1.PostedFile.FileName;
doc.Open()
doc.Close()
End Sub
End Class
This code snippet used to download zip file
Now , namespace of converting file
using System.IO;
using System.Data;
using System.Net.Mail;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html.simpleparser;
Example of code Behind under mouse click event