Merging & Layouts
PDFium allows you to combine multiple documents into one or create “N-up” layouts (multiple pages per sheet) for printing.
Merging Documents
Section titled “Merging Documents”You can import pages from one document into another. This is done using the importPages or importPagesByIndex methods.
Basic Merge (Append All)
Section titled “Basic Merge (Append All)”import { PDFium } from '@scaryterry/pdfium';
using pdfium = await PDFium.init();using destDoc = await pdfium.createDocument(); // Or open an existing oneusing sourceDoc = await pdfium.openDocument(sourceBytes);
// Append all pages from sourceDoc to the end of destDocdestDoc.importPages(sourceDoc);
// Save the resultconst mergedBytes = destDoc.save();Partial Merge (Specific Pages)
Section titled “Partial Merge (Specific Pages)”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 arraydestDoc.importPagesByIndex(sourceDoc, [0, 2, 4], 0);N-Up Layouts (Printing)
Section titled “N-Up Layouts (Printing)”“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.
Creating a 2-Up Document
Section titled “Creating a 2-Up Document”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}Common Layouts
Section titled “Common Layouts”| Layout | Rows | Cols | Orientation |
|---|---|---|---|
| 2-Up (Side-by-side) | 1 | 2 | Landscape |
| 4-Up (Grid) | 2 | 2 | Portrait |
| 6-Up (Handouts) | 3 | 2 | Portrait |
Copying Viewer Preferences
Section titled “Copying Viewer Preferences”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);