Amazon

Friday, 18 August 2017

Single PDF Into Number Of Pages As A PDF Using iTextSharp.

For Example - I will upload a sample PDF which contains 10 pages and after the split, it will show 10 PDF files which will be saved in a folder.

iTextSharp is a .NET PDF Library and for conversion, we need to download it from NuGet package, or from Package Manager Console, or from its website, or directly add it as a reference in our solution.



Note -  Always use the latest version of iTextSharp.

After iTextSharp is installed in your Solution, check whether it exists or not, in your "References".



Create two new folders in your solution which will be used for saving the uploaded documents and spliting the PDF document.



Then, take a new page and design it like the below picture.


First, upload the file and click on Save.

I am saving the uploaded document in a folder called OriginalDoc.



And, in that same method, I am calling another method named as SplitPdf which has two parameters, SourcePath and filenamewithoutExtension.



In the SplitPdf method, I created an object of PdfReader which will read the document from SourcePath. In for loop, I am creating the individual PDF by using Document and PdfCopy and storing those new pdf files into SplitDoc folder.

After the conversion is completed, the OriginalDoc folder looks like this.



And the SplitDoc folder looks like this.



Note
  1. Please add these "using" statements in your .cs file otherwise it will show you an error message.



  2. This is just a sample application and you can modify it as per your requirement.

Merge Image to 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.

Pdf File into Image File

Ghostscript is an interpreter for the PostScript language and for PDF.

Official Website.

First, we need to add Ghostscript in our solution by going to the Package Manager Console or we can add its dll file directly in reference of our Solution.



After you have installed the GhostScript in your application, please check in your reference whether it exists or not.



After that, design your UI like the below image.


And, create two new folders in that Solution with the names - PdfFolder and ImgFolder respectively.


Note
  1. Please install Ghostscript in your computer according to its bit rate, that is32/64 bit. Please refer this link and always install and use the latest version. You might get confused here that if I already have installed the Ghostscript in my Solution, then what is the need of installing it again. The reason behind is that while converting the PDF file to an image file, we need Ghostscript in our computer; and to access the Ghostscript properties, we have to install it in our Visual Studio Solution. 
  2. If we haven't installed the Ghostscript in our computer and are trying to run the application directly, then we can encounter the error which is shown in the below picture,

After that, I click on the Submit button. First, I am storing the original PDF in PdfFolder.



Here, I am calling Pdf2ImageConversion method which contains 2 parameters in which the first parameter is the name of the file and the second parameter is PdfFolder path.



Here, first I am checking whether that image exists in the ImgFolder or not. First, I created the object of GhostscriptPngDevice which will used to convert the Pdf to Image. After that I set the properties for that object.

img.InputFiles.Add(PdfFolderPath);


In the above line I am adding the SourceFile whose extension is “.pdf”.

Img.OutputPath=ImgFolderPath;

In the above line I am adding the OutputFile whose extension is “.png”;

Similarly I added some more properties for that object. Please see above in the picture like resolution etc.

And in the img.Process(); actually the pdf is converting to png image.

After Conversion the PdfFolder looks like this,



And the ImgFolder looks like this,



Note
  1. In the above example, I converted the PDF file into png image file. But, if you want to convert pdf file into jpg/jpeg, then in place of png, please write jpg/jpeg.
  2. If you want to convert PDF file into image file in production server or any other server, then you should install the Ghostscript (32/64 bit) for that server otherwise it will show you an error.
  3. This is just a sample application and you can change or modify anything as per your requirement.