Export GridView Data to PDF Format in VB.NET

In this article we will know how to export gridview data to pdf format. For that we need itextsharp.dll file. Get it from

 
Table structure
 
Tabble-in-SQL-Server.gif
 
First add the itextsharp.dll file into the application as click solution explorer- Right click on your application file-Add reference-On browse tab search the itextsharp.dll file from your computer-Click ok-Finish. Then you will notice that a Bin folder will be created where you will find itextsharp.dll file present inside that folder.
 
Default.aspx code
  1. <%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>  
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  3. <html xmlns="http://www.w3.org/1999/xhtml">  
  4. <head runat="server">  
  5.     <title>Untitled Page</title>  
  6. </head>  
  7. <body>  
  8.     <form id="form1" runat="server">  
  9.     <div>  
  10.     </div>  
  11.     <asp:GridView ID="GridView1" runat="server" BackColor="#FFFF99"  
  12.         Font-Bold="True" ForeColor="Red">  
  13.         <HeaderStyle BackColor="#66FFFF" Font-Bold="True" ForeColor="#CC3300" />  
  14.         <AlternatingRowStyle BackColor="#FFCC99" />  
  15.     </asp:GridView>  
  16.     <asp:Button ID="Button1" runat="server" Text="Export to Pdf" />  
  17.     </form>  
  18. </body>  
  19. </html>  
Default.aspx.vb code
  1. Imports System.Data  
  2. Imports System.Data.SqlClient  
  3. Imports iTextSharp.text.pdf  
  4. Imports iTextSharp.text.html  
  5. Imports iTextSharp.text.html.simpleparser  
  6. Imports System.IO  
  7. Imports iTextSharp.text  
  8. Partial Class _Default  
  9.     Inherits System.Web.UI.Page  
  10.     Dim strConnString As String = System.Configuration.ConfigurationManager.ConnectionStrings.Item("ConnectionString").ToString()  
  11.     Dim con As New SqlConnection(strConnString)  
  12.     Dim str As String  
  13.     Dim com As SqlCommand  
  14.     Dim sqlda As SqlDataAdapter  
  15.     Dim ds As DataSet  
  16.     Protected Sub Page_Load(ByVal sender As ObjectByVal e As System.EventArgs) Handles Me.Load  
  17.         con.Open()  
  18.         str = "select * from student"  
  19.         com = New SqlCommand(Str, con)  
  20.         Dim reader As SqlDataReader  
  21.         reader = com.ExecuteReader()  
  22.         GridView1.DataSource = reader  
  23.         GridView1.DataBind()  
  24.         con.Close()  
  25.     End Sub  
  26.     Protected Sub Button1_Click(ByVal sender As ObjectByVal e As System.EventArgs) Handles Button1.Click  
  27.         Response.Clear()  
  28.         Response.Buffer = True  
  29.         Response.ContentType = "application/pdf"  
  30.         Response.AddHeader("content-disposition""attachment;filename=student.pdf")  
  31.         Response.Cache.SetCacheability(HttpCacheability.NoCache)  
  32.         Dim StringWriter1 As New StringWriter()   
  33.         Dim HtmlTextWriter1 As New HtmlTextWriter(StringWriter1)  
  34.         GridView1.RenderControl(HtmlTextWriter1)  
  35.         Dim StringReader1 As New StringReader(StringWriter1.ToString())  
  36.         Dim newDocument As New Document(PageSize.A4, 7.0F, 7.0F, 7.0F, 7.0F)  
  37.         Dim HTMLWorker1 As New HTMLWorker(newDocument)  
  38.         PdfWriter.GetInstance(newDocument, Response.OutputStream)  
  39.         newDocument.Open()  
  40.         HTMLWorker1.Parse(StringReader1)  
  41.         newDocument.Close()  
  42.         Response.Write(newDocument)  
  43.         Response.End()  
  44.     End Sub  
  45.     Public Overrides Sub VerifyRenderingInServerForm(ByVal control As Control  
  46.     End Sub  
  47. End Class  
Output 
Export-to-PDF-in-VB.net.gif


Similar Articles