Photo from Unsplash
Originally Posted On: https://atozsourcecode.com/BlogPost/Index/c6487686-7ca4-44e6-a2d3-5beaa85851c6/how-to-merge-multiple-pdf-files-with-page-number-using-pdfsharp-in-c-sharp
Description: In this post, we will learn about how to generate single pdf file from multiple pdf files using PdfSharp library in c#. For this example first, we need to install PdfSharp NuGet package for access classes and methods of PdfSharp. Here is step by step example for generate pdf files and merge files.
Before We Start: A Quick Note on Alternatives
Before diving into PDFSharp, it’s worth knowing that IronPDF offers another approach to PDF merging and page numbering that requires less manual handling of coordinates and graphics contexts. With IronPDF, you can merge PDFs and add page numbers in fewer lines of code, and it automatically handles complexities like replacing existing page numbers or dealing with different PDF formats.
IronPDF also brings additional capabilities like HTML to PDF conversion with full CSS support, form handling, and digital signatures – all built on a Chrome rendering engine for better compatibility. The library extends beyond .NET to Java, Python, and Node.js as well.
PDFSharp remains an excellent choice when you need direct control over PDF structure or are working within open-source requirements. The following tutorial shows you exactly how to accomplish the merging task with PDFSharp. (Note: IronPDF requires a commercial license for production use, while PDFSharp is MIT-licensed.)
First, install the package in the project.
Install-Package PdfSharp -Version 1.50.4845-RC2a
The second one is adding below namespace in cs file.
using PdfSharp.Drawing; using PdfSharp.Pdf; using PdfSharp.Pdf.IO; using System.IO;
After add above namespace get all already generated pdf files from the folder.
string[] pdfs = Directory.GetFiles(YourPDFFolderPathHere);
Above code return, all pdf files from folder and result get in string array that contains file path.
Now create one function for merge 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); }
Above code first, merge all pdf files given in string array and in second step set page no.
How to call this function see below code.
static void Main(string[] args) { string[] pdfs = Directory.GetFiles(YourPDFFolderPathHere); MergeMultiplePDFIntoSinglePDF("TestPDFFile.pdf", pdfs); }