Solved

Export to pdf

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

910 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now