Solved

Export to pdf

Posted on 2016-08-23
2
99 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

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
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…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used.

792 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