Introduction
In this article, we will create a an application that will merge two pdf files into one using C# and .NET.
Step 1 - Create a Project
After opening Visual Studio, next, we need to create an ASP.NET MVC project. For doing that, just click File - New - Project.
After choosing a project, a new dialog will pop up. In that, select Console Application and give your project the location and a name. Then, click the "Ok" button.
Then after the Program.cs class will open and you will see the below blank main class.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
-
- namespace MergePDF
- {
- class Program
- {
- static void Main(string[] args)
- {
-
- }
- }
- }
Step 2 - Install the iTextSharp's Nuget Package
Right click on References in Solution explorer and select "Manage Nuget Package". On selecting it, the popup window will open like below.
In this select Browse and write "iTextSharp" in search box.
Then select the iTextSharp Nuget and click on Install.
Step 3 - Create a method for Merge PDF
Copy the below code and paste it in your program.cs file.
- private static void MergePDF(string File1,string File2)
- {
- string[] fileArray = new string[3];
- fileArray[0] = File1;
- fileArray[1] = File2;
-
- PdfReader reader = null;
- Document sourceDocument = null;
- PdfCopy pdfCopyProvider = null;
- PdfImportedPage importedPage;
- string outputPdfPath = @"E:/newFile.pdf";
-
- sourceDocument = new Document();
- pdfCopyProvider = new PdfCopy(sourceDocument, new System.IO.FileStream(outputPdfPath, System.IO.FileMode.Create));
-
-
- sourceDocument.Open();
-
-
-
- for (int f = 0; f < fileArray.Length - 1; f++)
- {
- int pages = TotalPageCount(fileArray[f]);
-
- reader = new PdfReader(fileArray[f]);
-
- for (int i = 1; i <= pages; i++)
- {
- importedPage = pdfCopyProvider.GetImportedPage(reader, i);
- pdfCopyProvider.AddPage(importedPage);
- }
-
- reader.Close();
- }
-
- sourceDocument.Close();
- }
-
- private static int TotalPageCount(string file)
- {
- using (StreamReader sr = new StreamReader(System.IO.File.OpenRead(file)))
- {
- Regex regex = new Regex(@"/Type\s*/Page[^s]");
- MatchCollection matches = regex.Matches(sr.ReadToEnd());
-
- return matches.Count;
- }
- }
In the above code "MergePDF" function works for merging both pdfs and the "TotalPageCount()" function works for counting each pdf's page.
Now we need to call the "MergePDF" function.
There are two parameters in MergePDF function; one is File 1 and the second is File 2, this is the source path of both pdfs.
So we need to pass both parameters.
Step 4 - Call the Merge PDF function
Call the "MergePdf" function from Main class as like below.
- static void Main(string[] args)
- {
- MergePDF(@"E:\PDF1.pdf", @"E:\PDF2.pdf");
- }
I give my pdf source path , you can give the path where you store the pdf.
Now, when you run the project the MergePDF function is called and new pdf is generated in E drive named "NewFile.pdf". You can give a file name and path as a destination path in MergePDF function in "OutputPdfPath".