Skip to content

Merging & Layouts

PDFium allows you to combine multiple documents into one or create “N-up” layouts (multiple pages per sheet) for printing.

You can import pages from one document into another. This is done using the importPages or importPagesByIndex methods.

import { PDFium } from '@scaryterry/pdfium';
using pdfium = await PDFium.init();
using destDoc = await pdfium.createDocument(); // Or open an existing one
using sourceDoc = await pdfium.openDocument(sourceBytes);
// Append all pages from sourceDoc to the end of destDoc
destDoc.importPages(sourceDoc);
// Save the result
const mergedBytes = destDoc.save();

You can specify a page range string (e.g., “1,3,5-7”) or an array of indices.

// Import pages 1, 3, and 5 through 7 (1-based index string)
destDoc.importPages(sourceDoc, {
pageRange: '1,3,5-7',
insertIndex: 0 // Insert at the beginning
});
// OR: Import by zero-based index array
destDoc.importPagesByIndex(sourceDoc, [0, 2, 4], 0);

“N-up” refers to placing multiple pages from a source document onto a single page in a new document. This is commonly used for printing handouts (e.g., 2 slides per page) or booklets.

using sourceDoc = await pdfium.openDocument(pdfBytes);
// Create a new document where every 2 source pages = 1 output page
// The output page size is A4 Landscape (842 x 595 points)
using nUpDoc = sourceDoc.createNUpDocument({
outputWidth: 842,
outputHeight: 595,
pagesPerRow: 2,
pagesPerColumn: 1
});
if (nUpDoc) {
const bytes = nUpDoc.save();
// nUpDoc is a completely new document instance
}
LayoutRowsColsOrientation
2-Up (Side-by-side)12Landscape
4-Up (Grid)22Portrait
6-Up (Handouts)32Portrait

When merging documents, you might want to preserve the viewer preferences (like “Two Page View” or “Print Scaling”) from the source.

// Copy preferences (PrintScaling, Duplex, NumCopies, etc.)
destDoc.copyViewerPreferences(sourceDoc);