Skip to content

Extract page ranges from a PDF

Builds a new PDF composed of one or more page ranges pulled from a source PDF. Supports ascending and descending ranges, open-ended stop pages, and clamps or skips ranges that fall outside the document. Unlike single-document conversion, failures here are not captured — they propagate.

Field Value
Operation ID OP-SFC-011
Trigger Caller request — SyncfusionPdfService.ExtractPages(request) (PdfAPI\Services\Syncfusion\SyncfusionPdfService.cs:187) — and internally by Merge document ranges into one PDF step 3
Inputs ExtractPagesRequest (DocumentRange with a Document and a list of PageRange, Options). The overload Extract(DocumentRange, PdfFormat) wraps its arguments in a new ExtractPagesRequest with Options = new PdfOptions(outputFormat) and calls this operation.
Outputs byte[] of the assembled PDF
Primary components PdfExtractor.Extract
Entry point PdfApi.SyncFusionCore\Extract\PdfExtractor.cs

Steps

  1. Short-circuit: when the range list holds exactly one PageRange and that range IsOriginal (1–*), return documentRange.Document.File unchanged — no Syncfusion work and no PDF/A upgrade on this path (BR-SFC-024).
  2. Choose conformance: Pdf_A1B when Options.IsPdfa, otherwise None (BR-SFC-011).
  3. Load the source bytes as a PdfLoadedDocument and record totalPages.
  4. Create the output PdfDocument with the chosen conformance.
  5. For each PageRange: skip the range entirely when both StartPage and StopPage exceed totalPages; otherwise clamp each bound down to totalPages (BR-SFC-025).
  6. When StopPage >= StartPage (or StopPage == -1, which is first resolved to totalPages), import pages ascending; otherwise import descending, i.e. a reversed range produces reversed page order. Page numbers are 1-based in the range and converted with i - 1 for ImportPage (BR-SFC-026, BR-SFC-027).
  7. Save the assembled document to a MemoryStream and capture the bytes.
  8. When Options.IsPdfa, reload the saved bytes as a PdfLoadedDocument, set Conformance = Pdf_A1B, save again and replace the output — a second PDF/A pass over the already-conformant output (BR-SFC-020).
  9. Return the bytes.
  10. On failure: any Syncfusion exception propagates directly to the caller. No exception handling wraps this operation (BR-SFC-028).

Process flow

The following diagram visualizes this operation's control flow.

flowchart TD
    Start(["Extract called with<br>ExtractPagesRequest"]) --> SingleFull{"Single original<br>1-to-* range?"}
    SingleFull -->|Yes| ReturnAsIs(["Return source bytes<br>unchanged"])
    SingleFull -->|No| Conf["Choose conformance,<br>load source as<br>PdfLoadedDocument"]
    Conf --> CreateOut[Create output PdfDocument]
    CreateOut --> ForEach{"More PageRanges<br>to process?"}
    ForEach -->|Yes| RangeCheck{"Range entirely<br>beyond totalPages?"}
    RangeCheck -->|Yes| Skip[Skip this range]
    RangeCheck -->|No| Clamp["Clamp bounds to<br>totalPages"]
    Clamp --> DirCheck{"StopPage >= StartPage?"}
    DirCheck -->|Yes| Ascend[Import pages ascending]
    DirCheck -->|No| Descend[Import pages descending]
    Ascend --> ForEach
    Descend --> ForEach
    Skip --> ForEach
    ForEach -->|No| Save[Save to MemoryStream]
    Save --> PdfaCheck{"Options.IsPdfa?"}
    PdfaCheck -->|Yes| Reload["Reload bytes, set<br>Conformance = Pdf_A1B,<br>save again"]
    PdfaCheck -->|No| Return([Return PDF bytes])
    Reload --> Return

Failure and edge-case behavior

  • Any Syncfusion failure during load, import, or save propagates directly to the caller — this operation has no catch block, unlike single-document conversion.
  • A range with both bounds beyond totalPages contributes nothing to the output rather than raising an error.
  • The full-document short-circuit (step 1) bypasses the PDF/A upgrade entirely, even when the caller requested PDF/A.

Sources

  • PdfApi.SyncFusionCore\Extract\PdfExtractor.cs
  • PdfAPI\Services\Syncfusion\SyncfusionPdfService.cs