views:

457

answers:

1

Hello Users,

I want to create a pdf file from Binary data. I looked around and found examples using iTextSharp by fetching data from the database.

But almost all of them show how to display in the browser. I want to create a file like pdffromDB.pdf instead of displaying as shown below

doc.Close();
Response.BinaryWrite(MemStream.GetBuffer());
Response.End();
MemStream.Close();

I would really appreciate if you can direct me to an example that will allow me to create a real pdf file.

Thanks

+1  A: 

Assuming your MemStream already contains all the bytes making up a valid PDF file, you should be able to convince the visitor's browser to prompt to save it as a file by adding the following statement before Response.BinaryWrite:

Response.AddHeader("Content-Disposition", "attachment; filename=Whatever.pdf")

As an aside, code after Response.End generally isn't executed: your MemStream will still be closed and disposed of just fine in this case due to going out of scope, but in general you should treat Response.End the same as Exit Sub, and code accordingly, e.g.

Using ms As New IO.MemoryStream
    ...
    Response.End()
End Using
mdb