Site icon Ryadel

Merge multiple GIF, PNG, JPG, TIFF and PDF files into a single PDF file with ASP.NET C# using the iTextSharp library

How to handle multipage TIFF files with ASP.NET C# (GDI+ alternative)

Yesterday I released a small project I was working on to merge multiple image files into a single, multi-page PDF (one image per page). It wasn't too difficult, yet I had to deal with the following issues:

  • Some nasty GDI+ issues when dealing with multi-page TIFF files (read this post for further details on that).
  • Some nasty GDI+ issues when trying to resize/resample each image to make it fit to the container PDF page size.

Yeah, you can easily guess I really don't like GDI+. Luckily enough, I found a great open-source alternative to deal with these issues: I'm talking about the iTextSharp library, freely available through NuGet or SourceForge, which can flawlessly do these kind of tasks.

IMPORTANT: The source code in this article is based upon iTextSharp 5.5.13.1, currently in EOL: the code will not work with the most recent versions (7+ and above). If you are interested in an updated version of this article with iText 7+ support, feel free to write us a request in the comments!

Using it proved to be really simple; here's what I did to fullfill my specific scenario (WARNING! big amount of source code incoming):

This is the code that defines the ByteArrayInfo class, which is used as the input parameter of the above method: as you can easily understand, the main purpose of this class is to feed the MergeIntoPDF method with either the file name and the byte array of each file we want to "merge".

The source code of the MergeIntoPDF method is pretty much self-explanatory. You will notice a wide amount of nested (and non-nested)  using blocks, which often happens when working with GDI+ image types (most of them implement the IDisposable interface, hence we have to manually dispose them) and also some Bitmap-into-Bitmap transformations which could seem rather odd at first: these are nothing less than attempts to properly deal with GDI+, which often throws the generic GDI+ error otherwise (in some evironments): if you are confident you can securely shrink the code feel free to do that... but be wary of the fact that it could break on other machines!

It's worth noting that the main method returns a byte array, which I needed in my specific scenario because I had to put the resulting PDF file into a DB blob column: you can modify the return value to get the MemoryStream, force an IO write somewhere in your hard-drive or anything else that might suit you better.

However, that's it for now: happy converting!

UPDATE: In case you run into a generic GDI+ error (such as "Generic Error Occurred in GDI+" exception or something like this) while dealing with multi-page TIFF files, it might be useful for you to also read this post to better understand how to effectively work around a nasty (yet fairly common) GDI+ nuisance that could cause it under the hood.

 

Exit mobile version