Creating PDF from byte array directly

Hi, I have created a byte array from a Excel Sheet, i want to convert it to PDF directly from the byte array or stream.


MemoryStream ms = new MemoryStream();
wrkBook.Save(ms, FileFormatType.Default);
ms.Seek(0, SeekOrigin.Begin);
byte[] fileBuffer = new byte[(int)ms.Length];
fileBuffer = ms.ToArray();
Here i want to create PDF from the fileBuffer.

I have used one trick on some previous post as:
MemoryStream memStrm = new MemoryStream(fileToPersist);
FileStream fileStrm = new FileStream(“C:\ABC\Output.pdf”, FileMode.Create, FileAccess.ReadWrite);
memStrm.WriteTo(fileStrm);
fileStrm.Close();
memStrm.Close();
Its creating the PDF but while opening the generated PDF, the pdf is not parsing, saying that its not in a correct format.

If i write the stream in an Excel file and then convert the excel into PDF then the PDF is working fine.
But i dont want to create any temporary file. I simply want to convert the stream into PDF.
Please suggest.

Hi,

I think there is an issue with your code segment. Please try the following sample code, it works fine with latest versions e.g v5.1.4, v5.2.0 etc.

Workbook workbook = new Workbook();
workbook.Worksheets[0].Cells[“A1”].PutValue(“Hello World!”);

MemoryStream ms = new MemoryStream();
workbook.Save(ms, SaveFormat.Pdf);
ms.Seek(0, SeekOrigin.Begin);
byte[] buffer = new byte[ms.Length];
buffer = ms.ToArray();
MessageBox.Show(ms.Length.ToString()); //It provides me the length. -OK
FileStream fs = new FileStream(“e:\test\mygeneratedPDF.pdf”, FileMode.Create);
fs.Write(buffer, 0, buffer.Length);
fs.Close();
ms.Close();


Thank you.