Solved

Export to pdf

Posted on 2016-08-23
2
96 Views
Last Modified: 2016-08-25
I have a gridview user control and I am trying to Export the gridview data to a pdf. It seems that the fact that the grid is in a user control that the code that I am trying to use is not working? I dont know if this is the reason, I can only speculate from all the things Ive seen out there and nothing is working? Anybody have a clue what it could be?
Ive tried 10 different code snippets and none has worked.

This was the latest code I tried:
        'PDF
        Response.Clear()
        Response.Buffer = True
        Response.ContentType = "application/pdf"
        Response.AddHeader("content-disposition", "attachment;filename=gridTest.pdf")
        Response.Cache.SetCacheability(HttpCacheability.NoCache)
        Dim sw As New StringWriter()
        Dim hw As New HtmlTextWriter(sw)
        myGridview.AllowPaging = False
        Dim frm As New HtmlForm()
        myGridview.Parent.Controls.Add(frm)
        frm.Attributes("runat") = "server"
        frm.Controls.Add(myGridview)
        frm.RenderControl(hw)
        myGridview.DataBind()
        Dim sr As New StringReader(sw.ToString())
        Dim pdfDoc As New iTextSharp.text.Document(PageSize.A4, 10.0F, 10.0F, 10.0F, 0.0F)
        Dim htmlparser As New HTMLWorker(pdfDoc)
        PdfWriter.GetInstance(pdfDoc, Response.OutputStream)
        pdfDoc.Open()
        htmlparser.Parse(sr)
        pdfDoc.Close()
        Response.Write(pdfDoc)
        Response.End()

Open in new window

0
Comment
Question by:jknj72
2 Comments
 
LVL 12

Accepted Solution

by:
Ramkisan Jagtap earned 500 total points
ID: 41769872
Protected Sub ExportToPDF(sender As Object, e As EventArgs)

    Using sw As New StringWriter()

        Using hw As New HtmlTextWriter(sw)

            'To Export all pages

            GridView1.AllowPaging = False

            Me.BindGrid()

 

            GridView1.RenderControl(hw)

            Dim sr As New StringReader(sw.ToString())

            Dim pdfDoc As New Document(PageSize.A2, 10.0F, 10.0F, 10.0F, 0.0F)

            Dim htmlparser As New HTMLWorker(pdfDoc)

            PdfWriter.GetInstance(pdfDoc, Response.OutputStream)

            pdfDoc.Open()

            htmlparser.Parse(sr)

            pdfDoc.Close()

 

            Response.ContentType = "application/pdf"

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

            Response.Cache.SetCacheability(HttpCacheability.NoCache)

            Response.Write(pdfDoc)

            Response.[End]()

        End Using

    End Using

End Sub

Open in new window


Reference:
http://www.aspsnippets.com/Articles/How-to-export-GridView-data-to-PDF-file-in-ASPNet.aspx
0
 

Author Closing Comment

by:jknj72
ID: 41770730
thanks
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question