Page selection syntax
Document management, page selection
Description
The extended page-range selectors add strides (1-12/3), exclusions (!2), omitted bounds and reverse r indices on top of the existing numeric, first, last, odd and even forms, while the strict PDFlibPageRanges helper applies TPDFPageRangeOptions limits and reports typed errors. PLCompactPageRangeList then formats a result back to canonical syntax. Use it when a copy, extract or delete operation must fail predictably instead of silently clipping an invalid range.
Selection syntax
The shared page-range parser accepts the following extended selectors in addition to its existing numeric, first, last, odd and even forms
| 1-12/3 | Every third page: 1, 4, 7, 10; descending ranges retain their direction |
|---|---|
| 1-8,!2,!4-6 | Remove the listed pages, keeping 1, 3, 7 and 8 |
| -5,7- | An omitted bound means the first or final available page; a bare dash selects the document |
| r1,r3-r7/2 | r1 means the final page, r2 the preceding page and so on |
Terms are applied from left to right. A leading exclusion starts from the full document; subsequent positive terms can add pages again. Exclusions remove all matching duplicate occurrences
Extended selectors are validated strictly even when an older caller allows invalid characters in legacy numeric input. Zero strides, malformed numbers and empty items are rejected without a partial selection
Strict Pascal helper
Use PDFlibPageRanges to parse with explicit limits and diagnostics before changing a document
Options:= PLDefaultPageRangeOptions(1, PageCount);
if PLTryParsePageRangeList('r1-r9/2,!r5', Options, Pages, Error) then
Compact:= PLCompactPageRangeList(Pages);
TPDFPageRangeOptions carries document bounds, duplicate and sorting policies, and maximum input length, work and output count. Defaults are 65,536 input bytes, 4,000,000 work units and 1,000,000 selected entries
FailInvalidPages=False clips to available pages while preserving stride alignment. Duplicate rejection takes precedence over duplicate retention. Sorting is optional
On failure, Pages is empty and TPDFPageRangeError supplies a code, the one-based token position, the token and a message. A successful empty selection has code zero
Error codes distinguish invalid options (1), syntax (2), unavailable pages (3), duplicate pages (4) and exhausted limits (5)
Compact formatting
PLCompactPageRangeList combines consecutive ascending or descending runs while preserving the exact input order and duplicate occurrences. For example, 1,2,3,3,2,1,7 becomes 1-3,3-1,7; use a duplicate-retaining parser when round-tripping
The strict helper and formatter are native Pascal unit functions; existing public range operations receive extended syntax through the shared parser and keep their own duplicate and ordering policies