Merge Multiple Pdfs Into A Single Pdf Using ItextSharp

In this article, I will explain how to merge any number of Pdfs into a single Pdf using ItextSharp.
 
ItextSharp is a .NET PDF library for pdf conversion or merging as we are using it. Take a new solution in Solution Explorer and you can either add itextsharp dll by using NuGet package or by using Package Manager Console or you can directly add dll in References. In the below picture I show you an example of how to install itextsharp using the NuGet package.
 
 
After you installed itextsharp in your solution please check whether it exists or not in your References of Solution Explorer. 
 
 
Now we will upload some of the pdf files and will store those pdf into a folder and when we click for merging then will retrieve one by one pdf and merge into a single pdf which is store in another folder. However, we can store the pdf file in the database as byte[] but the size and complexity will increase. So it’s better to store in a folder and store the path of pdf in the database. 
 
Create two folders under our solution and give their name as “SourcePdfFiles” and “DestPdfFile” and which is looks like this,
 
 
Now, design your user Interface like this,
 
On Choose file we will upload some Pdf file and when we click on Submit then that pdf will be stored in the “SourcePdfFiles” folder.
  1. protected void btnSubmit_Click(object sender, EventArgs e)  
  2.         {  
  3.             HttpPostedFile file = HttpContext.Current.Request.Files[0];  
  4.             string filename = HttpContext.Current.Server.MapPath("~/SourcePdfFiles/" + flupload.FileName);  
  5.             file.SaveAs(filename);  
  6.         }  
In the above code, we will get the pdf file in HttpPostedFile. The filename is nothing but the complete path of the SourcePdfFiles and in the file.SaveAs(filename) we will save the pdf file in the SourcePdfFiles location.
 
After that, change your user Interface like this,
 
When I will click on the Merge button then we will retrieve all the Pdf from the “SourcePdfFiles” folder and will merge all the pdf and convert them into a single pdf which will store in “DestPdfFile”.
  1. protected void btnMerge_Click(object sender, EventArgs e)  
  2.         {  
  3.             string SourcePdfPath = HttpContext.Current.Server.MapPath("~/SourcePdfFiles/");  
  4.             string[] filenames = System.IO.Directory.GetFiles(SourcePdfPath);  
  5.             string outputFileName = "Merge.pdf";  
  6.             string outputPath = HttpContext.Current.Server.MapPath("~/DestPdfFile/" + outputFileName);  
  7.             Document doc = new Document();  
  8.             PdfCopy writer = new PdfCopy(doc, new FileStream(outputPath, FileMode.Create));  
  9.             if(writer==null)  
  10.             {  
  11.                 return;  
  12.             }  
  13.             doc.Open();  
  14.             foreach(string filename in filenames)  
  15.             {  
  16.                 PdfReader reader = new PdfReader(filename);  
  17.                 reader.ConsolidateNamedDestinations();  
  18.                 for(int i=1;i<=reader.NumberOfPages;i++)  
  19.                 {  
  20.                     PdfImportedPage page = writer.GetImportedPage(reader, i);  
  21.                     writer.AddPage(page);  
  22.                 }                 
  23.                 reader.Close();  
  24.             }  
  25.             writer.Close();  
  26.             doc.Close();  
  27.         }  
Code Explanation
 
In the SourcePdfPath we will get the full path of the pdf files. In the filenames array, we will retrieve the entire file's full path. After that, we will create the OutputPath where we will store our final Pdf. In the next line, we will create the Document Object. After that, we will create a writer that listens to the document. We will open the document for reading or writing. In the foreach loop, we create a reader that will read the pdf document one by one, and again in for loop, we read the individual page and add it to the writer. After the entire loop is completed the final pdf contains the total number of pages which is the sum of all the individual pages of all pdf documents.
 
After all the uploading of files, and merging is complete, then our folder looks like this which is shown in the below picture,
 
 
The above picture is related to SourcePdfFiles.
 
 
The above picture is related to DestPdfFiles.
 
Note
This is just a simple application but you can add any numbers of Pdf to merge and convert it into a single Pdf.


Similar Articles