0
votes

I am trying to create a new pdf file file and save it to disk using iTextSharp.

I have bytes[] for pdf content but unable to understand how to use this with iTextSharp.

public bool CreatePdf(string filepath, byte[] content)
{

}

Can anyone help me with some sample code to generate pdf using byte[] in iTextSharp and save it to disk.

I found lots of tutorial on google with adding new paragraph or chunk.. but I want to use byte[].

1
hmmm,, if all you have byte[] of content and if it supposed of pdf file's byte data, then why you need to create pdf through iTextSharp. You can directly write a file using memorystream or File class.Hiren Visavadiya
absolutely over thinking things. File.WriteAllBytes(string path, byte[] bytes) is the way to goDaniel Casserly
I tried but pdf layout doesn't look good. using (var fileStream = new FileStream(filePath, FileMode.Create, FileAccess.ReadWrite)) { fileStream.Write(exportedByte, 0, exportedByte.Length); fileStream.Close(); }Abhash786
You say you have "bytes[] for pdf content" but that doesn't really make sense. If your byte array a PDF? If it is, write it to disk as Daniel said. If it doesn't look right, then you do not have a byte array of a PDF, you have something else. Inspect the byte array, the first four bytes should be 25-50-44-46 and the last (or almost last) bytes should be 25-25-45-4F-46.Chris Haas
Cannot share PDF because of data confidentiality. anyway, issue is resolved now. Thanks everyone for your help/suggestions.Abhash786

1 Answers

0
votes
public bool CreatePdf(string filepath, byte[] content)
{
  PdfReader reader = new PdfReader(new MemoryStream(content));
  PdfStamper stamper = new PdfStamper(reader, new FileStream(filepath, FileMode.Create));
}