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 the 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 store those pdf in a folder and when we click for merging then will retrieve them one by one pdf and merge them into a single pdf which is stored 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 it in a folder and store the path of the pdf in the database.
Create two folders under our solution and give their name “SourcePdfFiles” and “DestPdfFile” which looks like this.
Now, design your user Interface like this.
On Choose file we will upload some Pdf files and when we click on Submit then that pdf will be stored in the “SourcePdfFiles” folder.
protected void btnSubmit_Click(object sender, EventArgs e)
{
HttpPostedFile file = HttpContext.Current.Request.Files[0];
string filename = HttpContext.Current.Server.MapPath("~/SourcePdfFiles/" + flupload.FileName);
file.SaveAs(filename);
}
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 merge all the pdf and convert them into a single pdf which will be stored in “DestPdfFile”.
protected void btnMerge_Click(object sender, EventArgs e)
{
string SourcePdfPath = HttpContext.Current.Server.MapPath("~/SourcePdfFiles/");
string[] filenames = System.IO.Directory.GetFiles(SourcePdfPath);
string outputFileName = "Merge.pdf";
string outputPath = HttpContext.Current.Server.MapPath("~/DestPdfFile/" + outputFileName);
Document doc = new Document();
PdfCopy writer = new PdfCopy(doc, new FileStream(outputPath, FileMode.Create));
if (writer == null)
{
return;
}
doc.Open();
foreach (string filename in filenames)
{
PdfReader reader = new PdfReader(filename);
reader.ConsolidateNamedDestinations();
for (int i = 1; i <= reader.NumberOfPages; i++)
{
PdfImportedPage page = writer.GetImportedPage(reader, i);
writer.AddPage(page);
}
reader.Close();
}
writer.Close();
doc.Close();
}
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 the 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 number of Pdf to merge and convert it into a single Pdf.