How To Merge Number Of Images Into a Single PDF

I want to merge 5 numbers of images and after conversion, it will generate a single PDF file, using ItextSharp.

Take a new solution and install text Sharp latest version, using Manage NuGet Package or From Package Manager Console or you can directly add text Sharp. dll in your reference and NuGet Package Screen, as shown below. 

Package Manage

After you install ItextSharp in your Solution, please check in your references, whether it exists or not.

Package Manage

Create 2 folders under solution, whose names are Images and PDF, which looks, as given below.

Package Manage
Create the UI, as shown below.

Package Manage
Afterwards, upload some image file and click Submit button.

Package Manage

Here, I am saving the entire image files into Images folder.

Afterwards, change your UI, as shown below.

Package Manage
Here, I added a new button as “Merge Image 2 Pdf” and when I click on this button, it will retrieve all the images from the Images folder and will convert into a single PDF, which will be saved in Pdf folder.

Package Manage

Package Manage

In the method, shown above, I retrieved all the images from the Images folder and store in an array. Afterwards, I create the outputFolderPath, where I want to store the PDF file. Here, I created the object of PdfWriter and in that object, I am adding the individual images. Finally, it will be saved as PDF. When we upload different images of different height and weight to handle this, I wrote a piece of code, where I set a default height and weight.

Package Manage

I am checking here whether the file exists or not in for loop because if the file does not exist in that folder and if I try to add the file in PDF file, then it will give you an exception.

Package Manage

After uploading of images, Images folder looks, as shown below.

Package Manage

After merging images, the Pdf folder looks, as shown below.



Note
 
You can merge the combination of JPG, JPEG and PNG file also.
Next Recommended Reading Convert HTML to PDF in ASP.NET MVC Razor