Free Support Forum - aspose.com

How to write out pdf to response.outputstream

I'm really having trouble getting my head around Aspose.

My latest problem is trying to output a pdf to an outputstream. I'm sending my documents to a HttpHandler window and trying to load the window with the document.

For TIF's I'm doing:

Pdf pdf1 = new Pdf();

for (int page = 0; page < MyDocument.PAGE_COUNT; page++)

and adding a section for each page and placing an image in that page from the TIF.

Finally, I'm outputting the pdf:

HttpContext.Current.Response.ClearContent();

HttpContext.Current.Response.ClearHeaders();

HttpContext.Current.Response.ContentType = "application/pdf";pdf1.Save(

HttpContext.Current.Response.OutputStream);

HttpContext.Current.ApplicationInstance.CompleteRequest();

However, if my input file is already a PDF I'm confused. I'm trying:

System.IO.MemoryStream ms = new System.IO.MemoryStream();PdfConverter pdfConv = new PdfConverter();

pdfConv.BindPdf(MyDocument.FullImagePath);

pdfConv.DoConvert();

pdfConv.SavaAsTIFF(ms);

Pdf pdf1 = new Pdf(ms);

HttpContext.Current.Response.ClearContent();

HttpContext.Current.Response.ClearHeaders();

HttpContext.Current.Response.ContentType = "application/pdf";pdf1.Save(

HttpContext.Current.Response.OutputStream);

HttpContext.Current.ApplicationInstance.CompleteRequest();

but I'm getting an error on the pdf1.Save - something about being in output only mode and I have to close it, not save it.

In any case, the outputting to the Response.OutputStream is very very slow.

Are there better solutions?

Thanks!!!


This message was posted using Aspose.Live 2 Forum

Hi Larry,

If I have understood your requirement properly, you can use the following code snippet to render a PDF file to the browser, and it doesn’t involve Aspose.Pdf.Kit for other component at all:

//create a memory stream
MemoryStream outStream = new MemoryStream();
//get your PDF into the memory stream
//outStream <- Your PDF
// create a byte array that will hold the output pdf
byte[] outBuf = outStream.GetBuffer();

// specify the duration of time before a page,cached on a browser expires
response.Expires = 0;
// Specify the property to buffer the output page
response.Buffer = true;
// Erase any buffered HTML output
response.ClearContent();
//Add a new HTML header and value to the response sent to the client
response.AddHeader("content-disposition","inline; filename=" + "output.pdf");
// Specify the HTTP content type for response as Pdf
response.ContentType = "application/pdf";
// Write specified information of current HTTP output to Byte array
response.BinaryWrite(outBuf);
// close the output stream
outStream.Close();
//end the processing of the current page to ensure that no other HTML content is sent
response.End();
I hope this helps. If it doesn't resolve your issue, or I haven't understood your requirement properly, please do let us know.
Regards,