Page Labeler
PageLabeler assigns page label ranges to a PDF, controlling how page numbers are displayed in PDF viewers. Supports arabic numerals, roman numerals (upper and lower), alphabetic labels (upper and lower), and custom prefixes.
Opening a PDF
Section titled “Opening a PDF”use Phpdftk\Pdf\Toolkit\PageLabeler;
// From file$labeler = PageLabeler::open('report.pdf');
// From string$labeler = PageLabeler::openString($pdfBytes);
// Encrypted PDF$labeler = PageLabeler::open('secured.pdf', password: 'secret');Roman numerals
Section titled “Roman numerals”Set roman numeral labels for a page range (e.g. i, ii, iii, iv for front matter):
$labeler->setRomanNumerals(1, 4); // pages 1-4: i, ii, iii, iv
// Uppercase: I, II, III, IV$labeler->setRomanNumerals(1, 4, uppercase: true);When the range ends before the last page, the labeler automatically sets arabic numbering on the next page (unless you have already configured that page).
Arabic numerals
Section titled “Arabic numerals”// Pages 5 onward: 1, 2, 3, ...$labeler->setArabic(5, startNumber: 1);
// Pages 5-10: 1, 2, 3, 4, 5, 6$labeler->setArabic(5, 10, startNumber: 1);Alphabetic labels
Section titled “Alphabetic labels”$labeler->setAlphabetic(1, 4); // a, b, c, d$labeler->setAlphabetic(1, 4, uppercase: true); // A, B, C, DCustom label ranges
Section titled “Custom label ranges”For full control, use setLabels with a LabelStyle enum:
use Phpdftk\Pdf\Toolkit\Label\LabelStyle;
$labeler->setLabels( startPage: 1, style: LabelStyle::RomanLower, prefix: 'Preface-', startNumber: 1,);// => "Preface-i", "Preface-ii", ...LabelStyle values
Section titled “LabelStyle values”| Enum case | PDF value | Example output |
|---|---|---|
LabelStyle::Arabic | D | 1, 2, 3 |
LabelStyle::RomanLower | r | i, ii, iii |
LabelStyle::RomanUpper | R | I, II, III |
LabelStyle::AlphaLower | a | a, b, c |
LabelStyle::AlphaUpper | A | A, B, C |
Removing labels
Section titled “Removing labels”$labeler->removeLabels();Common pattern: front matter + body
Section titled “Common pattern: front matter + body”PageLabeler::open('report.pdf') ->setRomanNumerals(1, 4) // pages 1-4: i, ii, iii, iv ->setArabic(5, null, 1) // pages 5+: 1, 2, 3, ... ->save('labeled.pdf');Saving
Section titled “Saving”// To file$labeler->save('labeled.pdf');
// To string$bytes = $labeler->toBytes();Document info
Section titled “Document info”$labeler->getPageCount(); // intEscape hatch
Section titled “Escape hatch”$reader = $labeler->getReader(); // PdfReader