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¶
- Short-circuit: when the range list holds exactly one
PageRangeand that rangeIsOriginal(1–*), returndocumentRange.Document.Fileunchanged — no Syncfusion work and no PDF/A upgrade on this path (BR-SFC-024). - Choose conformance:
Pdf_A1BwhenOptions.IsPdfa, otherwiseNone(BR-SFC-011). - Load the source bytes as a
PdfLoadedDocumentand recordtotalPages. - Create the output
PdfDocumentwith the chosen conformance. - For each
PageRange: skip the range entirely when bothStartPageandStopPageexceedtotalPages; otherwise clamp each bound down tototalPages(BR-SFC-025). - When
StopPage >= StartPage(orStopPage == -1, which is first resolved tototalPages), 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 withi - 1forImportPage(BR-SFC-026, BR-SFC-027). - Save the assembled document to a
MemoryStreamand capture the bytes. - When
Options.IsPdfa, reload the saved bytes as aPdfLoadedDocument, setConformance = Pdf_A1B, save again and replace the output — a second PDF/A pass over the already-conformant output (BR-SFC-020). - Return the bytes.
- 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
catchblock, unlike single-document conversion. - A range with both bounds beyond
totalPagescontributes 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.
Related documentation¶
Sources¶
PdfApi.SyncFusionCore\Extract\PdfExtractor.csPdfAPI\Services\Syncfusion\SyncfusionPdfService.cs