Click here to Skip to main content
15,877,857 members
Please Sign up or sign in to vote.
4.17/5 (3 votes)
Dear All,

I need some help in converting a byte array "Which Came From DataBase" to pdf. Could someone give me an example of how in C#? Here is what I have so far,

using System.IO;

string sFile = "c:\testpdf.pdf"; //Path
FileStream fs = File.Create(sFile);
BinaryWriter bw = new BinaryWriter(fs); 

And I have Used This To Convert The Pdf File Into Byte Array:

byte[] picArray= System.IO.File.ReadAllBytes(filePathName);

thanks in advance,
Updated 29-Jun-17 9:15am
[no name] 2-Jul-17 22:25pm    
If you use a Pdf component (, you can simply load byte array to PdfDocument object and save as a Pdf file.

PdfDocument doc = new PdfDocument();

You seem to be trying to write a byte arry from a database into a file: why is this giving you problems? If you have the bytes, just write them:
File.WriteAllBytes(@"C:\testpdf.pdf", myArrayOfBytes);
(You should be aware that this is likely to fail - permissions problems often prevent writes to the root directory of a HDD - try using a subdirectory instead)
Share this answer
moral_2011 30-May-11 5:33am    
Thank You It Is working With Me now
Member 12546186 22-Jul-16 12:15pm    
thanks bro
Deepak_Das 19-Jun-18 1:14am    
Is there a way to pass server url to writeAllBytes method
like http://localhost:17813/QuadraSuiteWeb/PROPAttachments/PropertyBill.pdf. Because when I am trying add this url, an exception is coming "URI formats are not supported."

String _ReportPath = "Reports/PropertyManagement/PROPBillGeneration.rdlc";
PDFReportGenerator _PDFReportGenerator = new PDFReportGenerator(_ReportPath, "PROPBillReportDTO", P.PROPBillReportDTOList, Param);
var FileContent = _PDFReportGenerator.getReportBytes();

if (FileContent.Length > 0)
DefaultResponseDTO _DefaultResponseDTO = serviceUtil.GetServiceProxy<ipropbillgenerationservice>().GenerateBillAndSendMail(ReportDTO, FileContent);
string url = HttpContext.Current.Request.Url.AbsoluteUri.Replace(HttpContext.Current.Request.Url.AbsolutePath, "");
string siteFolder = "/" + HttpContext.Current.Request.FilePath.Split('/')[1];
url = url.Split('?')[0] + siteFolder + @"/PROPAttachments/PropertyBill.pdf";
url = url.Replace("~", "");
url = url.Replace("//", "/");
url = url.Insert(5, "/");
System.IO.File.WriteAllBytes(url, FileContent);
MemoryStream ms = new MemoryStream(pdfBytearray); 
Response.ContentType = "application/pdf"; 
Response.AddHeader("content-disposition", "attachment;filename=labtest.pdf"); 
Response.Buffer = true; 
Share this answer
If it is a byte array, you can write it to disk so it becomes saved as *pdf file.
either, you can write the bytes to the response output stream and user will be prompt to download and save the file.

Response.ContentType = "application/pdf";
Response.AppendHeader("Content-Disposition", "inline;filename=data.pdf");
Response.BufferOutput = true;
byte[] pdf;
Response.AddHeader("Content-Length", response.Length.ToString());

Can you please have a look at this code. I got it from the link given below.[^]

Hope this will help you.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900