The file is damaged and could not be repaired

Posted by acadia on Stack Overflow See other posts from Stack Overflow or by acadia
Published on 2010-03-13T23:36:11Z Indexed on 2010/03/14 0:15 UTC
Read the original article Hit count: 668

Filed under:

Hello Experts, I am trying to display a PDF file in my ASP.net page based on the binary data received from the ASP.net Web service. Below is the code. though I am getting the data from the Web Service for some reason, if I run the below mentioned code on page load I am getting the above mentioned error.

Please help

Response.Buffer = True
Response.ContentType = "application/pdf"
Response.AddHeader("Content-Disposition", "Inline")

Dim ws As New imageGenService.Service1
Dim imagebyte As Byte() = Nothing

imagebyte = ws.generateSamplePDF()

If imagebyte IsNot Nothing Then
    '"attachment; filename=Whatever.pdf"

    Dim MemStream As New System.IO.MemoryStream
    Dim doc As New iTextSharp.text.Document
    Dim reader As iTextSharp.text.pdf.PdfReader
    Dim numberOfPages As Integer
    Dim currentPageNumber As Integer
    Dim writer As iTextSharp.text.pdf.PdfWriter = iTextSharp.text.pdf.PdfWriter.GetInstance(doc, MemStream)
    doc.Open()
    Dim cb As iTextSharp.text.pdf.PdfContentByte = writer.DirectContent
    Dim page As iTextSharp.text.pdf.PdfImportedPage
    Dim rotation As Integer

    reader = New iTextSharp.text.pdf.PdfReader(imagebyte)
    numberOfPages = reader.NumberOfPages
    currentPageNumber = 0

    Do While (currentPageNumber < numberOfPages)
        currentPageNumber += 1
        doc.SetPageSize(PageSize.LETTER)
        doc.NewPage()
        page = writer.GetImportedPage(reader, currentPageNumber)
        rotation = reader.GetPageRotation(currentPageNumber)
        If (rotation = 90) Or (rotation = 270) Then
            cb.AddTemplate(page, 0, -1.0F, 1.0F, 0, 0, reader.GetPageSizeWithRotation(currentPageNumber).Height)
        Else
            cb.AddTemplate(page, 1.0F, 0, 0, 1.0F, 0, 0)
        End If

    Loop

    If MemStream Is Nothing Then
        Response.Write("No Data is available for output")
    Else
        Response.BinaryWrite(MemStream.GetBuffer())
    End If



End If

© Stack Overflow or respective owner

Related posts about ASP.NET