How to Merge or Split PDFs
Merging multiple PDF files into one can be highly useful in various scenarios. For instance, you can consolidate similar documents such as resumes into a single file instead of sharing multiple files. This article guides you through the process of merging multiple PDF files using C#. IronPDF simplifies PDF splitting and merging with intuitive method calls within your C# application. Below, we'll walk you through all the page manipulation functionalities.
Get started with IronPDF
Start using IronPDF in your project today with a free trial.
How to Merge and Split PDF Pages in C#

- Download IronPDF library for PDF document manipulation
- Load existing or create a PDF from HTML string, file or URL
- Merge two PDF files in C# with
Merge
method - Split a PDF file by pages utilizing the
CopyPage
andCopyPages
methods - Save the PDF document to the desired location
Merge PDFs Example
In the following demonstration, we will initialize two two-paged HTML strings, render them as separate PDFs with IronPDF, and then merge them:
:path=/static-assets/pdf/content-code-examples/how-to/merge-or-split-pdfs-merge.cs
using IronPdf;
// This code renders two separate HTML contents into PDFs and merges them into a single PDF document.
// Define HTML content for a two-page PDF document A
const string htmlA =
@"<p> [PDF_A] </p>
<p> [PDF_A] 1st Page </p>
<div style='page-break-after: always;'></div>
<p> [PDF_A] 2nd Page</p>";
// Define HTML content for a two-page PDF document B
const string htmlB =
@"<p> [PDF_B] </p>
<p> [PDF_B] 1st Page </p>
<div style='page-break-after: always;'></div>
<p> [PDF_B] 2nd Page</p>";
// Create a PDF renderer using IronPdf's ChromePdfRenderer
var renderer = new ChromePdfRenderer();
// Render the HTML content as individual PDF documents
var pdfDocumentA = renderer.RenderHtmlAsPdf(htmlA);
var pdfDocumentB = renderer.RenderHtmlAsPdf(htmlB);
// Merge the two rendered PDF documents into a single PDF document
var mergedDocument = PdfDocument.Merge(pdfDocumentA, pdfDocumentB);
// Save the merged PDF document as "Merged.pdf"
mergedDocument.SaveAs("Merged.pdf");
Imports IronPdf
' This code renders two separate HTML contents into PDFs and merges them into a single PDF document.
' Define HTML content for a two-page PDF document A
Private Const htmlA As String = "<p> [PDF_A] </p>
<p> [PDF_A] 1st Page </p>
<div style='page-break-after: always;'></div>
<p> [PDF_A] 2nd Page</p>"
' Define HTML content for a two-page PDF document B
Private Const htmlB As String = "<p> [PDF_B] </p>
<p> [PDF_B] 1st Page </p>
<div style='page-break-after: always;'></div>
<p> [PDF_B] 2nd Page</p>"
' Create a PDF renderer using IronPdf's ChromePdfRenderer
Private renderer = New ChromePdfRenderer()
' Render the HTML content as individual PDF documents
Private pdfDocumentA = renderer.RenderHtmlAsPdf(htmlA)
Private pdfDocumentB = renderer.RenderHtmlAsPdf(htmlB)
' Merge the two rendered PDF documents into a single PDF document
Private mergedDocument = PdfDocument.Merge(pdfDocumentA, pdfDocumentB)
' Save the merged PDF document as "Merged.pdf"
mergedDocument.SaveAs("Merged.pdf")
Result
This is the file that the code produced:
Combine PDF Pages
Use the CombinePages
method to combine multiple PDF pages into a single page. The method requires the width, height, number of rows, and number of columns.
:path=/static-assets/pdf/content-code-examples/how-to/merge-or-split-pdfs-combine.cs
using IronPdf;
// Load an existing PDF document from a file.
PdfDocument pdf = PdfDocument.FromFile("Merged.pdf");
// Combine pages of the loaded PDF into a grid with specified dimensions.
// The parameters for CombinePages are the width and height of each page
// in millimeters followed by the number of rows and columns to create the grid.
int pageWidth = 250; // Width of each page in the grid
int pageHeight = 250; // Height of each page in the grid
int rows = 2; // Number of rows in the grid
int columns = 2; // Number of columns in the grid
// Combine the pages of the PDF document into a single page with specified dimensions.
PdfDocument combinedPages = pdf.CombinePages(pageWidth, pageHeight, rows, columns);
// Save the combined document as a new PDF file.
combinedPages.SaveAs("combinedPages.pdf");
Imports IronPdf
' Load an existing PDF document from a file.
Private pdf As PdfDocument = PdfDocument.FromFile("Merged.pdf")
' Combine pages of the loaded PDF into a grid with specified dimensions.
' The parameters for CombinePages are the width and height of each page
' in millimeters followed by the number of rows and columns to create the grid.
Private pageWidth As Integer = 250 ' Width of each page in the grid
Private pageHeight As Integer = 250 ' Height of each page in the grid
Private rows As Integer = 2 ' Number of rows in the grid
Private columns As Integer = 2 ' Number of columns in the grid
' Combine the pages of the PDF document into a single page with specified dimensions.
Private combinedPages As PdfDocument = pdf.CombinePages(pageWidth, pageHeight, rows, columns)
' Save the combined document as a new PDF file.
combinedPages.SaveAs("combinedPages.pdf")
Result
Split PDF Example
In the following demonstration, we will split the multi-page PDF document from the previous example.
:path=/static-assets/pdf/content-code-examples/how-to/merge-or-split-pdfs-split.cs
using IronPdf;
// Example usage of the IronPdf library to manipulate PDF pages
// Load a PDF document from a file named "Merged.pdf"
var pdf = PdfDocument.FromFile("Merged.pdf");
// Create a new PDF document to hold the extracted page 1
var page1doc = new PdfDocument();
// Copy the first page (index 0) from the original PDF to the new document
// Assumes the existence of a method `CopyPage` with proper parameters
page1doc.CopyPage(pdf, 0);
// Save the new PDF document containing only the first page
page1doc.SaveAs("Page1Only.pdf");
// Create a new PDF document to hold the extracted pages 2 and 3
var page23doc = new PdfDocument();
// Copy pages 2 and 3 (with indices 1 and 2) from the original PDF to the new document
// Assumes the existence of a method `CopyPages` that accepts an array of indices
page23doc.CopyPages(pdf, new[] { 1, 2 });
// Save the new PDF document containing pages 2 and 3
page23doc.SaveAs("Pages2to3.pdf");
// Note: Ensure that the IronPdf library is properly referenced in your project for this code to function correctly.
// The corrected code now uses the proper method signatures for copying pages in IronPdf.
Imports IronPdf
' Example usage of the IronPdf library to manipulate PDF pages
' Load a PDF document from a file named "Merged.pdf"
Private pdf = PdfDocument.FromFile("Merged.pdf")
' Create a new PDF document to hold the extracted page 1
Private page1doc = New PdfDocument()
' Copy the first page (index 0) from the original PDF to the new document
' Assumes the existence of a method `CopyPage` with proper parameters
page1doc.CopyPage(pdf, 0)
' Save the new PDF document containing only the first page
page1doc.SaveAs("Page1Only.pdf")
' Create a new PDF document to hold the extracted pages 2 and 3
Dim page23doc = New PdfDocument()
' Copy pages 2 and 3 (with indices 1 and 2) from the original PDF to the new document
' Assumes the existence of a method `CopyPages` that accepts an array of indices
page23doc.CopyPages(pdf, { 1, 2 })
' Save the new PDF document containing pages 2 and 3
page23doc.SaveAs("Pages2to3.pdf")
' Note: Ensure that the IronPdf library is properly referenced in your project for this code to function correctly.
' The corrected code now uses the proper method signatures for copying pages in IronPdf.
So this code saves two files:
- Page1Only.pdf (Only the first page)
- Pages2to3.pdf (Second to Third page)
Results
These are the two files produced:
Page1Only.pdf
Pages2to3.pdf
Frequently Asked Questions
What is IronPDF?
IronPDF is a library that allows developers to manipulate PDF documents in C#, VB, and ASP.NET applications. It provides functionalities to merge and split PDFs, among other features.
How can I merge PDF files using IronPDF?
To merge PDF files using IronPDF, you can use the 'Merge' method. First, create or load the PDF documents, then call the 'Merge' method on them, and finally save the merged document to a desired location.
Can I split PDF files using IronPDF?
Yes, you can split PDF files using IronPDF. You can utilize methods like 'CopyPage' or 'CopyPages' to extract specific pages from a PDF document and create new PDFs with those pages.
What is the 'CombinePages' method in IronPDF?
The 'CombinePages' method in IronPDF allows you to combine multiple PDF pages into a single page. You need to specify the width, height, number of rows, and columns for the combined page.
How do I start using IronPDF for PDF manipulation?
First, download the IronPDF library from a package manager like NuGet. Then, you can start creating or loading PDF documents within your C# application using IronPDF's methods.
Is there a trial version available for IronPDF?
Yes, IronPDF offers a trial version which you can start using by downloading the library from the official website or a package manager like NuGet.
What are some practical uses for merging PDFs?
Merging PDFs can be useful for consolidating similar documents, such as combining multiple resumes or reports into a single file for easier sharing and organization.
What C# code is used to merge PDFs with IronPDF?
To merge PDFs in C#, you reference the IronPDF library, create PDF documents using HTML strings or files, and then use the 'Merge' method to combine them. Finally, save the merged document using the 'SaveAs' method.
Can I create a new PDF from a URL using IronPDF?
Yes, you can create a new PDF from a URL using IronPDF. By loading the URL into the IronPDF renderer, you can convert the web page content into a PDF document.
What is the 'CopyPage' method in IronPDF?
The 'CopyPage' method in IronPDF is used to extract a single page from a PDF document and save it as a new PDF file. This is useful for splitting PDFs into separate pages.