How to Merge or Split PDFs

Chaknith Bin
Chaknith Bin
February 13, 2023
Updated December 10, 2024
Share:

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");

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;

PdfDocument pdf = PdfDocument.FromFile("Merged.pdf");

// New wdith and heights are in millimeters
PdfDocument combinedPages = pdf.CombinePages(250, 250, 2, 2);

combinedPages.SaveAs("combinePages.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;

// 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");

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

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.