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.

First Step:
green arrow pointer



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;

// Two paged PDF
const string html_a =
    @"<p> [PDF_A] </p>
    <p> [PDF_A] 1st Page </p>
    <div style = 'page-break-after: always;' ></div>
    <p> [PDF_A] 2nd Page</p>";

// Two paged PDF
const string html_b =
    @"<p> [PDF_B] </p>
    <p> [PDF_B] 1st Page </p>
    <div style = 'page-break-after: always;' ></div>
    <p> [PDF_B] 2nd Page</p>";

var renderer = new ChromePdfRenderer();

var pdfdoc_a = renderer.RenderHtmlAsPdf(html_a);
var pdfdoc_b = renderer.RenderHtmlAsPdf(html_b);

// Four paged PDF
var merged = PdfDocument.Merge(pdfdoc_a, pdfdoc_b);
merged.SaveAs("Merged.pdf");
Imports IronPdf

' Two paged PDF
Private Const html_a 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>"

' Two paged PDF
Private Const html_b 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>"

Private renderer = New ChromePdfRenderer()

Private pdfdoc_a = renderer.RenderHtmlAsPdf(html_a)
Private pdfdoc_b = renderer.RenderHtmlAsPdf(html_b)

' Four paged PDF
Private merged = PdfDocument.Merge(pdfdoc_a, pdfdoc_b)
merged.SaveAs("Merged.pdf")
$vbLabelText   $csharpLabel

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")
$vbLabelText   $csharpLabel

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;

// We will use the 4-page PDF from the Merge example above:
var pdf = PdfDocument.FromFile("Merged.pdf");

// Takes only the first page into a new PDF
var page1doc = pdf.CopyPage(0);
page1doc.SaveAs("Page1Only.pdf");

// Take the pages 2 & 3 (Note: index starts at 0)
var page23doc = pdf.CopyPages(1, 2);
page23doc.SaveAs("Pages2to3.pdf");
Imports IronPdf

' We will use the 4-page PDF from the Merge example above:
Private pdf = PdfDocument.FromFile("Merged.pdf")

' Takes only the first page into a new PDF
Private page1doc = pdf.CopyPage(0)
page1doc.SaveAs("Page1Only.pdf")

' Take the pages 2 & 3 (Note: index starts at 0)
Dim page23doc = pdf.CopyPages(1, 2)
page23doc.SaveAs("Pages2to3.pdf")
$vbLabelText   $csharpLabel

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

Ready to see what else you can do? Check out our tutorial page here: Organize PDFs

Frequently Asked Questions

How do I merge PDF files in C#?

To merge PDF files in C#, you can use the Merge method provided by IronPDF. First, load or create the PDF documents, apply the Merge method, and save the combined document.

What is the process to split a PDF document in C#?

You can split a PDF document in C# using IronPDF by employing the CopyPage and CopyPages methods. These methods allow you to extract specific pages and save them as separate PDF files.

Can I combine multiple PDF pages into a single page?

Yes, with IronPDF's CombinePages method, you can combine multiple PDF pages into a single page. You will need to specify the dimensions and layout, such as the number of rows and columns.

How can I create a PDF from an HTML string in C#?

You can create a PDF from an HTML string in C# using IronPDF by rendering the HTML string into a PDF document. This is done by loading the HTML content into the IronPDF renderer.

What are the steps to start using a PDF library in a C# project?

To start using a PDF library in a C# project, download IronPDF from a package manager like NuGet, then integrate it into your project. You can create and manipulate PDF documents using IronPDF's methods.

Is there a free trial available for the PDF library?

Yes, IronPDF offers a free trial version. You can download it from the official website or a package manager like NuGet to start experimenting with PDF functionalities.

How do I extract a single page from a PDF in C#?

To extract a single page from a PDF in C#, use IronPDF's CopyPage method to select the desired page and save it as a new PDF file.

What are some practical applications for merging PDFs?

Merging PDFs is useful for combining related documents, such as merging multiple invoices or reports into a single file for easier sharing and organization.

How can I convert a web page to a PDF document?

You can convert a web page to a PDF document using IronPDF by loading the URL into the IronPDF renderer, which then creates a PDF with the content of the web page.

What C# code is needed to split a multi-page PDF into separate files?

To split a multi-page PDF into separate files using IronPDF, utilize the CopyPage or CopyPages methods to extract the pages you need and save them individually.

Chaknith Bin
Software Engineer
Chaknith works on IronXL and IronBarcode. He has deep expertise in C# and .NET, helping improve the software and support customers. His insights from user interactions contribute to better products, documentation, and overall experience.