2

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

When I open the PDF in a notepad below is the following I see:

%PDF-1.4
%âãÏÓ


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>
    Untitled Page
</title></head>
<body>
    <form name="form1" method="post" action="ShowPDF.aspx" id="form1">
<div>
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUJNzgzNDMwNTMzZGQpkGvp7bYOLIE6xshhcZ2kYd2baA==" />
</div>

    <div>

    </div>
    </form>
</body>
</html>
acadia
  • 2,619
  • 18
  • 55
  • 72

3 Answers3

8

Your code is continuing to render your aspx website code in the response because you haven't called Response.End to prevent further processing. Add a call to that method in after you've completed your binary write - your PDF render code will end up as:

    Response.ContentType = "application/pdf"
    Response.AddHeader("Content-Disposition", "Inline")
    Response.BinaryWrite(MemStream.GetBuffer())
    Response.Flush
    Response.End

EDIT: In addition, I think that you may not be creating your PDF document correctly - see for example this question. The important step that I think you might have missed is to close the document by calling doc.Close before you attempt to read from it (that is, immediately after the loop where you add your content).

Community
  • 1
  • 1
Dexter
  • 18,213
  • 4
  • 44
  • 54
  • Dexter, After putting response.end I still get the same error and if I open the PDF in notepad I see the below %PDF-1.4 %粤マモ – acadia Mar 14 '10 at 16:49
  • @acadia - did adding a call to doc.Close fix your problem? – Dexter Mar 16 '10 at 04:06
  • It's better to use CompleteRequest() instead of Response.End() which exists only for Classic ASP compatibility. See these blogs http://stevesmithblog.com/blog/use-httpapplication-completerequest-instead-of-response-end/ or http://blogs.msdn.com/b/tmarq/archive/2009/06/25/correct-use-of-system-web-httpresponse-redirect.aspx – HashName May 03 '11 at 12:50
2

I encountered the same problem. Tried all the suggestions. Did not work until finally tried specifying the content-length and voila!

Hope this reply is help after 1 year.

Add this code:

Response.AddHeader("Content-Length", MemStream.GetBuffer().Length)

caocao
  • 21
  • 1
  • Works perfectly.Thanks. My issue was that I was using HttpContext.Current.ApplicationInstance.CompleteRequest() instead of Response.End(). Setting the Content-Length header fixes the issue. – HashName May 03 '11 at 12:47
0

You might try setting the content-length header equal to the size of the MemoryStream. In addition, try calling Response.Flush after your call to Response.BinaryWrite.

Thomas
  • 63,911
  • 12
  • 95
  • 141