How To Merge Multiple PDF Files With Page Number Using PdfSharp In C#

In this post, we will learn how to generate a single pdf file from multiple pdf files using PdfSharp library in C#.

For this example, first, we need to install PdfSharp NuGet package for accessing classes and methods of PdfSharp.

Here is a step by step example of generating pdf files and merging files.

First, install the package in the project.

Install-Package PdfSharp -Version 1.50.4845-RC2a

The second one is adding the below namespace in cs file.
  1. using PdfSharp.Drawing;  
  2. using PdfSharp.Pdf;  
  3. using PdfSharp.Pdf.IO;  
  4. using System.IO;  
After adding the above namespace, get all already generated pdf files from the folder.
  1. string[] pdfs = Directory.GetFiles(YourPDFFolderPathHere);  
The above code returns all pdf files from the folder and the result gets in string array that contains file path. Now, create one function for merging pdf files.
  1. private void MergeMultiplePDFIntoSinglePDF(string outputFilePath, string[] pdfFiles) {  
  2.     PdfDocument document = new PdfDocument();  
  3.     foreach(string pdfFile in pdfFiles) {  
  4.         PdfDocument inputPDFDocument = PdfReader.Open(pdfFile, PdfDocumentOpenMode.Import);  
  5.         document.Version = inputPDFDocument.Version;  
  6.         foreach(PdfPage page in inputPDFDocument.Pages) {  
  7.             document.AddPage(page);  
  8.         }  
  9.         // When document is add in pdf document remove file from folder  
  10.         System.IO.File.Delete(pdfFile);  
  11.     }  
  12.     // Set font for paging  
  13.     XFont font = new XFont("Verdana", 9);  
  14.     XBrush brush = XBrushes.Black;  
  15.     // Create variable that store page count  
  16.     string noPages = document.Pages.Count.ToString();  
  17.     // Set for loop of document page count and set page number using DrawString function of PdfSharp  
  18.     for (int i = 0; i < document.Pages.Count; ++i) {  
  19.         PdfPage page = document.Pages[i];  
  20.         // Make a layout rectangle.  
  21.         XRect layoutRectangle = new XRect(240 /*X*/ , page.Height - font.Height - 10 /*Y*/ , page.Width /*Width*/ , font.Height /*Height*/ );  
  22.         using(XGraphics gfx = XGraphics.FromPdfPage(page)) {  
  23.             gfx.DrawString("Page " + (i + 1).ToString() + " of " + noPages, font, brush, layoutRectangle, XStringFormats.Center);  
  24.         }  
  25.     }  
  26.     document.Options.CompressContentStreams = true;  
  27.     document.Options.NoCompression = false;  
  28.     // In the final stage, all documents are merged and save in your output file path.  
  29.     document.Save(outputFilePath);  
  30. }  
The above code, first, merges all pdf files given in string array and in the second step, sets page number.

To call this function, you can use the below code.
  1. static void Main(string[] args) {  
  2.     string[] pdfs = Directory.GetFiles(YourPDFFolderPathHere);  
  3.     MergeMultiplePDFIntoSinglePDF("TestPDFFile.pdf", pdfs);  
  4. }