Skip to content

Parse a page expression

This operation parses a comma-separated page-expression string (e.g., "1-5,7,9-*,*") into an array of PageRange selections consumed by Extract pages.

Field Value
Operation ID OP-11
Trigger Caller invokes PageRange.Parse(string text)
Inputs Page expression string
Outputs Array of PageRange (possibly empty)
Primary components PdfAPI.Core.PageRange.Parse
Entry point PdfAPI.Core\Extract\PageRange.cs

Steps

  1. Split the expression on commas. (BR-25)
  2. Match each token against ((?<start>\d+)-(?<stop>(\d*|\*)))|(?<page>(\d+|\*)); silently skip non-matching tokens. (BR-25, BR-26)
  3. For a start-stop token, parse both numbers and normalise a stop of zero or less to -1 (open-ended). (BR-27)
  4. For a bare *, add an all-pages range (1 to -1). (BR-28)
  5. For a bare page number, add a single-page range. (BR-29)
  6. Return the accumulated array (possibly empty when nothing matched). (BR-26)

DocumentRange.Create(document, pages) defaults pages to "*", and DocumentRange's private constructor substitutes a default all-pages PageRange when the supplied array is null or empty (BR-30).

Process flow

The following diagram visualizes this operation's control flow.

flowchart TD
    Start(["Parse(text)"]) --> Split["Split on commas"]
    Split --> Loop{For each token}
    Loop --> Match{Matches grammar?}
    Match -->|No| Loop
    Match -->|start-stop| Range["Parse start/stop,<br>normalize stop <= 0 to -1"]
    Match -->|*| All["Add all-pages range<br>(1 to -1)"]
    Match -->|number| Single["Add single-page range"]
    Range --> Loop
    All --> Loop
    Single --> Loop
    Loop -->|Done| Return([Return PageRange array])

Failure and edge-case behavior

  • An entirely unmatched expression yields an empty range array rather than an exception (BR-26).
  • A null or empty page-range array supplied to DocumentRange's constructor defaults to all pages (BR-30).

Sources

  • PdfAPI.Core\Extract\PageRange.cs
  • PdfAPI.Core\Extract\DocumentRange.cs