Merge multiple GIF, PNG, JPG, TIFF and PDF files into a single PDF file with ASP.NET C# using the iTextSharp library A short, yet useful guide explaining how to convert one or more GIF, PNG, JPG, TIFF and/or PDF files into a single PDF file in ASP.NET C# using iTextSharp

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.

 

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

12 Comments on “Merge multiple GIF, PNG, JPG, TIFF and PDF files into a single PDF file with ASP.NET C# using the iTextSharp library A short, yet useful guide explaining how to convert one or more GIF, PNG, JPG, TIFF and/or PDF files into a single PDF file in ASP.NET C# using iTextSharp

  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
  3. Thank you so much for posting this! I was having trouble trying to merge a bunch of attachments into one pdf, where the attachments could be pdf base64 string or image base64 strings. I think I wasn’t going deep enough with the multiple uses of a pdf writer/reader within the pdfsmart copy. THANKS

  4. Thank you very much for your code! I am interested in an updated version of this article for iText7.

  5. Can we done PPT slides through asp.net with repeater control or HTML control, which contain multiple images. Please suggest .

    1. No idea, definitely not with iTextSharp which is meant to generate PDF files; unfortunately I don’t know any HTML to PPT converter for ASP.NET or C#.

    1. You simply persist the resulting byte[] to a PDF file using File.WriteAllBytes() or something like that, just like the post says.

      “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.”

Leave a Reply

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


The reCAPTCHA verification period has expired. Please reload the page.

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