Click here to Skip to main content
15,888,610 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:

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


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

PdfDocument doc = new PdfDocument();
doc.LoadFromBytes(byteArray);
doc.SaveToFile("output.pdf");

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
 
Comments
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)
{
ReportDTO.PROPBillID = P.PROPBillID;
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);
}
Response.Clear(); 
MemoryStream ms = new MemoryStream(pdfBytearray); 
Response.ContentType = "application/pdf"; 
Response.AddHeader("content-disposition", "attachment;filename=labtest.pdf"); 
Response.Buffer = true; 
ms.WriteTo(Response.OutputStream); 
Response.End(); 
 
Share this answer
 
If it is a byte array, you can write it to disk so it becomes saved as *pdf file.
or
either, you can write the bytes to the response output stream and user will be prompt to download and save the file.

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


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

http://social.msdn.microsoft.com/Forums/en-US/csharplanguage/thread/6810a67f-66d9-4ce4-87e5-06dbbb754730/[^]

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