Skip to footer content
USING IRONPDF

How Do You Merge PDF Files in C# .NET?

.NET PDF Merge Tasks with IronPDF: A Complete C# Guide: Image 1 - Merge PDFs with IronPDF

Combining multiple PDF files into a single document is a common requirement in business applications. Whether you are generating consolidated financial reports, bundling legal contracts for archival, or assembling multi-part invoices, merging PDFs programmatically saves time and eliminates manual steps. IronPDF provides a straightforward API that handles this in just a few lines of C#, with no external tools or command-line utilities required.

This guide covers every practical scenario: two-file merges, multi-file list merges, in-memory processing with MemoryStream, and post-merge operations like password protection and compression.

Get started with a free trial to add PDF merge capabilities to your .NET application today.

How Do You Get Started with IronPDF?

Before writing any merge code, install IronPDF from NuGet. The package targets .NET Framework 4.6.2+ and .NET 5/6/7/8/9/10, so it works in console apps, ASP.NET Core services, Azure Functions, and Windows Forms projects without any additional runtime dependencies.

Open the NuGet Package Manager Console and run:

Install-Package IronPdf
Install-Package IronPdf
SHELL

Or use the .NET CLI:

dotnet add package IronPdf
dotnet add package IronPdf
SHELL

For a step-by-step setup walkthrough including license key application, see the IronPDF installation overview. Once the package is installed and a license key is applied, every example in this guide will run without modification.

How Do You Merge PDF Documents Programmatically?

The PdfDocument.Merge method provides the most direct way to combine two PDF files into a single PDF. Load each source document with PdfDocument.FromFile, pass both objects to Merge, and save the result.

Input Invoice One

.NET PDF Merge Tasks with IronPDF: A Complete C# Guide: Image 2 - Input Invoice One

Input Invoice Two

.NET PDF Merge Tasks with IronPDF: A Complete C# Guide: Image 3 - Input Invoice Two

using IronPdf;

// Load two input PDF files from disk
PdfDocument pdfA = PdfDocument.FromFile("invoice_one.pdf");
PdfDocument pdfB = PdfDocument.FromFile("invoice_two.pdf");

// Merge PDF documents into a single PDF
PdfDocument merged = PdfDocument.Merge(pdfA, pdfB);

// Save the merged document
merged.SaveAs("combined_invoices.pdf");
using IronPdf;

// Load two input PDF files from disk
PdfDocument pdfA = PdfDocument.FromFile("invoice_one.pdf");
PdfDocument pdfB = PdfDocument.FromFile("invoice_two.pdf");

// Merge PDF documents into a single PDF
PdfDocument merged = PdfDocument.Merge(pdfA, pdfB);

// Save the merged document
merged.SaveAs("combined_invoices.pdf");
$vbLabelText   $csharpLabel

Output

.NET PDF Merge Tasks with IronPDF: A Complete C# Guide: Image 4 - Combined PDF documents

The Merge method accepts two PdfDocument instances and returns a new document containing all pages from both sources in order. The resulting file preserves the original formatting, images, fonts, and embedded content from each input. Once you have the merged document, you can save it to disk, return it from a web endpoint, or add headers and footers before delivering it to users.

Note that PdfDocument.FromFile works with both file paths and URI strings, so documents stored in cloud blob storage can be loaded directly by URL. The merge operation is non-destructive: the two source objects remain unchanged after the call.

How Do You Merge Multiple PDF Files at Once?

When you need to combine more than two documents, pass a List<PdfDocument> (or any IEnumerable<PdfDocument>) to the overloaded Merge method. This approach scales from three files to hundreds without changing your code structure.

using IronPdf;

// Build a list of quarterly reports
List<PdfDocument> pdfsToMerge = new()
{
    PdfDocument.FromFile("report_q1.pdf"),
    PdfDocument.FromFile("report_q2.pdf"),
    PdfDocument.FromFile("report_q3.pdf"),
    PdfDocument.FromFile("report_q4.pdf")
};

// Combine into one annual report
PdfDocument merged = PdfDocument.Merge(pdfsToMerge);

// Save the final document
merged.SaveAs("annual_report.pdf");
using IronPdf;

// Build a list of quarterly reports
List<PdfDocument> pdfsToMerge = new()
{
    PdfDocument.FromFile("report_q1.pdf"),
    PdfDocument.FromFile("report_q2.pdf"),
    PdfDocument.FromFile("report_q3.pdf"),
    PdfDocument.FromFile("report_q4.pdf")
};

// Combine into one annual report
PdfDocument merged = PdfDocument.Merge(pdfsToMerge);

// Save the final document
merged.SaveAs("annual_report.pdf");
$vbLabelText   $csharpLabel

Merged PDF Document

.NET PDF Merge Tasks with IronPDF: A Complete C# Guide: Image 5 - Newly merged output PDF file

IronPDF processes pages in list order, so the sequence of documents in your List<PdfDocument> directly controls the page order in the output. If you need to reorder pages after merging, the page management API lets you copy, move, or delete individual pages from any PdfDocument.

This pattern also works with documents generated at runtime. For example, you can convert HTML to PDF for each section of a report and merge the resulting PdfDocument objects without ever writing intermediate files to disk. The same applies to DOCX-to-PDF conversions when your source content originates from Word documents.

How Do You Merge PDF Files Using MemoryStream?

In-memory processing is required for web services and serverless functions where writing to disk is not possible or is too slow. IronPDF supports loading PDF documents from byte arrays and exposes the merged result as a MemoryStream via the Stream property.

using IronPdf;
using System.IO;

// Load PDF content as byte arrays (e.g., from database or HTTP response)
byte[] firstFileBytes = File.ReadAllBytes("contract_part1.pdf");
byte[] secondFileBytes = File.ReadAllBytes("contract_part2.pdf");

// Create PdfDocument objects from byte arrays
PdfDocument doc1 = new PdfDocument(firstFileBytes);
PdfDocument doc2 = new PdfDocument(secondFileBytes);

// Merge and access the result as a stream
PdfDocument merged = PdfDocument.Merge(doc1, doc2);
MemoryStream outputStream = merged.Stream;

// Optionally save to disk as well
merged.SaveAs("merged_contract.pdf");
using IronPdf;
using System.IO;

// Load PDF content as byte arrays (e.g., from database or HTTP response)
byte[] firstFileBytes = File.ReadAllBytes("contract_part1.pdf");
byte[] secondFileBytes = File.ReadAllBytes("contract_part2.pdf");

// Create PdfDocument objects from byte arrays
PdfDocument doc1 = new PdfDocument(firstFileBytes);
PdfDocument doc2 = new PdfDocument(secondFileBytes);

// Merge and access the result as a stream
PdfDocument merged = PdfDocument.Merge(doc1, doc2);
MemoryStream outputStream = merged.Stream;

// Optionally save to disk as well
merged.SaveAs("merged_contract.pdf");
$vbLabelText   $csharpLabel

Merged Contract File

.NET PDF Merge Tasks with IronPDF: A Complete C# Guide: Image 6 - Merged Contract PDF document

The Stream property returns a MemoryStream positioned at the start, ready for writing to an HTTP response or passing to another processing step. In an ASP.NET Core controller, you can return File(outputStream, "application/pdf", "merged.pdf") directly. No temporary files are created, which simplifies cleanup and reduces disk overhead in high-throughput scenarios.

IronPDF accepts string file paths, byte arrays, and streams interchangeably as input sources, so you can mix and match in the same merge call. A document loaded from a database BLOB can be merged with one fetched from a URL without any intermediate conversion step.

How Are Bookmarks and Navigation Preserved in Merged Files?

When you merge PDF files, IronPDF carries forward the bookmark structure from each source document. Readers see the full table of contents from all combined files in the merged output's navigation pane, making it straightforward for users to jump between sections that originated from different source documents. Bookmarks in the PDF specification are stored in the document outline dictionary, and IronPDF reconstructs this structure correctly for all merged sources.

Bookmark preservation happens automatically with no extra configuration. If the source files contain nested bookmarks (chapters and sub-sections), the hierarchy is maintained in the merged output. For merged documents that need a unified table of contents rather than separate per-document bookmark trees, the table of contents API lets you build a custom navigation structure after merging.

If a source document was created without bookmark metadata, the merged output will contain no outline entries for that section. Before delivering a merged file in a compliance or archival context, verify which bookmarks have been carried forward by inspecting the OutlineManager property on the resulting PdfDocument. You can programmatically rename bookmark labels, reorder entries, or inject new items that bridge content from different source documents into a logical hierarchy. For workflows that also require PDF/A conversion for long-term archiving, apply the compliance conversion step after merging rather than to each source individually. Converting the final merged document in one pass is more efficient and avoids re-converting outputs that may need further edits.

After merging, you can also add page numbers to the combined document using the page numbering feature, which is useful when the output spans dozens of pages from multiple sources.

How Do You Apply Password Protection to a Merged PDF?

After merging, the resulting PdfDocument exposes SecuritySettings for applying user and owner passwords, restricting printing, copying, and editing rights.

using IronPdf;

PdfDocument pdfA = PdfDocument.FromFile("part1.pdf");
PdfDocument pdfB = PdfDocument.FromFile("part2.pdf");

PdfDocument merged = PdfDocument.Merge(pdfA, pdfB);

// Require a password to open the document
merged.SecuritySettings.UserPassword = "reader123";

// Owner password controls permissions such as printing and editing
merged.SecuritySettings.OwnerPassword = "admin456";

merged.SaveAs("secured_merged.pdf");
using IronPdf;

PdfDocument pdfA = PdfDocument.FromFile("part1.pdf");
PdfDocument pdfB = PdfDocument.FromFile("part2.pdf");

PdfDocument merged = PdfDocument.Merge(pdfA, pdfB);

// Require a password to open the document
merged.SecuritySettings.UserPassword = "reader123";

// Owner password controls permissions such as printing and editing
merged.SecuritySettings.OwnerPassword = "admin456";

merged.SaveAs("secured_merged.pdf");
$vbLabelText   $csharpLabel

The UserPassword prompts readers before they can view the document, while the OwnerPassword gates permission changes. You can combine password protection with printing restrictions, copy-prevention flags, and digital signatures. For a full walkthrough of permission options, the PDF security tutorial covers every available setting.

If the source documents are themselves password-protected, IronPDF can decrypt encrypted PDFs before merging, provided you have the owner credentials. This means you can merge secured documents from different vendors or systems into a single, re-secured output without manual unlocking steps.

How Do You Reduce the File Size of a Merged PDF?

Merging large documents with many images can produce outputs that are large to store or send. IronPDF's PDF compression API can significantly reduce file size by re-compressing embedded images at a lower quality setting while preserving text sharpness.

using IronPdf;

PdfDocument pdfA = PdfDocument.FromFile("report_section1.pdf");
PdfDocument pdfB = PdfDocument.FromFile("report_section2.pdf");

PdfDocument merged = PdfDocument.Merge(pdfA, pdfB);

// Compress embedded images to reduce output file size
merged.CompressImages(60); // quality 0–100

merged.SaveAs("compressed_merged.pdf");
using IronPdf;

PdfDocument pdfA = PdfDocument.FromFile("report_section1.pdf");
PdfDocument pdfB = PdfDocument.FromFile("report_section2.pdf");

PdfDocument merged = PdfDocument.Merge(pdfA, pdfB);

// Compress embedded images to reduce output file size
merged.CompressImages(60); // quality 0–100

merged.SaveAs("compressed_merged.pdf");
$vbLabelText   $csharpLabel

The integer argument to CompressImages is a quality percentage: values between 50 and 75 typically cut file size by 40-70% for image-heavy documents with no visible degradation to text content. For documents that contain vector graphics and minimal raster images, the savings are smaller but the call is safe to include regardless.

What Are Common Use Cases for PDF Merging?

The PdfDocument.Merge API fits a wide range of document workflows in production .NET applications:

Common PDF merge scenarios and the IronPDF approach for each
Use CaseInput SourcesRecommended Approach
Annual financial reportFour quarterly PDF files on diskList merge with Merge(List<PdfDocument>)
Multi-part legal contractBase contract + amendments from databaseByte array load + in-memory merge
Invoice consolidationHTML-rendered invoices per line itemHTML-to-PDF per item, then list merge
Automated report deliveryMixed DOCX and PDF source filesDOCX-to-PDF conversion, then merge
Secure document archiveMultiple department reportsList merge + SecuritySettings

For workflows where documents need to be selectively extracted before merging, the split PDF API can extract specific page ranges from a source document. You can copy pages between PDF files directly, which is useful when only certain sections of a large document need to appear in the merged output.

IronPDF also supports extracting text and images from merged documents for downstream processing, making it suitable as a central document assembly step in larger data pipelines.

What Are Your Next Steps?

IronPDF's PdfDocument.Merge method covers the full range of .NET PDF merging scenarios with minimal code: two-file merges, multi-file list merges, in-memory processing, password-protected outputs, and post-merge compression. Every approach uses the same consistent API regardless of the input source type.

Start a free trial to run these examples in your own project. For production deployment, view the licensing options that fit your team's size and usage.

After adding merge support, explore related document operations: split a PDF into separate files, add page numbers to multi-page outputs, or apply watermarks to all pages in the merged document.

Get stated with IronPDF now.
green arrow pointer

Frequently Asked Questions

How do you merge two PDF files in C#?

Load each file with PdfDocument.FromFile, then call PdfDocument.Merge(pdfA, pdfB) and save the result with merged.SaveAs.

How do you merge more than two PDF files in .NET?

Create a List<PdfDocument> containing all source documents and pass it to PdfDocument.Merge(list). The output preserves the list order.

Can you merge PDFs in memory without writing to disk?

Yes. Load documents from byte arrays using new PdfDocument(bytes), merge them, and access the result via merged.Stream to avoid any disk writes.

Does IronPDF preserve bookmarks when merging PDFs?

Yes. IronPDF automatically carries forward the bookmark (outline) structure from every source document into the merged output, including nested bookmark hierarchies.

How do you password-protect a merged PDF in C#?

After merging, set merged.SecuritySettings.UserPassword for the reader password and merged.SecuritySettings.OwnerPassword for the permissions password, then call SaveAs.

How do you reduce the file size of a merged PDF?

Call merged.CompressImages(quality) with a quality value between 50 and 75 before saving. This re-compresses embedded raster images and typically reduces file size by 40-70% for image-heavy documents.

Can IronPDF merge encrypted PDF files?

Yes. IronPDF can load and merge encrypted PDFs when you have the owner password. Use PdfDocument.FromFile(path, password) to unlock the file before merging.

Does IronPDF's merge API work in ASP.NET Core?

Yes. The merge result can be returned as File(merged.Stream, "application/pdf", "merged.pdf") from any ASP.NET Core controller action.

Curtis Chau
Technical Writer

Curtis Chau holds a Bachelor’s degree in Computer Science (Carleton University) and specializes in front-end development with expertise in Node.js, TypeScript, JavaScript, and React. Passionate about crafting intuitive and aesthetically pleasing user interfaces, Curtis enjoys working with modern frameworks and creating well-structured, visually appealing manuals.

...

Read More

Iron Support Team

We're online 24 hours, 5 days a week.
Chat
Email
Call Me