Merge PDF files in C#
Combine several PDFs into one document, pages in the order you give.
Short answer
Section titled “Short answer”Open each file with PdfDocument.Open, pass them to PdfDocument.Merge, and save the result.
Saving runs the lossless optimizer, so fonts and images the files share are written once.
#:package PdfNetStack@1.2.0using PdfNetStack;
string[] files = ["a.pdf", "b.pdf", "c.pdf"];PdfDocument merged = PdfDocument.Merge(files.Select(file => PdfDocument.Open(file)));merged.Save("merged.pdf"); // shared fonts and images are written onceConsole.WriteLine($"merged.pdf: {merged.Pages.Count} pages");dotnet add package PdfNetStackVariations
Section titled “Variations”- To insert pages at a given place instead, use
doc.Pages.Import(other.Pages, index). - Annotations travel with their pages. Links to pages imported in the same call are remapped.
- Encrypted inputs open with
PdfDocument.Open(path, password). Pages can be copied out of them only when their permissions allow copying.
Related
Section titled “Related”- Edit pages: import, reorder, rotate and remove pages
- Command line:
pdfnetstack merge out.pdf a.pdf b.pdf c.pdf - API: PdfDocument, PdfPageCollection