18

Hei guys I have this byte array i want to convert to pdf and make it available for download. Anybody has any idea how this is done?

here is my Action Controller

public ActionResult DownloadLabTestResult(string labTestResultID)
{
            PdfReader pdfReader = new PdfReader("Xue_Tang.pdf");

            MemoryStream stream = new MemoryStream();
            PdfStamper stamper = new PdfStamper(pdfReader, stream);

            pdfReader.Close();
            stamper.Close();
            stream.Flush();
            stream.Close();
            byte[] pdfByte = stream.ToArray();

            // So i got the byte array of the original pdf at this point. Now how do i convert this
            // byte array to a downloadable pdf? i tried the method below but to no avail.

            MemoryStream ms = new MemoryStream(pdfByte);

            Response.ContentType = "application/pdf";
            Response.AddHeader("content-disposition", "attachment;filename=labtest.pdf");
            Response.Buffer = true;
            Response.Clear();
            Response.OutputStream.Write(ms.GetBuffer(), 0, ms.GetBuffer().Length);
            Response.OutputStream.Flush();
            Response.End();

            return new FileStreamResult(Response.OutputStream, "application/pdf");

 }
tereško
  • 58,060
  • 25
  • 98
  • 150
Ari
  • 271
  • 3
  • 5
  • 15
  • If you have the pdf file on disk you could read it directly with a FileStream. – Albin Sunnanbo Sep 02 '10 at 04:45
  • Even if you need the PdfStamper it looks like you have to many streams, you should not need the "ms" steam, the "stream" stream should be enough, but you should not close it before you use it, you may try "ms.Seek(0, SeekOrigin.Begin);" before you add it to the OutputStream. – Albin Sunnanbo Sep 02 '10 at 04:53
  • oh ya i could always read from the local disk of course but i am just trying to simulate something here where im only provided with the byte array and my job is to convert it to a downloadable file, dont exactly know how to do that hehe =p – Ari Sep 02 '10 at 05:48

1 Answers1

33

I am using similar code with a few differences:

Response.Clear();
MemoryStream ms = new MemoryStream(pdfByte);
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=labtest.pdf");
Response.Buffer = true;
ms.WriteTo(Response.OutputStream);
Response.End();
  1. Call Reponse.Clear() earlier.
  2. Use MemoryStream.WriteTo to write to Response.OutputStream.

Edit: sorry, I didn't see that you are using ASP.NET MVC, the above code is in a WebForms aspx page.

For ASP.NET MVC, couldn't you just do

return new FileStreamResult(ms, "application/pdf");

?

Andreas Paulsson
  • 7,745
  • 3
  • 25
  • 31
  • actually yeah you can do this. i over-complicated things haha. One other question though, is it even possible to convert a byte array (not knowing whether its pdf or doc or txt) into the right format, without specifying whether its pdf txt or doc? im stuck with this now. – Ari Sep 22 '10 at 06:55