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.
After you install ItextSharp in your Solution, please check in your references, whether it exists or not.
Create 2 folders under solution, whose names are Images and PDF, which looks, as given below.
Create the UI, as shown below.
Afterwards, upload some image file and click Submit button.
Here, I am saving the entire image files into Images folder.
Afterwards, change your UI, as shown below.
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.
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.
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.
After uploading of images, Images folder looks, as shown below.
After merging images, the Pdf folder looks, as shown below.
Note
You can merge the combination of JPG, JPEG and PNG file also.