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

What is a library for PDF manipulation in C#, VB, and ASP.NET applications?

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?

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?

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 method to combine multiple PDF pages into a single page?

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 a PDF library for document 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 a PDF library?

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?

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?

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 method to extract a single page from a PDF document?

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.

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.