Splitting and Merging PDFs with PDFEdit
Requirements: Secure PDF or PDF SDK
Introduction
Reorganizing PDF documents – breaking a large file into smaller pieces, or combining several files into one – is one of the most common document management tasks developers are asked to automate. Whether you are separating a scanned batch of invoices into individual files, extracting a single page from a signed contract, or assembling a set of reports into a single deliverable, the PDFEdit component in Secure PDF and PDF SDK provides a straightforward, page-level API for doing so.
This guide covers how to use PDFEdit to split a document into multiple files and merge multiple documents into one.
Overview
Both splitting and merging are built on the same small set of page-level methods:
- SelectPage selects a page from an open document, populating the SelectedPage property with a PDFPage object.
- NewPage accepts a PDFPage object to be committed to a document.
- AppendPage adds the page in NewPage to the end of the document.
- InsertPage adds the page in NewPage at a specific position in the document.
- RemovePage and RemovePages remove one or more pages from a document.
Splitting PDFs
Splitting is handled by removing the unwanted pages from a copy of the document and saving the rest. Because RemovePages operates on the document currently loaded in a PDFEdit object, the simplest approach is to open the source document once per output file, remove the pages that do not belong in that output, and save.
Splitting a Document into Two Parts
The following example splits a 4-page document into two 2-page documents:
pdfedit.InputFile = "input_4_pages.pdf";
pdfedit.Open();
// Remove the pages after page 1, and save the first part (pages 0-1)
pdfedit.OutputFile = "input_first.pdf";
pdfedit.RemovePages(2, 2);
pdfedit.Close();
// Remove the pages up to and including page 1, and save the second part (pages 2-3)
pdfedit.OutputFile = "input_second.pdf";
pdfedit.Open();
pdfedit.RemovePages(0, 2);
pdfedit.Close();
RemovePages takes a starting index and a count, so RemovePages(2, 2) removes two pages beginning at index 2 (i.e., pages 3 and 4), leaving pages 1 and 2 in the output.
Splitting a Document into One File per Page
The same pattern extends naturally to splitting every page of a document into its own file. Reopen the source document for each page, remove every page except the one you want to keep, and save it under a unique name:
int pageCount = 0;
pdfedit.OnDocumentInfo += (s, e) =>
{
pageCount = e.PageCount;
};
pdfedit.InputFile = "input.pdf";
pdfedit.Open();
for (int i = 0; i < pageCount; i++)
{
pdfedit.Reset();
pdfedit.InputFile = "input.pdf";
pdfedit.OutputFile = $"page_{i + 1}.pdf";
pdfedit.Open();
if (i < pageCount - 1)
{
pdfedit.RemovePages(i + 1, pageCount - (i + 1)); // remove trailing pages
}
if (i > 0)
{
pdfedit.RemovePages(0, i); // remove leading pages
}
pdfedit.Close();
}
Extracting a Specific Page
To pull a single page out of a larger document without discarding the rest, remove every page except the one you want to extract, in two operations – one for the pages before it, and one for the pages after:
pdfedit.InputFile = "report.pdf";
pdfedit.OutputFile = "report_page3.pdf";
pdfedit.Open();
pdfedit.RemovePages(3, pageCount - 3); // remove everything after page 3
pdfedit.RemovePages(0, 2); // remove pages 1-2
pdfedit.Close();
Merging PDFs
Merging works in the opposite direction: rather than removing pages, you copy pages from one or more source documents into a destination document. Since PDFEdit operates on a single open document at a time, merging requires a separate PDFEdit object for each document involved – one for the destination, and one for each source you are pulling pages from.
Merging Two Documents
To copy pages from a source document into a destination document, open both documents, then use SelectPage on the source to populate SelectedPage, assign that page to the destination's NewPage property, and commit it with AppendPage:
dest.InputFile = "recipient.pdf";
dest.Open();
src.InputFile = "donor.pdf";
src.Open();
src.SelectPage(0);
dest.NewPage = src.SelectedPage;
dest.AppendPage();
dest.OutputFile = "merged.pdf";
dest.Close();
src.Close();
AppendPage adds the page to the end of the destination document. If you need the page inserted at a specific position instead (e.g., to interleave pages from two sources), use InsertPage with the target index in place of AppendPage.
Merging Entire Documents
To merge every page of a source document rather than a single page, loop over each page in the source, selecting and appending it in turn:
dest.InputFile = "recipient.pdf";
dest.Open();
src.InputFile = "donor.pdf";
src.Open();
for (int i = 0; i < donorPageCount; i++)
{
src.SelectPage(i);
dest.NewPage = src.SelectedPage;
dest.AppendPage();
}
dest.OutputFile = "merged.pdf";
dest.Close();
src.Close();
Merging More than Two Documents
Because merging is just repeated appending or inserting, combining any number of documents into one is a matter of looping over your list of source files and repeating the copy step for each:
dest.CreateNew();
foreach (string file in new[] { "part1.pdf", "part2.pdf", "part3.pdf" })
{
src.InputFile = file;
src.Open();
for (int i = 0; i < pageCount; i++)
{
src.SelectPage(i);
dest.NewPage = src.SelectedPage;
dest.AppendPage();
}
src.Close();
}
dest.RemovePage(0); // remove the empty page created by CreateNew()
dest.OutputFile = "combined.pdf";
dest.Close();
Creating the destination document with CreateNew instead of opening an existing file lets you build the combined document entirely from the source pages rather than starting from one of them.
We appreciate your feedback. If you have any questions, comments, or suggestions about this article please contact our support team at support@nsoftware.com.