Click or drag to resize

Splitting PDF Documents

The PDF split part of ExpertPdf Anywhere extracts pages from PDF documents into new PDF documents. It is in the ExpertPdf.Anywhere.PDFSplit namespace of the ExpertPdf.Anywhere library and runs on every platform the library supports: Windows, Linux and macOS.

The main class is PDFSplitManager. It is disposable: a using statement, or Close, releases what it holds.

Features
  • Extract a range of pages into a new document.

  • Split a document into a number of documents of the same size, or into documents of a given number of pages up to the end of the document.

  • Read the source document from a file or from memory, and save the new documents to files or keep them in memory.

  • Split password protected documents, opened with their user or owner password.

  • Set the document information (title, subject, author, keywords) and the security (passwords, permissions, RC4 with a 40 or 128 bit key, AES with a 128 or 256 bit key, or AES-256 GCM) of the new documents.

  • Split a document at its top level bookmarks: one document per chapter, named after the bookmark.

  • Extract a list of pages ("1-3,7,10-"), or the odd or even pages.

  • Split a document into documents under a maximum file size.

  • Get the page count of a document.

Installation

The split classes are part of the ExpertPdf.Anywhere package; there is no separate package to install. See Installation for the packages and Requirements for the supported platforms. Splitting is done in managed code: an application that only splits documents does not need a native engine package.

dotnet add package ExpertPdf.Anywhere
Getting started

The page numbers start at 1. A range of pages is given by the number of the first page and the number of pages; when the range goes past the end of the document, the new document ends with the last page.

using ExpertPdf.Anywhere.PDFSplit;

PDFSplitManager splitManager = new PDFSplitManager();
// splitManager.LicenseKey = "your license key";

// pages 2 to 4 into a new document
splitManager.ExtractPagesFromFileToFile("report.pdf", "pages-2-4.pdf", 2, 3);

Console.WriteLine("Pages: " + splitManager.GetPdfPageCount("report.pdf"));
Splitting into several documents

The methods with a repeat count extract that number of documents of the same size; the methods with a "repeat to end" flag go on to the end of the document. A file name given for several documents gets the index of each document: part.pdf becomes part_1.pdf, part_2.pdf and so on. The memory versions return an array of PDFSplitDoc objects: Buffer has the bytes of the document and SaveToFile(String) saves it.

PDFSplitManager splitManager = new PDFSplitManager();

// documents of 1 page up to the end: part_1.pdf, part_2.pdf, ...
splitManager.ExtractPagesFromFileToFile("report.pdf",
    "part.pdf", 1, 1, true);

// two documents of 3 pages, starting with page 1: half_1.pdf and half_2.pdf
splitManager.ExtractPagesFromFileToFile("report.pdf",
    "half.pdf", 1, 3, 2);

// documents of 2 pages up to the end, in memory
byte[] pdfBytes = File.ReadAllBytes("report.pdf");
PDFSplitDoc[] parts = splitManager.ExtractPagesFromMemoryToMemory(pdfBytes, 1, 2,
    true);
foreach (PDFSplitDoc part in parts)
{
    Console.WriteLine("{0} bytes, {1} pages", part.Buffer.Length,
        splitManager.GetPdfPageCount(part.Buffer));
}
parts[0].SaveToFile("first-part.pdf");

Every combination exists: ExtractPagesFromFileToFile, ExtractPagesFromFileToMemory, ExtractPagesFromMemoryToFile and ExtractPagesFromMemoryToMemory, each for one range, a repeat count or "repeat to end".

One document per chapter (bookmarks)

SplitByBookmarksFromFileToFolder(String, String) splits a document at its top level bookmarks: each chapter goes from the page of its bookmark to the page before the next chapter, and is saved in the folder as a file named after the bookmark (the characters that are not allowed in file names become '_', a repeated title gets a number: "Title (2).pdf"). The pages before the first chapter are saved as "Pages 1-N.pdf"; a document without bookmarks gives one file with all its pages. The methods that keep the documents in memory (SplitByBookmarksFromFileToMemory(String), SplitByBookmarksFromMemoryToMemory(Byte)) give the bookmark title in Title and the source page numbers in PageNumbers.

PDFSplitManager splitManager = new PDFSplitManager();

string[] files = splitManager.SplitByBookmarksFromFileToFolder(
    "manual.pdf", "Chapters");

foreach (PDFSplitDoc chapter in splitManager.SplitByBookmarksFromFileToMemory(
    "manual.pdf"))
    Console.WriteLine("{0}: {1} page(s)", chapter.Title, chapter.PageNumbers.Length);
Page lists, odd and even pages

The ExtractPages methods that take a page list string extract the listed pages into one new document, in the order of the list: page numbers start at 1 and are separated by commas, "a-b" is a range (taken backwards when a is greater than b), "a-" goes to the last page and "-b" starts with the first page. The overloads that take a PageSelection extract the odd or the even pages.

PDFSplitManager splitManager = new PDFSplitManager();

splitManager.ExtractPagesFromFileToFile("report.pdf",
    "selected.pdf", "1-3,7,10-");
splitManager.ExtractPagesFromFileToFile("report.pdf",
    "odd.pdf", PageSelection.OddPages);
PDFSplitDoc even = splitManager.ExtractPagesFromFileToMemory(
    "report.pdf", PageSelection.EvenPages);
Split by maximum file size

SplitBySizeFromFileToFile(String, String, Int64) splits a document into documents of consecutive pages that are each at most the given number of bytes - for example to stay under an e-mail attachment limit. Each document takes as many pages as fit, with the security and document information of the new documents included in the size. A page that is larger than the limit on its own makes a document of its own. The files are saved as <name>_1.pdf, <name>_2.pdf..., and the method returns their paths; SplitBySizeFromFileToMemory(String, Int64) keeps them in memory.

PDFSplitManager splitManager = new PDFSplitManager();

string[] parts = splitManager.SplitBySizeFromFileToFile("report.pdf",
    "Part.pdf", 5 * 1024 * 1024);
foreach (string part in parts)
    Console.WriteLine(part);
Document information, security and protected sources

PdfDocumentInfo and PdfSecurityOptions apply to every new document. SourcePdfPassword opens a password protected source document.

PdfDocumentOptions options = new PdfDocumentOptions();
options.PdfCompressionLevel = PDFCompressionLevel.Best;

PDFSplitManager splitManager = new PDFSplitManager(options);

// the password of the source document (its user or owner password)
splitManager.SourcePdfPassword = "source password";

// the new documents: document information, open password, no printing
splitManager.PdfDocumentInfo.Title = "Report - first part";
splitManager.PdfDocumentInfo.AuthorName = "Sample Company";
splitManager.PdfSecurityOptions.UserPassword = "secret";
splitManager.PdfSecurityOptions.OwnerPassword = "owner";
splitManager.PdfSecurityOptions.CanPrint = false;
splitManager.PdfSecurityOptions.KeySize = EncryptionKeySize.EncryptKey256Bit;

splitManager.ExtractPagesFromFileToFile("protected.pdf",
    "first-part.pdf", 1, 3);
Samples

The samples download has these console applications, in C# and VB.NET, for Windows, Linux and macOS:

  • SplitPdfCommandLine - a command line tool: start page, split size, repeat count or repeat to the end, password of the source document; or split at the bookmarks, extract a page list or the odd/even pages, split by size.

  • SplitPdfFeatures - page ranges and chunks from and to files or memory, the page count, document information and security of the new documents, splitting a password protected document, one document per chapter, page lists, odd and even pages, split by size.

They split the 6 page document that comes with them and need no arguments.

PDF Split command line tool

The SplitPdfCommandLine sample is also a ready to use tool. The syntax is:

SplitPdfCommandLine /pdf:source_file [/startpage:number] [/splitsize:pages]
    [/repeatcount:count] [/password:source_password] [/out:file]
SplitPdfCommandLine /pdf:source_file
    {/bookmarks | /pages:list | /odd | /even | /maxsize:KB}
    [/password:source_password] [/out:file]

By default the split starts with the first page, makes documents of 1 page and repeats to the end of the document. /bookmarks saves one document per chapter in the folder named after the out file; /pages, /odd and /even save one document with those pages; /maxsize saves documents of at most that many kilobytes. Example - two documents of 2 pages, from page 3:

SplitPdfCommandLine /pdf:"My Documents/Report.pdf" /startpage:3 /splitsize:2
    /repeatcount:2 /out:Part.pdf
Licensing

Set LicenseKey once when the application starts, or the LicenseKey property before you split documents. Without a license key the library adds an evaluation notice to the pages of the new documents. See Licensing.

See Also