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!


Combining 2 PDF Documents into 1 with PDFSharp

4 03 2010

After looking at the example of how to do this on the PDF sharp website, I wrote the following code to combine 2 existing PDF documents into one & send the new document back to the user to download. Both PDF documents existed as byte arrays (which I had retrieved from my database, reportPdfContents & certsPdfContents) so if you’re retrieving them from your file system, you will need to slightly alter the code.

MemoryStream msReport = new MemoryStream(reportPdfContents); //reportPdfContents is a byte array
MemoryStream msCerts = new MemoryStream(certsPdfContents); //certsPdfContents is a byte array
PdfDocument reports = PdfReader.Open(msReport, PdfDocumentOpenMode.Import);
PdfDocument certificates = PdfReader.Open(msCerts, PdfDocumentOpenMode.Import);
PdfDocument combinedOutput = new PdfDocument();
int repPageCount = reports.PageCount;
for (int i = 0; i < repPageCount; i++)
PdfPage page = reports.Pages[i];
page = combinedOutput.AddPage(page);
int certPageCount = certificates.PageCount;
for (int i = 0; i < certPageCount; i++)
PdfPage page = certificates.Pages[i];
page = combinedOutput.AddPage(page);
byte[] combinedContents = null;
using(MemoryStream stream = new MemoryStream())
combinedOutput.Save(stream, true);
combinedContents = stream.ToArray();
Response.ContentType = "application/pdf"; //set the MIME type here
Response.AddHeader("content-disposition", "attachment; filename=ReportAndCertificates.pdf");

If you are familiar with PDF Sharp and have already looked at the examples, I think this code is probably pretty self explanatory. If you want some more information though, on the PDF’s, saving to/reading from byte arrays or opening a file on the client side just leave a comment. I do check them quite often so I’ll probably reply fairly quickly!

Casting SqlParameter/Object as a Byte array

2 03 2010

I’ve just spent ages working this out, and all the time I was just making a silly mistake! So hopefully this may save someone else the pain! For anyone that doesn’t want to read the rest of the post, the code to do it is:

byte[] fileContents = (byte[])sqlParameters[3].Value;

I was saving a PDF file as a byte array and then inserting that to my database as a varbinary. I needed to be able to then get that varbinary back out of the database as a byte array, and open it without corrupting the information.

So casting is the obvious candidate. But I made a silly mistake and had the following:

byte[] fileContents = (byte)sqlParamters[3].Value;

Which caused a problem as I was trying to cast a single byte to a byte array. I then got all caught up in using the ToString() method on the parameter value and then converting it from a string to a byte array. It just kept getting corrupted!

When I actually sat back and thought about it, a SqlParameter is just an object. So a simple cast is all that’s required. When I took a step back, I realised my code should have been:

byte[] fileContents = (byte[])sqlParameters[3].Value;

See what I mean about it being a stupid mistake?! All I needed was the square brackets in the casting and now it works perfectly, giving me back my lovely PDF, uncorrupted!

Sometime problems aren’t as complicated as you first think and the easy solution is the right one 🙂