.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Post New Resource Bookmark and Share   

Export gridview data to pdf format

Posted By:satyapriyanayak       Posted Date: March 25, 2013    Points: 40    Category:    URL: http://www.dotnetspark.com  

Export gridview data to pdf format
 

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.

 

Program

 

Default.aspx code

 

<%@ 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>

   

    </div>

    <asp:GridView ID="GridView1" runat="server" BackColor="#FFFF99"

        Font-Bold="True" ForeColor="Red">

        <HeaderStyle BackColor="#66FFFF" Font-Bold="True" ForeColor="#CC3300" />

        <AlternatingRowStyle BackColor="#FFCC99" />

    </asp:GridView>

    <asp:Button ID="Button1" runat="server" Text="Export to Pdf" />

    </form>

</body>

</html>

 

 

Default.aspx.vb code

 

Imports System.Data

Imports System.Data.SqlClient

Imports iTextSharp.text.pdf

Imports iTextSharp.text.html

Imports iTextSharp.text.html.simpleparser

Imports System.IO

Imports iTextSharp.text

 

 

Partial Class _Default

    Inherits System.Web.UI.Page

    Dim strConnString As String = System.Configuration.ConfigurationManager.ConnectionStrings.Item("ConnectionString").ToString()

    Dim con As New SqlConnection(strConnString)

    Dim str As String

    Dim com As SqlCommand

    Dim sqlda As SqlDataAdapter

    Dim ds As DataSet

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        con.Open()

        str = "select * from student"

        com = New SqlCommand(Str, con)

        Dim reader As SqlDataReader

        reader = com.ExecuteReader()

        GridView1.DataSource = reader

        GridView1.DataBind()

        con.Close()

    End Sub

 

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click

        Response.Clear()

        Response.Buffer = True

        Response.ContentType = "application/pdf"

 

        Response.AddHeader("content-disposition", "attachment;filename=student.pdf")

 

        Response.Cache.SetCacheability(HttpCacheability.NoCache)

 

        Dim StringWriter1 As New StringWriter()

 

        Dim HtmlTextWriter1 As New HtmlTextWriter(StringWriter1)

 

        GridView1.RenderControl(HtmlTextWriter1)

 

        Dim StringReader1 As New StringReader(StringWriter1.ToString())

 

        Dim newDocument As New Document(PageSize.A4, 7.0F, 7.0F, 7.0F, 7.0F)

 

        Dim HTMLWorker1 As New HTMLWorker(newDocument)

 

        PdfWriter.GetInstance(newDocument, Response.OutputStream)

 

        newDocument.Open()

 

        HTMLWorker1.Parse(StringReader1)

 

        newDocument.Close()

 

        Response.Write(newDocument)

 

        Response.End()

 

    End Sub

    Public Overrides Sub VerifyRenderingInServerForm(ByVal control As Control)

 

    End Sub

 

End Class