+ Reply to Thread
Results 1 to 5 of 5

Thread: convert repeater to pdf

  1. #1
    Barn Enthusiast peebman2000 is on a distinguished road peebman2000's Avatar
    Join Date
    Mar 2008
    Posts
    215
    Rep Power
    4

    convert repeater to pdf

    Good morning everyone its peeb again; working an .net application thats starting to get on my nerves.

    Anyway, I want to be able to take data thats displayed in a repeater and export it to a pdf. I know how to do it for excel and word, but I have a problem with a pdf.

    The code I have opens ADobe acrobat and when I hit the "Open" button I get this error message:
    Acrobat could not open "Filename[1].pdf' because it is either not a supported file type or becasue the file
    has been damaged(for example, it was sent as an email attachment and wasn't correctly decoded).

    to create an Adobe PDF document, go to the source application. Then print the document to Adobe PDF.

    This is my code:
    Code:
     Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
            'Repeater1.Visible = True
            Response.Clear()
            Response.ClearHeaders()
            Response.Charset = ""
            'Response.Cache.SetCacheability(HttpCacheability.NoCache) '
            'Response.AddHeader("content-disposition", "attachment;filename=FileName.xls")
    
            Response.AppendHeader("Content-disposition", "attachment;filename=Filename.pdf")
    
    
            'for export to word
            'Response.Cache.SetCacheability(HttpCacheability.NoCache)
            'Response.AddHeader("content-disposition", "attachment;filename=FileName.doc")
            'Response.ContentType =  "application/vnd.word"
            'Response.ContentType = "application/vnd.ms-excel"
            Response.ContentType = "application/pdf"
            Response.ContentEncoding = System.Text.Encoding.UTF7
            Dim stringWrite = New System.IO.StringWriter()
            Dim htmlWrite As New System.Web.UI.HtmlTextWriter(stringWrite)
    
            'GridView1.HeaderStyle.Font.Bold = True
            'Repeater1.HeaderStyle.Font.Bold = True
    
    
            Repeater1.DataSourceID = SqlDataSource1.ID
            Repeater1.DataBind()
            'Repeater2.DataSourceID = SqlDataSource1.ID
            'Repeater2.DataBind()
    
    
            'GridView2.DataSourceID = SqlDataSource2.ID
            'GridView2.DataBind()
    
            Repeater1.RenderControl(htmlWrite)
            Response.Write(stringWrite.ToString)
            Response.End()
    
           
        End Sub
    
    Any ideas on why i'm getting this message? Am I missing something in my code?

    Thanks for all and any help.

  2. #2
    Barn Enthusiast peebman2000 is on a distinguished road peebman2000's Avatar
    Join Date
    Mar 2008
    Posts
    215
    Rep Power
    4

    reply

    Okay everyone, I thought I found a solution, but it's not working. I found this asp.net forum link:export gridview to pdf - ASP.NET Forums. It mentions a dll called Itextsharp which allows conversions to pdf.

    Well I mirrored the code on the link to mine for a repeater and I get this error:
    Server Error in '/questionaire' Application.
    --------------------------------------------------------------------------------

    The document has no pages.
    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.IO.IOException: The document has no pages.

    Source Error:


    Line 132:
    Line 133: 'clean up ;;
    Line 134: document.Close()
    Line 135: writer.Close()
    Line 136: 'deleate the tempfile
    Has anyone used Itextsharp before? My code is below, does anyone know why i'm getting this message? Or is there another way to export to a pdf?

    Thanks again for all and any help.


    Code:
    Imports System.Data.SqlClient
    Imports System.Web.Configuration
    Imports System.IO
    Imports System.Text
    Imports System.IO.stream
    Imports System.data
    Imports System.Net.Mail
    Imports System.Web.Security
    Imports System.Web.UI
    Imports System.Web.UI.UserControl
    Imports System.Web.UI.WebControls
    Imports System.Web.Management
    Imports System.Diagnostics.Process
    Imports iTextSharp
    Imports iTextSharp.text
    Imports iTextSharp.text.pdf
    Imports iTextSharp.text.html
    Partial Class addrowgridview
        Inherits System.Web.UI.Page
    
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            Repeater1.DataSourceID = SqlDataSource1.ID
            Repeater1.DataBind()
        End Sub
        Dim Total As Integer = 0
    
        Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            'Get the html form reapeater1''
            Dim stringWrite = New System.IO.StringWriter()
            Dim htmlWrite As New System.Web.UI.HtmlTextWriter(stringWrite)
    
    
            Repeater1.RenderControl(htmlWrite)
    
            Dim html As String = "<html><body>" + stringWrite.ToString() + "</body></html>"
    
    
            'set up the response''
            Response.Clear()
           
            Response.ContentType = "application/pdf"
         
    
            'create pdf document''
            Dim document As New iTextSharp.text.Document(PageSize.A4, 80, 50, 30, 65)
            'create pdf writer, output directly to outPutStream''
            Dim writer As iTextSharp.text.pdf.PdfWriter = PdfWriter.GetInstance(document, Response.OutputStream)
            document.Open()
    
            Repeater1.DataSourceID = SqlDataSource1.ID
            Repeater1.DataBind()
            'Create tempfile to hold the html''
            Dim tempfile As String = Path.GetTempFileName()
            Using tempwriter As New System.IO.StreamWriter(tempfile, False)
                tempwriter.Write(html)
            End Using
            'pares the html into the document''
            HtmlParser.Parse(document, tempfile)
    
            'clean up ;;
            document.Close()
            writer.Close()
            'deleate the tempfile
            File.Delete(tempfile)
    
    
            writer = Nothing
            document = Nothing
    
            Response.End()
    
    
    
         
        End Sub
        Public Overrides Sub VerifyRenderingInServerForm(ByVal control As Control)
    
            ' Confirms that an HtmlForm control is rendered for the specified ASP.NET server control at run time.
    
        End Sub
    
        Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
            Dim memory As New MemoryStream
    
            ' Me.Repeater1.WritePdfToResponse()
    
        End Sub
    End Class
    

  3. #3
    Barn Enthusiast peebman2000 is on a distinguished road peebman2000's Avatar
    Join Date
    Mar 2008
    Posts
    215
    Rep Power
    4

    reply

    Hey guys, any ideas on my issue?

  4. #4
    Barn Enthusiast peebman2000 is on a distinguished road peebman2000's Avatar
    Join Date
    Mar 2008
    Posts
    215
    Rep Power
    4

    reply status

    Heys guy I found some of things on this; i found this link:CodeProject: Creating PDF documents in ASP.NET. Free source code and programming help. I used his ShowHello() code as a practice run and it works. If I could just find a way to add the repeater to the doc.add I thought are it would work just as it would with "hello world".

    The ShowHello code workly perfectly with the Itextsharp dll, but can't find any additonal code online that works for repeaters or gridviews.

    Anyway i'm still playing with this one.

  5. #5
    Barn Enthusiast peebman2000 is on a distinguished road peebman2000's Avatar
    Join Date
    Mar 2008
    Posts
    215
    Rep Power
    4

    reply

    Hey everyone, I haven't found any solution to converting a repeater to a pdf. But I have found a different approach to creating pdf's. This code below is probably what I'll use for my .net app. I have 40 questions and the client would like for the questions and answers to be printed off into a pdf format.

    Anyway I saw I had alot of looks, but I guess there's nothing out there to convert a repeater to pdf, so I'll just print the questions off from each textbox.

    Thanks anyway for the views.

    Code:
     Dim doc As Document = New Document
            PdfWriter.GetInstance(doc, New FileStream(Request.PhysicalApplicationPath + "\1.pdf", FileMode.Create))
            doc.Open()
            doc.Add(New Paragraph("Hello World"))
            doc.Add(New Paragraph(TextBox1.Text))
            doc.Add(New Paragraph(TextBox2.Text))
    
    
    
            doc.Close()
            Response.Redirect("~/1.pdf")
    

+ Reply to Thread

Similar Threads

  1. ModalPopup, Server Event, Repeater, Close ModalPopup
    By Shem in forum .NET Development
    Replies: 26
    Last Post: August 15th, 2008, 08:36 AM
  2. adding an event for my btnDelete in repeater
    By Shem in forum .NET Development
    Replies: 13
    Last Post: July 15th, 2008, 10:33 AM
  3. convert problem
    By todd2006 in forum Microsoft Access
    Replies: 2
    Last Post: July 7th, 2008, 03:17 AM
  4. convert problem
    By todd2006 in forum ASP Development
    Replies: 5
    Last Post: June 27th, 2008, 04:47 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

SEO by vBSEO