convert byte array to pdf file with asp.net

Posted by Arefin under ASP.NET on 6/17/2012 | Points: 10 | Views : 4981 | Status : [Member] | Replies : 1
I have a byte array and now I want to convert the byte array to a pdf file. I am using the following code, but when I try to see/open the pdf file, it shows

"Adobe reader could not open 'sample.pdf' becauase it is either not a supported file type or because the fiel has been damaged (for example, it was sent as an email attachment and wasn't correctly decoded)"

my code that i am using to byte[] to pdf convert.

pdfByte is a byte array.

MemoryStream m = new MemoryStream(pdfByte);
Response.Clear();
Response.AddHeader("Content-Length", pdfByte.Length.ToString());
Response.ContentType = "application/pdf";
Response.AddHeader("Expires", "0");
Response.AddHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
Response.AddHeader("Pragma", "public");
Response.AddHeader("Content-Disposition", "attachment; filename=sample.pdf");
Response.BinaryWrite(pdfByte);
Response.Flush();
Response.End();


I have also tried with the following code:

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();


but in both case, I see the above message when I try to open the pdf file that just created. so,need to find out the issue why this happens? Please help.

Thanks, Arefin

Thanks,
Arefin



Responses

Posted by: Dineshkumarwin on: 6/19/2012 [Member] Starter | Points: 25

Up
0
Down
Is that byte array a PDF file's array ?

if it is not a PDF file try PDFSharp.dll because PDF file structure is different from other file formats.

The PDFSharp.dll is free one (http://www.pdfsharp.net/ )



Arefin, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response