View PDF in browser

Hi. I cant seem to find a working example on how to stream a generated pdf to the browser. When I save the pdf to the file system, it works.


Can you guide me to an example where I can see how to send the stream to the browser? I dont want the client to download the pdf as file, but rather just view the pdf directly in the browser.

Thanks in advance

Current code that can save to file:
'Instantiate Pdf instance by calling its empty constructor
Dim pdf1 As Aspose.Pdf.Generator.Pdf = New Aspose.Pdf.Generator.Pdf()
'Add a section into the pdf document
Dim sec As Aspose.Pdf.Generator.Section = pdf1.Sections.Add()

Dim data = GetFile(4273)


’ Create a MemoryStream object from image Byte array
Dim ms As MemoryStream = New MemoryStream(data)
'Create an image object in the section
Dim imageht As Aspose.Pdf.Generator.Image = New Aspose.Pdf.Generator.Image(sec)
'Set the type of image using ImageFileType enumeration
imageht.ImageInfo.ImageFileType = Aspose.Pdf.Generator.ImageFileType.Tiff

’ Specify the image source as MemoryStream
imageht.ImageInfo.ImageStream = ms
'Add image object into the Paragraphs collection of the section
sec.Paragraphs.Add(imageht)

'Save the Pdf
pdf1.Save(“c:\temp\tifftest\tiff1.pdf”)

’ Close the MemoryStream Object
ms.Close()

Hi Jonas,

Thanks for your inquiry. Please check following code snippet to send PDF docuemnt to browser. Hopefully it will serve the purpose.

byte[] pdfBytes = File.ReadAllBytes(“input.pdf”);<o:p></o:p>
<o:p></o:p>
Response.Clear();<o:p></o:p>
//Specify the document type.<o:p></o:p>
Response.ContentType = “application/pdf”;<o:p></o:p>
//Specify how the document is sent to the browser.
Response.AddHeader(“content-disposition”, “attachment; filename=test.pdf”);<o:p></o:p>
//Another option could be:<o:p></o:p>
//Response.AddHeader “content-disposition”,“inline; filename=test.pdf”;<o:p></o:p>
//Get data bytes from the stream and send it to the response.<o:p></o:p>
Response.BinaryWrite(bytes);<o:p></o:p>
Response.End();

Please feel free to contact us for any further assistance.

Best Regards,

Hi there.


I have tried out your solution. It turns out that this produces an error described here: http://helpx.adobe.com/acrobat/kb/pdf-error-1015-11001-update.html

So i tried using Aspose.Pdf.Generator.Pdf(“dummy.pdf”, SaveType.OpenInBrowser, Response) and this works for me. I do not see the above mentioned error.


Hi Jonas,

Yes you are correct. In order to render the PDF document in client browser, you can use an overload of Pdf.Save(…) method which takes HttpResponse object as an argument. Furthermore, you may also take a look over Aspose.Pdf for .NET examples which explain the step on how to render the output to client browser (something similar to Tilal’s comments).