Add Pages
This guide covers adding and managing pages when creating PDF documents from scratch using PDFiumDocumentBuilder.
Prerequisites
Section titled “Prerequisites”- Familiarity with Create Document guide
Creating Pages
Section titled “Creating Pages”Use addPage() on PDFiumDocumentBuilder to create new pages:
using pdfium = await PDFium.init();using builder = pdfium.createDocument();
// Add a page with default dimensions (US Letter)const page = builder.addPage();Page Dimensions
Section titled “Page Dimensions”By default, pages are created with US Letter dimensions (612 × 792 points = 8.5 × 11 inches).
// Custom dimensionsconst page = builder.addPage({ width: 595, // A4 width in points height: 842, // A4 height in points});Common Page Sizes
Section titled “Common Page Sizes”| Size | Width (points) | Height (points) | Inches |
|---|---|---|---|
| US Letter | 612 | 792 | 8.5 × 11 |
| US Legal | 612 | 1008 | 8.5 × 14 |
| A4 | 595 | 842 | 8.27 × 11.69 |
| A5 | 420 | 595 | 5.83 × 8.27 |
| A3 | 842 | 1191 | 11.69 × 16.54 |
Landscape Orientation
Section titled “Landscape Orientation”Swap width and height for landscape:
// Landscape US Letterconst page = builder.addPage({ width: 792, height: 612,});Adding Multiple Pages
Section titled “Adding Multiple Pages”Sequential Page Creation
Section titled “Sequential Page Creation”using pdfium = await PDFium.init();using builder = pdfium.createDocument();
const font = builder.loadStandardFont('Helvetica');
// Create 5 pagesfor (let i = 0; i < 5; i++) { const page = builder.addPage(); page.addText(`Page ${i + 1}`, 72, 720, font, 24);}
const pdfBytes = builder.save();Pages with Different Sizes
Section titled “Pages with Different Sizes”using builder = pdfium.createDocument();
// Cover page (US Letter)const cover = builder.addPage({ width: 612, height: 792 });cover.addText('Cover Page', 200, 400, font, 36);
// Content pages (A4)for (let i = 0; i < 10; i++) { const page = builder.addPage({ width: 595, height: 842 }); page.addText(`Chapter ${i + 1}`, 72, 780, font, 18);}
// Appendix (US Legal)const appendix = builder.addPage({ width: 612, height: 1008 });appendix.addText('Appendix', 72, 950, font, 18);Page Count
Section titled “Page Count”Access the current page count:
console.log(`Document has ${builder.pageCount} pages`);Deleting Pages
Section titled “Deleting Pages”Remove pages by index using deletePage():
// Delete the second page (index 1)builder.deletePage(1);Deleting Multiple Pages
Section titled “Deleting Multiple Pages”// Delete pages 3, 5, and 7 (indices 2, 4, 6)// Delete in reverse order to maintain correct indicesconst indicesToDelete = [6, 4, 2];for (const index of indicesToDelete) { builder.deletePage(index);}Common Patterns
Section titled “Common Patterns”Document with Cover and Content
Section titled “Document with Cover and Content”async function createDocumentWithCover( title: string, chapters: string[],) { using pdfium = await PDFium.init(); using builder = pdfium.createDocument();
const font = builder.loadStandardFont('Helvetica'); const boldFont = builder.loadStandardFont('Helvetica-Bold');
// Cover page const cover = builder.addPage(); cover.addText(title, 150, 450, boldFont, 36); cover.addText('Generated Document', 200, 400, font, 14);
// Table of contents const toc = builder.addPage(); toc.addText('Table of Contents', 72, 720, boldFont, 18);
let tocY = 680; chapters.forEach((chapter, i) => { toc.addText(`${i + 1}. ${chapter}`, 90, tocY, font, 12); tocY -= 20; });
// Chapter pages for (const chapter of chapters) { const page = builder.addPage(); page.addText(chapter, 72, 720, boldFont, 18); page.addText('Chapter content goes here...', 72, 680, font, 12); }
return builder.save();}Multi-page Report
Section titled “Multi-page Report”async function createReport(data: ReportData[]) { using pdfium = await PDFium.init(); using builder = pdfium.createDocument();
const font = builder.loadStandardFont('Helvetica'); const boldFont = builder.loadStandardFont('Helvetica-Bold');
const itemsPerPage = 20; const totalPages = Math.ceil(data.length / itemsPerPage);
for (let pageNum = 0; pageNum < totalPages; pageNum++) { const page = builder.addPage();
// Header page.addText('Monthly Report', 72, 750, boldFont, 18); page.addText(`Page ${pageNum + 1} of ${totalPages}`, 480, 750, font, 10);
// Content const startIndex = pageNum * itemsPerPage; const pageData = data.slice(startIndex, startIndex + itemsPerPage);
let y = 700; for (const item of pageData) { page.addText(`${item.name}: ${item.value}`, 72, y, font, 11); y -= 16; }
// Footer page.addText(`Generated: ${new Date().toLocaleDateString()}`, 72, 36, font, 8); }
return builder.save();}Business Card (Small Page)
Section titled “Business Card (Small Page)”async function createBusinessCard() { using pdfium = await PDFium.init(); using builder = pdfium.createDocument();
const font = builder.loadStandardFont('Helvetica'); const boldFont = builder.loadStandardFont('Helvetica-Bold');
// Business card: 3.5" × 2" const page = builder.addPage({ width: 252, // 3.5 * 72 height: 144, // 2 * 72 });
page.addText('Jane Smith', 18, 100, boldFont, 14); page.addText('Software Engineer', 18, 82, font, 10); page.addText('jane@example.com', 18, 50, font, 8); page.addText('+44 20 1234 5678', 18, 38, font, 8);
return builder.save();}Complete Example
Section titled “Complete Example”import { PDFium } from '@scaryterry/pdfium';import { promises as fs } from 'fs';
async function createMultiPageDocument() { using pdfium = await PDFium.init(); using builder = pdfium.createDocument();
const font = builder.loadStandardFont('Helvetica'); const boldFont = builder.loadStandardFont('Helvetica-Bold');
// Page 1: Title page (US Letter) const titlePage = builder.addPage(); titlePage.addRectangle(0, 0, 612, 792, { fill: { r: 30, g: 60, b: 90, a: 255 }, }); titlePage.addText('Annual Report 2024', 150, 450, boldFont, 32);
// Page 2: Executive Summary (US Letter) const summaryPage = builder.addPage(); summaryPage.addText('Executive Summary', 72, 720, boldFont, 18); summaryPage.addText('Key highlights from the year...', 72, 680, font, 12);
// Pages 3-5: Content pages (A4 for international readers) for (let i = 1; i <= 3; i++) { const contentPage = builder.addPage({ width: 595, height: 842 }); contentPage.addText(`Section ${i}`, 72, 780, boldFont, 16); contentPage.addText(`Content for section ${i}...`, 72, 750, font, 11); }
// Page 6: Appendix (US Legal for detailed tables) const appendixPage = builder.addPage({ width: 612, height: 1008 }); appendixPage.addText('Appendix: Detailed Data', 72, 950, boldFont, 18);
console.log(`Created document with ${builder.pageCount} pages`);
const pdfBytes = builder.save(); await fs.writeFile('multi-page-report.pdf', pdfBytes);}
createMultiPageDocument();See Also
Section titled “See Also”- Create Document Guide — Full document creation workflow
- Add Text Guide — Adding text content
- Add Shapes Guide — Drawing rectangles
- Save Document Guide — Saving options
- PDFiumDocumentBuilder — Document builder API