Merge PDFs In C#

In this article, we will learn how to merge PDF files in ASP.NET MVC and C#.

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.
 
Merge PDF
 
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. 

Merge PDF
 
Then after the Program.cs class will open and you will see the below blank main class. 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6.   
  7. namespace MergePDF  
  8. {  
  9.     class Program  
  10.     {  
  11.         static void Main(string[] args)  
  12.         {  
  13.   
  14.         }  
  15.     }  
  16. }  
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.
 
Merge PDF
 
Step 3 - Create a method for Merge PDF
 
Copy the below code and paste it in your program.cs file.
  1.       private static void MergePDF(string File1,string File2)  
  2.        {  
  3.            string[] fileArray = new string[3];  
  4.            fileArray[0] = File1;  
  5.            fileArray[1] = File2;  
  6.   
  7.            PdfReader reader = null;  
  8.            Document sourceDocument = null;  
  9.            PdfCopy pdfCopyProvider = null;  
  10.            PdfImportedPage importedPage;  
  11.            string outputPdfPath = @"E:/newFile.pdf";  
  12.   
  13.            sourceDocument = new Document();  
  14.            pdfCopyProvider = new PdfCopy(sourceDocument, new System.IO.FileStream(outputPdfPath, System.IO.FileMode.Create));  
  15.   
  16.            //output file Open  
  17.            sourceDocument.Open();  
  18.   
  19.   
  20.            //files list wise Loop  
  21.            for (int f = 0; f < fileArray.Length - 1; f++)  
  22.            {  
  23.                int pages = TotalPageCount(fileArray[f]);  
  24.   
  25.                reader = new PdfReader(fileArray[f]);  
  26.                //Add pages in new file  
  27.                for (int i = 1; i <= pages; i++)  
  28.                {  
  29.                    importedPage = pdfCopyProvider.GetImportedPage(reader, i);  
  30.                    pdfCopyProvider.AddPage(importedPage);  
  31.                }  
  32.   
  33.                reader.Close();  
  34.            }  
  35.            //save the output file  
  36.            sourceDocument.Close();  
  37.        }  
  38.   
  39.        private static int TotalPageCount(string file)  
  40.        {  
  41.            using (StreamReader sr = new StreamReader(System.IO.File.OpenRead(file)))  
  42.            {  
  43.                Regex regex = new Regex(@"/Type\s*/Page[^s]");  
  44.                MatchCollection matches = regex.Matches(sr.ReadToEnd());  
  45.   
  46.                return matches.Count;  
  47.            }  
  48.        }  
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.
  1. static void Main(string[] args)  
  2. {  
  3.    MergePDF(@"E:\PDF1.pdf", @"E:\PDF2.pdf");  
  4. }  
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".
 

Output

 
Merge PDF