/PageLabels number tree is replaced with the supplied rangesfunction SaveAsPageLabelled(Stream: TStream;
const Ranges: TPdfPageLabelRanges;
ReplaceExisting: Boolean;
out Report: TPdfPageLabelReport): Boolean; overload;
function SaveAsPageLabelled(const FileName: string;
const Ranges: TPdfPageLabelRanges;
ReplaceExisting: Boolean;
out Report: TPdfPageLabelReport): Boolean; overload;
SaveAsPageLabelled writes a complete PDF copy where the
document catalogue carries a fresh /PageLabels number
tree built from the supplied ranges. The numbering style, prefix
text string and start number of each range follow ISO 32000-1
§12.4.2
Internally PDFium first saves the document to a memory stream;
the saved bytes are then rewritten to inject (or replace) the
/PageLabels entry in the catalogue and append a new
cross-reference section. The visible page content is unchanged;
only the page label metadata is rewritten
Existing /PageLabels entries are detected via a
dictionary scan. When ReplaceExisting=False the call
fails with a descriptive error rather than silently overwriting
prior labels. When ReplaceExisting=True the prior
/PageLabels entry is stripped before the new ranges
are injected, so the resulting catalogue has exactly one
/PageLabels key
Ranges — one or more TPdfPageLabelRange records. Ranges are sorted by StartPageIndex before emission so the number tree is always well-formedReplaceExisting — False rejects the call when the catalogue already has /PageLabels; True strips and replaces itReport — receives input/output byte counts, applied range count, whether prior labels were replaced, and any error messageTPdfPageLabelStyle values map to PDF /S keys: plsDecimal=/D, plsUppercaseRoman=/R, plsLowercaseRoman=/r, plsUppercaseLetters=/A, plsLowercaseLetters=/a, plsNone emits prefix-only labelsPrefix is encoded as a PDF literal string. ASCII characters round-trip cleanly; (, ), \ and whitespace are escaped. Non-ASCII code points above U+007F fall back to low-byte octal escapes; callers needing full UTF-16BE should pre-encode the prefixStartNumber only takes effect when Style is not plsNone. The default PDF value of 1 is omitted from the emitted dictionaryTPdf.PageLabel[] on the reloaded document to verify the applied labels
var
Ranges: TPdfPageLabelRanges;
Report: TPdfPageLabelReport;
begin
SetLength(Ranges, 2);
Ranges[0].StartPageIndex := 0;
Ranges[0].Style := plsDecimal;
Ranges[0].StartNumber := 1;
Ranges[1].StartPageIndex := 3;
Ranges[1].Style := plsLowercaseRoman;
Ranges[1].Prefix := 'Appendix ';
Pdf1.SaveAsPageLabelled('C:\Labelled.pdf', Ranges, False, Report);
end;