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.
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.
- using PdfSharp.Drawing;
- using PdfSharp.Pdf;
- using PdfSharp.Pdf.IO;
- using System.IO;
After adding the above namespace, get all already generated pdf files from the folder.
- 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.
- private void MergeMultiplePDFIntoSinglePDF(string outputFilePath, string[] pdfFiles) {
- PdfDocument document = new PdfDocument();
- foreach(string pdfFile in pdfFiles) {
- PdfDocument inputPDFDocument = PdfReader.Open(pdfFile, PdfDocumentOpenMode.Import);
- document.Version = inputPDFDocument.Version;
- foreach(PdfPage page in inputPDFDocument.Pages) {
- document.AddPage(page);
- }
- // When document is add in pdf document remove file from folder
- System.IO.File.Delete(pdfFile);
- }
- // Set font for paging
- XFont font = new XFont("Verdana", 9);
- XBrush brush = XBrushes.Black;
- // Create variable that store page count
- string noPages = document.Pages.Count.ToString();
- // Set for loop of document page count and set page number using DrawString function of PdfSharp
- for (int i = 0; i < document.Pages.Count; ++i) {
- PdfPage page = document.Pages[i];
- // Make a layout rectangle.
- XRect layoutRectangle = new XRect(240 /*X*/ , page.Height - font.Height - 10 /*Y*/ , page.Width /*Width*/ , font.Height /*Height*/ );
- using(XGraphics gfx = XGraphics.FromPdfPage(page)) {
- gfx.DrawString("Page " + (i + 1).ToString() + " of " + noPages, font, brush, layoutRectangle, XStringFormats.Center);
- }
- }
- document.Options.CompressContentStreams = true;
- document.Options.NoCompression = false;
- // In the final stage, all documents are merged and save in your output file path.
- document.Save(outputFilePath);
- }
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.
To call this function, you can use the below code.
- static void Main(string[] args) {
- string[] pdfs = Directory.GetFiles(YourPDFFolderPathHere);
- MergeMultiplePDFIntoSinglePDF("TestPDFFile.pdf", pdfs);
- }

Robert CastlesPosted Sep 22, 2020, 4:56 PM
You are amazing! I so appreciate that you provided fully functioning code where even the maintainers have not. You provided all the using statements along with an example call, and you even provided the commands to get the pre-reqs. Thank you, and I just wanted to let you know it was appreciated. I'm looking forward to your NPOI articles!
Viknaraj ManogararajahPosted Jul 21, 2018, 10:48 PM
Nice Article, Thank you for sharing