Skip to content

fix(document): hinted remote files find pages without walking the page tree (#417) - #419

Merged
michaelstonis merged 3 commits into
spec/33-broadside-v1from
417-hinted-page-tree-walks
Oct 25, 2026
Merged

michaelstonis merged 3 commits into
spec/33-broadside-v1from
417-hinted-page-tree-walks

Conversation

@michaelstonis

@michaelstonis michaelstonis commented Oct 25, 2026 •

Copy link
Copy Markdown
Owner

Closes #417.

Root cause

#410 (PR #413) made PdfPageCollection.IndexOf(CosDictionary) and TryGetPage answer from the hint tables, but only for pages the hints had already handed out (HintedPageList._indexes). A page object reached any other way fell back to Current, a full walk of the page tree. In a linearized file the page tree comes after the pages (F.3.10), so the walk needs the main cross-reference section at the end of the file. Examples: a structure element's /Pg, a link's or outline item's destination page, a widget's page, CreateDestination. In the browser (SynchronousRangeFetch off) the walk throws PdfRangeNotLoadedException. The outline, the structure tree and the name trees also come after the pages, and only a synchronous read could reach them.

Audit (core callers of the full page-tree walk while hints are in use)

Path Before After
Pages.Count linearization N unchanged
foreach Pages hinted page by page (needs each page's bytes) unchanged; the async path is PdfDocument.ReadAsync, and the miss message names it
IndexOf(CosDictionary) / IndexOf(PdfPage) (StructureLayout, AnnotationLayer InvalidateField, CreateDestination, FieldCreation, Remove) tree walk unless handed out hints: page handed out, or loaded page object matched against the hinted object numbers; -1 otherwise, no walk
TryGetPage (structure /Pg in PdfStructureElement, StructureContext.FindPage, MarkedContentIndex, ShownItems) tree walk unless handed out same as IndexOf
PdfExplicitDestination.PageIndex / TargetKind / StructureElement (links, outline items, GoTo actions, GetDestinationViewport) resolved the page, then IndexOf (walk) a page reference is mapped to its index by object number (Table F.4 items 1, 2), without reading the page
Outline, structure tree, named destinations come after the pages: only reachable synchronously PdfDocument.ReadAsync (fetches the main section and the objects when needed)
Whole-document passes (StructureContext.IndexParentTreePages repair, AnnotationIndex owners, PDF/A and PDF/UA checkers, save) enumerate every page unchanged; they need every page and run through ReadAsync

Changes

  • HintedPageList.TryGetIndex(CosReference) maps page object numbers to pages. Find(CosDictionary) finds a page by a loaded page object; the object cache keeps every object, so identity is stable.
  • PdfPageCollection gets HintedIndexOf(CosDictionary) (validated through Get; rejects the hints on failure, as before) and HintedIndexOf(CosReference). IndexOf and TryGetPage use them and never walk the tree while the hints are in use.
  • PdfExplicitDestination answers a page reference from the hints.
  • New public API: PdfDocument.ReadAsync<T>(Func<PdfDocument, T>, CancellationToken). It runs a synchronous read, fetching what it misses asynchronously (HttpRangeSource.RunAsync), and runs once for a local document. This is the async path for anything that needs more than one page. It stays inside the existing prefetch boundary: the same "run synchronous code without blocking, fetch the miss, run it again" mechanism as OpenAsync and PrefetchAsync, made available for any read.
  • PdfRangeNotLoadedException's message and docs point to ReadAsync.
  • Viewer: DocumentSession.ReadAsync runs a remote document's reads through PdfDocument.ReadAsync, in both cooperative and background modes. A tagged remote page's layout (accessible tree, text selection) needs the structure tree, and now loads in the browser.
  • Conformance: G.4 (types, four tests, note) and F.4.2 (note).

Tests

All tests open a remote linearized file with WithSynchronousRangeFetch(false). The new TaggedLinearizedFile is 30 pages, tagged, with links, an outline and a named destination.

  • HintedPageTreeTests.Enumerating_the_pages_goes_through_the_hinted_pages_and_never_reads_the_page_tree: a sync enumeration throws, naming ReadAsync. Enumeration through ReadAsync gives every page and its text, and fetches neither the page tree nor the main cross-reference section.
  • ...A_link_finds_its_page_from_the_hints_and_a_named_destination_resolves_through_the_async_read
  • ...The_outline_items_find_their_pages_from_the_hints
  • ...A_structure_element_finds_its_page_from_the_hints_and_a_tagged_page_extracts_its_layout: page 20's /Pg resolves before the hints hand out page 20.
  • ...A_local_document_runs_an_async_read_once
  • DocumentSessionTests.A_remote_linearized_tagged_page_loads_its_accessible_tree_and_links_without_synchronous_fetches (cooperative and background).

Each test checks that the hints are still in use and the page tree root was never loaded. On the old code, 6 of the 9 core cases and both viewer cases fail.

Local gate: build with -warnaserror passes, Broadside.Core.slnf has 17184 passed and 0 failed, and ConformanceCheck, format and the ADR-reference check pass.

Follow-up

AnnotationLayer.Activate resolves a named destination synchronously in the UI event. For a remote file in the browser, that name tree has not arrived. Filed as #420.

🤖 Generated with Claude Code

…e tree (#417)

#410 made PdfPageCollection.IndexOf and TryGetPage answer from the hint
tables, but only for pages the hints had already handed out. Any other
page object (a structure element's Pg, a link's or an outline item's
destination page) fell back to walking the page tree, which needs the
main cross-reference section at the end of a remote linearized file. In
the browser that throws PdfRangeNotLoadedException.

- HintedPageList maps page object numbers to pages (Table F.4 items 1
  and 2) and finds a page by its loaded page object. IndexOf and
  TryGetPage use it while the hints are in use and never read the tree.
- PdfExplicitDestination.PageIndex, TargetKind and StructureElement
  answer a page reference from the hints by its object number, without
  reading the page.
- New PdfDocument.ReadAsync<T> runs a synchronous read and fetches
  asynchronously whatever it misses. Use it for reads that need more of
  the file (every page, the outline, the structure tree, the name
  trees). PdfRangeNotLoadedException now points to it.
- DocumentSession.ReadAsync runs a remote document's reads through it,
  so a tagged page's accessible tree and text load in the browser.
- Conformance rows G.4 and F.4.2 updated.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Copilot AI balanced review requested due to automatic review settings October 25, 2026 11:17

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.

@michaelstonis
michaelstonis merged commit af68dfb into spec/33-broadside-v1 Oct 25, 2026
29 checks passed
@michaelstonis
michaelstonis deleted the 417-hinted-page-tree-walks branch October 25, 2026 12:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants