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.

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

The code is pretty much self-explanatory: however, it might be useful for you to also read this post to better understand the meaning of the last two methods, which are only used when dealing with multi-page TIFF files (GDI+ alternative).

You will also notice a wide amount of nested (and non-nested)     blocks, which often happens when working with GDI+ image types (most of them implement   , 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  , force a IO write somewhere in your hard-drive or anything else that might suit you better.

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




About Ryan

IT Project Manager, Web Interface Architect and Lead Developer for many high-traffic web sites & services hosted in Italy and Europe. Since 2010 it's also a lead designer for many App and games for Android, iOS and Windows Phone mobile devices for a number of italian companies. Microsoft MVP for Development Technologies since 2018.

View all posts by Ryan

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

  1. Was this written with iTextSharp 5 or 7? I’m having trouble getting all the references working. Can you provide the using statements? Thank you for sharing your code! Very helpful.

  2. Pingback: Merge multiple image files into a single PDF file with ASP.NET C# – Thinking Enterprise Solutions

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.