Save a PDF to a byte array using PDF Sharp/MigraDoc

9 03 2010

After looking at my blog stats since putting up my post about combining 2 PDF’s using PDF sharp, I see that a lot of people are coming here when searching for the above. So although it is in my previous post amongst the combining code, I thought I’d post another little code snippet specifically for saving the PDF to a byte array using MigraDoc/PDFSharp.

So the following code assumes I have created a Document object called pdfDoc and formatted it and populated it as required.

byte[] fileContents = null;
using(MemoryStream stream = new MemoryStream())
{
pdfDoc.Save(stream, true);
fileContents = stream.ToArray();
}

So I am saving the PDF document to a memory stream, rather than a physical file location. I can then use the ToArray() function on the memory stream to give me my byte array. I personally then save this to my database as a VARBINARY.

So there you go. A PDF saved as a byte array. Enjoy!

Advertisements

Actions

Information

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s




%d bloggers like this: