Reputation: 67
At first : Sorry for my english, i'm French :p
I'm trying to generate a PDF Document with iTextSharp in a VB.NET web application, curently, i could generate it and save in my pc.
My worry is that when I open it with a pdf reader, and then closes the document, it asks me to save the changes. Without make any changes ! I just want to see my PDF, without this save ask.
This is how i generate and save my PDF :
'Déclaration des variables documents'
Dim document = New Document(PageSize.A4, 50, 50, 25, 25)
Dim output = New MemoryStream()
Dim writer = PdfWriter.GetInstance(document, output)
'Ouverture du document - Opening File '
document.Open()
'Ajout du contenu'
'Fermeture du document'
document.Close()
'Génération de l enregistrement'
Response.ContentType = "application/pdf"
Response.AddHeader("Content-Disposition", String.Format("attachment;filename=Receipt-{0}.pdf", btn_test.Text))
Response.BinaryWrite(output.ToArray())
Someone could help me to solve this ?
Upvotes: 0
Views: 487
Reputation: 55427
Whenever you change the Response
stream to something other than what ASP.Net is expecting you need to make sure to tell ASP.Net that you're done. After you calling Response.BinaryWrite(output.ToArray())
make sure that you close the stream so that ASP.Net doesn't continue writing to it.
Response.End()
Also, you might want to throw a Response.Clear()
at the top of the stream.
Upvotes: 1
Reputation: 22457
A substantial amount of random (?) trash appears after the actual end of your PDF. The PDF 1.7 specification allows some fluff:
3.4.4, “File Trailer”
18. Acrobat viewers require only that the %%EOF marker appear somewhere within the last 1024 bytes of the file.
but checking the Before file, I found no less than 5,576 bytes of HTML after the last line
startxref
281974
%%EOF
Apparently, the modern Reader is even more fault-tolerant than Adobe recommends, but to prevent other readers running into problems, it urges you to save it as a correct PDF.
Proof: I removed everything after the last line %%EOF
using a binary editor. The PDF opened correctly, and closed without warning.
As to where this HTML chunk came from, and how to prevent it, you'll have to go through the documentation of your toolchain.
Upvotes: 1