DEV Community

Michael Lip
Michael Lip

Posted on • Originally published at zovo.one

Splitting a PDF Is Easier Than Merging One

A PDF file is a collection of pages with a cross-reference table that maps page numbers to byte offsets. Splitting extracts pages by creating a new file with only the selected page references. No re-encoding required.

How splitting works internally

A PDF file has a structure like this:

Header
Body (objects: pages, fonts, images, etc.)
Cross-reference table (maps object numbers to byte offsets)
Trailer (points to the root object and cross-reference table)
Enter fullscreen mode Exit fullscreen mode

Splitting a PDF means:

  1. Reading the cross-reference table
  2. Identifying which objects belong to the desired pages
  3. Copying those objects to a new file
  4. Writing a new cross-reference table and trailer
  5. Handling shared resources (fonts, images used by multiple pages)

The "shared resources" part is the complication. If pages 1 and 5 share an embedded font, splitting out page 5 alone must include that font in the new file.

Common split patterns

Range extraction: Pages 1-5 from a 20-page document. Most common for extracting chapters or sections.

Every N pages: Split a 100-page document into 10-page chunks. Common for batch processing or meeting file size limits.

By bookmark: Split at PDF bookmark boundaries, producing one file per chapter or section.

Remove pages: Create a new PDF excluding specific pages. Effectively a split that removes rather than extracts.

Using pdf-lib in JavaScript

import { PDFDocument } from 'pdf-lib';

async function splitPdf(pdfBytes, startPage, endPage) {
  const srcDoc = await PDFDocument.load(pdfBytes);
  const newDoc = await PDFDocument.create();

  const pageIndices = Array.from(
    { length: endPage - startPage + 1 },
    (_, i) => startPage - 1 + i
  );

  const copiedPages = await newDoc.copyPages(srcDoc, pageIndices);
  copiedPages.forEach(page => newDoc.addPage(page));

  return await newDoc.save();
}
Enter fullscreen mode Exit fullscreen mode

For splitting PDFs by page range or extracting specific pages, I built a splitter at zovo.one/free-tools/pdf-splitter. It processes the file in your browser and handles shared resources correctly.


I'm Michael Lip. I build free developer tools at zovo.one. 500+ tools, all private, all free.

Top comments (0)