Solved

Export to pdf

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Log4Net custom Appender not being fired. 5 44
ASP.net VB.net Email  a webpage 3 26
Hovering effect 9 29
Visual Studio editor? 1 16
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

705 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

11 Experts available now in Live!

Get 1:1 Help Now