flpdf 0.8.0

A pure-Rust PDF toolkit modeled on qpdf — read, rewrite, linearize, and inspect PDFs.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
//! Outline and named-destination remapping after page extraction.
//!
//! qpdf correspondence: QPDFJob.cc page selection and QPDFWriter.cc null visibility specialized for surviving destinations.
//!
//! After [`crate::pages::tree_rebuild::rebuild_page_tree`] has rebuilt the page tree
//! for a subset extraction, this module updates the document's `/Outlines` tree,
//! `/Names /Dests` name-tree (and the legacy `/Catalog /Dests` dictionary), the
//! link annotations (`/Dest`, `/A /GoTo /D`) on every surviving page, and the
//! catalog `/OpenAction`, all to match qpdf's `--pages` behaviour:
//!
//! - Every outline item and named destination is **kept** — none are dropped.
//!   Sibling `/Prev`/`/Next` links, parent `/First`/`/Last`, `/Count`, and name-tree
//!   `/Limits` are all left unchanged, and `/Outlines`/`/Names` are never removed
//!   from the catalog.
//! - A destination whose target page **survived** is remapped to its new
//!   `ObjectRef` (the first element of `ref_map[old_ref]`, matching qpdf's rule
//!   that a destination resolves to the first occurrence of a duplicated page).
//! - Every **removed** original page leaf is replaced with `null` in place, up
//!   front and independent of how it is referenced — qpdf enumerates the
//!   original page tree and `replaceObject`s each unselected `/Page`. A
//!   destination targeting a removed page is left verbatim, now resolving to
//!   that `null`. The subsequent job subset sweep keeps
//!   the null object only while a surviving destination still references it; a
//!   removed page referenced by nothing is garbage-collected entirely. Nulling
//!   the page object — rather than whatever a destination's (possibly indirect,
//!   possibly non-page) first element points at — means a removed page reached
//!   only through a reference holder or a non-page wrapper dictionary is still
//!   severed, so excluded page contents cannot leak into the output, while a
//!   non-page object a malformed destination happens to reference is never
//!   touched.
//!
//! # qpdf 11.9.0 observed behaviour (truth source `/usr/bin/qpdf`)
//!
//! For `qpdf in.pdf --pages in.pdf 1,3 -- out.pdf` over a document with an
//! `/Outlines` tree and a `/Names /Dests` name-tree, qpdf does not drop any
//! outline item or named destination: it sets each removed page object to `null`,
//! leaving destinations pointing at the now-null page (e.g. `[ 10 0 R /XYZ 0 792 0 ]`
//! where `10 0 R` resolves to `null`), and leaves `/Count` and the name-tree
//! `/Limits` unchanged. A removed page referenced by no surviving destination is
//! absent from the output. This module reproduces that behaviour.
//!
//! The removed-page null-out is page-driven, so it also covers a removed page
//! reached only through a surviving page's link annotation (`/Dest`, or
//! `/A /GoTo /D`) or the catalog `/OpenAction`: qpdf keeps the destination
//! reference verbatim and the target page object is already `null`. An
//! annotation is structurally identical to an outline item for the *remap* of a
//! surviving-page destination, so that remap logic is reused.
//! (A removed page reached only through a structure element's `/Pg` belongs to a
//! different, drop-and-garbage-collect family handled by
//! [`crate::struct_tree_pg`]; a thread bead's `/P` is in the same drop family
//! and is not handled here.)
//!
//! # String-form `/Dest`
//!
//! `/Dest (name)` on an outline item is a named destination. Because no entry is
//! dropped, such items are kept regardless of whether their named destination's
//! page survived; only explicit page references are remapped or nulled.
//!
//! # Scope
//!
//! Single-document only. Multi-input cross-document merge is a separate path.

use crate::object_handle::{ObjectHandle, ObjectValue};
use crate::pages::tree_rebuild::RebuildResult;
use crate::{Error, ObjectRef, Pdf, Result};
use std::collections::{BTreeMap, BTreeSet};
use std::io::{Read, Seek};

const DEFAULT_MAX_OUTLINE_REMAP_DEPTH: usize = 100;

// ---------------------------------------------------------------------------
// Surviving-page map
// ---------------------------------------------------------------------------

/// The surviving-page map for a page-tree rebuild, paired with the set of every
/// rebuilt output page ref.
///
/// `map` sends each surviving source page ref to its first new occurrence
/// (`ref_map[old][0]`); a source ref absent from `map` was removed. `new_refs`
/// holds every ref in the rebuilt `/Pages` tree, so a destination already
/// pointing at a remapped new ref is recognised as a surviving target — not a
/// removed page — and is never nulled by the null-pass.
#[derive(Default)]
struct Surviving {
    /// Surviving source page ref → its first new occurrence.
    map: BTreeMap<ObjectRef, ObjectRef>,
    /// Every page ref present in the rebuilt `/Pages` tree.
    new_refs: BTreeSet<ObjectRef>,
}

impl Surviving {
    /// Build from a [`RebuildResult`]: `map` from `ref_map`'s first occurrences,
    /// `new_refs` from every rebuilt page ref (`new_kids`).
    fn from_rebuild(result: &RebuildResult) -> Self {
        let map = result
            .ref_map
            .iter()
            .filter_map(|(&old, new_refs)| new_refs.first().map(|&new| (old, new)))
            .collect();
        let new_refs = result.new_kids.iter().copied().collect();
        Surviving { map, new_refs }
    }

    /// The new (first-occurrence) ref a surviving source page remaps to, or
    /// `None` when `old` is not a surviving source ref.
    fn remap(&self, old: ObjectRef) -> Option<ObjectRef> {
        self.map.get(&old).copied()
    }

    /// Whether `page_ref` denotes a surviving page: either a surviving source
    /// ref (a remap key) or a rebuilt output ref (an already-remapped target).
    fn is_surviving_target(&self, page_ref: ObjectRef) -> bool {
        self.map.contains_key(&page_ref) || self.new_refs.contains(&page_ref)
    }
}

fn child_if_present(parent: &ObjectHandle, key: &[u8]) -> Result<Option<ObjectHandle>> {
    if parent.try_has_key(key)? {
        Ok(Some(parent.try_get_key(key)?))
    } else {
        Ok(None)
    }
}

fn indirect_child(parent: &ObjectHandle, key: &[u8]) -> Result<Option<ObjectHandle>> {
    Ok(child_if_present(parent, key)?.filter(|child| child.object_ref().is_some()))
}

// ---------------------------------------------------------------------------
// Public entry point
// ---------------------------------------------------------------------------

/// Null removed pages and remap surviving-page destinations after a page-tree
/// rebuild (qpdf `--pages` parity).
///
/// `result` is the [`RebuildResult`] returned by
/// [`crate::pages::tree_rebuild::rebuild_page_tree`]. Its `ref_map` encodes the
/// old → new page reference mapping (a page absent from the map was removed; a
/// page present maps to `ref_map[old][0]`, the first new occurrence), and
/// `removed_pages` is the set of dropped original page leaves.
///
/// First, every removed page leaf in `removed_pages` is replaced with `null` in
/// place (qpdf enumerates the original page tree and nulls each unselected
/// `/Page`, regardless of how it is referenced). Then every outline item and
/// named destination is kept and a surviving-page target is remapped to its new
/// ref; a destination targeting a removed page is left verbatim, now resolving
/// to that `null`. The function mutates `pdf` in place (same convention as
/// `rebuild_page_tree`) and remaps no navigation when there is no `/Outlines` or
/// named-destination structure (it still nulls removed pages).
///
/// # Errors
///
/// - Any error propagated from the canonical [`ObjectHandle`] resolver.
/// - [`Error::Unsupported`] when the outline depth limit (100) is exceeded or
///   an unexpected object type is encountered in the outline tree.
pub fn remap_outline_and_dests<R: Read + Seek>(
    pdf: &mut Pdf<R>,
    result: &RebuildResult,
) -> Result<()> {
    remap_outline_and_dests_with_max_depth(pdf, result, DEFAULT_MAX_OUTLINE_REMAP_DEPTH)
}

/// Like [`remap_outline_and_dests`] but with a caller-supplied outline-depth limit.
///
/// # Errors
///
/// - Any error propagated from the canonical [`ObjectHandle`] resolver.
/// - [`Error::Unsupported`] when the name-tree or outline-tree depth exceeds
///   `max_depth` while remapping.
pub fn remap_outline_and_dests_with_max_depth<R: Read + Seek>(
    pdf: &mut Pdf<R>,
    result: &RebuildResult,
    max_depth: usize,
) -> Result<()> {
    // Step 0: null every removed original page leaf in place (qpdf null-out).
    // qpdf's `--pages` enumerates the original page tree and `replaceObject`s
    // each unselected `/Page` with `null`, independent of how the page is
    // referenced. Doing this up front — rather than nulling whatever a
    // destination's first element resolves to — severs a removed page reached
    // only through a reference holder or a non-page wrapper dictionary, so its
    // contents cannot leak, while never touching a non-page object a malformed
    // destination happens to reference.
    null_removed_pages(pdf, result)?;

    // Step 1: build the surviving-page map (first new ref per surviving source)
    // together with the set of all rebuilt output refs, so a destination already
    // remapped to a surviving page's new ref is never mistaken for a removed
    // target by the remap-pass.
    let surviving = Surviving::from_rebuild(result);

    // Locate the catalog.
    let catalog_ref = match pdf.root_ref() {
        Some(r) => r,
        None => return Ok(()), // No catalog, nothing to do.
    };
    let catalog = pdf.get_object_handle(catalog_ref);
    if !catalog.try_is_dictionary()? {
        return Ok(());
    }

    let outlines = indirect_child(&catalog, b"/Outlines")?;

    // --- Step 2: Remap named destinations -------------------------------------
    // qpdf keeps every named destination: a surviving-page dest is remapped to
    // its new page ref; a removed-page dest is left verbatim (its target page
    // object was already nulled in Step 0, and an unreferenced removed page is
    // then garbage-collected by the later subset sweep). /Names and /Dests are
    // never removed from the catalog, and /Limits is never recomputed.

    // /Names may be an indirect reference OR a direct dictionary on the catalog;
    // /Dests inside it likewise.
    if let Some(names) = child_if_present(&catalog, b"/Names")? {
        if names.try_is_dictionary()? {
            if let Some(dests) = child_if_present(&names, b"/Dests")? {
                let mut nt_visited = BTreeSet::new();
                remap_name_tree(pdf, &dests, &surviving, 0, max_depth, &mut nt_visited)?;
            }
        }
    }

    // 2b. Legacy /Catalog /Dests dictionary (PDF 1.1 style)
    if let Some(dests) = child_if_present(&catalog, b"/Dests")? {
        if dests.try_is_dictionary()? {
            remap_dests_dict(pdf, &dests, &surviving)?;
        }
    }

    // --- Step 3: Remap the outline tree -----------------------------------
    // Every outline item is kept; only its destination page ref is remapped when
    // the target page survived (a removed target was already nulled in Step 0 and
    // is left referenced verbatim). Sibling links, /Count, and the /Outlines
    // catalog entry are all left unchanged.
    if let Some(outlines) = outlines {
        if let Some(first) = indirect_child(&outlines, b"/First")? {
            let mut visited: BTreeSet<ObjectRef> = BTreeSet::new();
            remap_outline_tree(pdf, first, 0, max_depth, &surviving, &mut visited)?;
        }
        // If there is no /First, the outline root has no items → nothing to do.
    }

    // --- Step 4: Link-annotation and /OpenAction destinations -------------
    // Remap a surviving-page destination reached via a surviving page's link
    // annotation (/Dest or /A /GoTo /D) or the catalog /OpenAction. A removed
    // page reached only this way was already nulled in Step 0 (the destination
    // reference is kept verbatim). (A removed page reached only via a thread-bead
    // /P or a struct element /Pg is a different, drop-and-GC family; struct elem
    // /Pg is handled by crate::struct_tree_pg, after this pass in the pipeline.)
    remap_annot_dests(pdf, result, &surviving)?;
    remap_open_action_dest(pdf, &catalog, &surviving)?;

    Ok(())
}

/// Remap link-annotation destinations on every surviving page (qpdf `--pages`
/// parity). An annotation is structurally identical to an outline item for
/// destination purposes (`/Dest` and `/A /GoTo /D`): a surviving target is
/// remapped to its new ref, while a removed target needs no action — the page
/// object was already replaced with `null` by [`null_removed_pages`] and the
/// `/Dest`/`/D` reference is kept verbatim. qpdf applies this to both indirect
/// annotations and inline (direct-dict) annotations stored in `/Annots`, so both
/// forms are handled here.
///
/// An *indirect* annotation is remapped in place via [`remap_item_dest`]. An
/// *inline* (direct-dict) annotation has no object identity, so it is remapped on
/// the array element and the updated `/Annots` array written back (to the page
/// dict for an inline array, or to the array object for an indirect array).
///
/// A duplicate-page selection (e.g. `--pages . 1,1`) produces several surviving
/// pages that share the same indirect annotation object, so the same annotation
/// reference can appear under more than one page. A `visited` set (bounded-
/// traversal guard, as in [`remap_outline_tree`] / [`remap_name_tree`]) processes
/// each shared annot reference — and each shared *indirect* `/Annots` array
/// object — exactly once, so a shared destination is not re-remapped on a later
/// pass (avoiding redundant rewrites). Correctness does not rest on the dedup
/// alone: a destination already pointing at a rebuilt output ref is recognised as
/// a surviving target by [`Surviving::is_surviving_target`], so a re-resolved
/// already-remapped `/Dest` is a no-op because its new page reference is not a
/// source key in the surviving-page map.
/// An *inline* `/Annots` array lives in a single page dict and cannot be shared
/// by reference, so it needs no dedup.
fn remap_annot_dests<R: Read + Seek>(
    pdf: &mut Pdf<R>,
    result: &RebuildResult,
    surviving: &Surviving,
) -> Result<()> {
    let mut visited_arrays: BTreeSet<ObjectRef> = BTreeSet::new();
    let mut visited_items: BTreeSet<ObjectRef> = BTreeSet::new();
    for &page_ref in &result.new_kids {
        // /Annots may be an inline array (stored in the page dict) or an
        // indirect reference to an array object.
        let page = pdf.get_object_handle(page_ref);
        let Some(annots) = child_if_present(&page, b"/Annots")? else {
            continue;
        };
        if !annots.try_is_array()? {
            continue;
        }
        if let Some(array_ref) = annots.object_ref() {
            if !visited_arrays.insert(array_ref) {
                continue;
            }
        }
        remap_annot_array(pdf, &annots, surviving, &mut visited_items)?;
    }
    Ok(())
}

/// Process every element of an `/Annots` array for destination remap.
///
/// Each indirect annotation is rewritten through its live handle exactly once
/// across duplicated pages. Direct dictionaries are mutated in place through
/// the containing array handle, so their owner observes the same identity.
fn remap_annot_array<R: Read + Seek>(
    pdf: &mut Pdf<R>,
    arr: &ObjectHandle,
    surviving: &Surviving,
    visited: &mut BTreeSet<ObjectRef>,
) -> Result<()> {
    let Some(items) = arr.try_as_array()? else {
        return Ok(());
    };
    for item in items {
        if let Some(item_ref) = item.object_ref() {
            if !visited.insert(item_ref) {
                continue;
            }
        }
        remap_item_dest(pdf, &item, surviving)?;
    }
    Ok(())
}

/// Remap the catalog `/OpenAction` destination (qpdf `--pages` parity).
/// `/OpenAction` is either a destination array `[page /Fit ...]` or an action
/// dict (possibly indirect). [`remap_action_dest`] handles both: a
/// `/S /GoTo` action's `/D` — or a bare destination array/dict — targeting a
/// surviving page is remapped (a removed target is left verbatim, its page
/// already nulled), while a non-GoTo action is kept verbatim (its `/D` is not a
/// local page destination).
fn remap_open_action_dest<R: Read + Seek>(
    pdf: &mut Pdf<R>,
    catalog: &ObjectHandle,
    surviving: &Surviving,
) -> Result<()> {
    let Some(oa) = child_if_present(catalog, b"/OpenAction")? else {
        return Ok(());
    };
    remap_action_dest(pdf, &oa, surviving)?;
    Ok(())
}

/// Remap a GoTo destination carried by an action value (`/A` or `/OpenAction`).
///
/// Only a `/S /GoTo` action's `/D` is a local page destination, so a non-GoTo
/// action (e.g. `/GoToR`, `/URI`, `/Launch`) is kept verbatim — its `/D`, when
/// present, targets a remote or named destination and must never be mistaken
/// for a local page ref. A bare destination value (an array `[page /Fit]` or a
/// `<< /D … >>` dict with no `/S`) is passed through to [`remap_dest`].
/// This mirrors the `/S /GoTo` check the indirect-annotation path performs in
/// [`remap_item_dest`].
fn remap_action_dest<R: Read + Seek>(
    pdf: &mut Pdf<R>,
    value: &ObjectHandle,
    surviving: &Surviving,
) -> Result<bool> {
    // Resolve to inspect /S without losing the original value form for the
    // write-back (remap_dest handles an indirect value in place).
    if !value.try_is_dictionary()? {
        return remap_dest(pdf, value, surviving);
    }
    if let Some(action_type) = child_if_present(value, b"/S")? {
        // A non-GoTo action: keep verbatim (its /D is not a local destination).
        if let Some(name) = action_type.try_as_name()? {
            if name != b"GoTo" {
                return Ok(false);
            }
        }
    }
    let Some(dest) = child_if_present(value, b"/D")? else {
        return Ok(false);
    };
    remap_dest(pdf, &dest, surviving)
}

/// Remap only an outline or annotation action explicitly typed as `/GoTo`.
fn remap_goto_action<R: Read + Seek>(
    pdf: &mut Pdf<R>,
    action: &ObjectHandle,
    surviving: &Surviving,
) -> Result<bool> {
    if !action.try_is_dictionary()? {
        return Ok(false);
    }
    let Some(action_type) = child_if_present(action, b"/S")? else {
        return Ok(false);
    };
    if !matches!(action_type.try_as_name()?, Some(name) if name == b"GoTo") {
        return Ok(false);
    }
    let Some(dest) = child_if_present(action, b"/D")? else {
        return Ok(false);
    };
    remap_dest(pdf, &dest, surviving)
}

// ---------------------------------------------------------------------------
// qpdf null-out: replace every removed original page leaf with `null` in place,
// independent of how it is referenced. Destination remap (surviving pages) is
// handled separately below.
// ---------------------------------------------------------------------------

/// Replace every removed original page leaf with `null` in place (qpdf null-out).
///
/// `result.removed_page_objgens` is the set of original page-tree leaves the
/// rebuild dropped — exactly the objects qpdf's `--pages` nulls (`QPDFJob` enumerates
/// the original page tree and `replaceObject`s each unselected `/Page`). This is
/// page-driven, never destination-driven: a removed page reached only through a
/// reference holder (`[40 0 R]` with `40 0 obj` = `4 0 R`) or a non-page wrapper
/// dictionary (`40 0 obj` = `<< /X 4 0 R >>`) is still severed, so its contents
/// cannot leak; a non-page object a malformed destination happens to reference
/// is never in this set and is left untouched. The subsequent subset sweep
/// drops any nulled page that no surviving destination still references.
fn null_removed_pages<R: Read + Seek>(pdf: &mut Pdf<R>, result: &RebuildResult) -> Result<()> {
    for object_gen in &result.removed_page_objgens {
        let page =
            pdf.get_object_handle_by_raw_identity(object_gen.get_obj(), object_gen.get_gen());
        // QPDF::replaceObject updates the shared cache slot for the exact raw
        // QPDFObjGen. ObjectHandle::set_resolved is the same live-slot write
        // for generations that cannot cross the N G R projection boundary.
        page.set_resolved(ObjectValue::Null);
    }
    Ok(())
}

/// Remap a `/Names`-leaf name tree (or descend its `/Kids`) in place, keeping
/// every entry. A surviving-page dest is remapped; a removed-page dest is left
/// verbatim (its target page object was already nulled by [`null_removed_pages`]).
/// `/Limits` is never recomputed.
fn remap_name_tree<R: Read + Seek>(
    pdf: &mut Pdf<R>,
    node: &ObjectHandle,
    surviving: &Surviving,
    depth: usize,
    max_depth: usize,
    visited: &mut BTreeSet<ObjectRef>,
) -> Result<()> {
    if depth >= max_depth {
        let node_ref = node
            .object_ref()
            .map_or_else(|| "direct".to_owned(), |object_ref| object_ref.to_string());
        return Err(Error::Unsupported(format!(
            "outline_dest_remap: name-tree depth limit {max_depth} exceeded at {node_ref}"
        )));
    }
    if let Some(node_ref) = node.object_ref() {
        if !visited.insert(node_ref) {
            return Ok(()); // Cycle: already processed.
        }
    }
    if !node.try_is_dictionary()? {
        return Ok(()); // Malformed node.
    }

    if let Some(pairs) = child_if_present(node, b"/Names")? {
        remap_name_pairs(pdf, &pairs, surviving)?;
        return Ok(());
    }

    if let Some(kids) = child_if_present(node, b"/Kids")? {
        if let Some(items) = kids.try_as_array()? {
            for child in items {
                if child.object_ref().is_some() {
                    remap_name_tree(pdf, &child, surviving, depth + 1, max_depth, visited)?;
                } // cov:ignore: llvm-cov attributes this syntactic closing line to the recursive call span
            }
        }
    }
    Ok(())
}

/// Keep every `(name, dest)` pair of a flat name-pairs array, remapping a
/// surviving-page dest (a removed-page dest is left verbatim, its page already
/// nulled). A trailing odd orphan key is dropped in place.
fn remap_name_pairs<R: Read + Seek>(
    pdf: &mut Pdf<R>,
    pairs: &ObjectHandle,
    surviving: &Surviving,
) -> Result<()> {
    let Some(mut items) = pairs.try_as_array()? else {
        return Ok(());
    };
    if items.len() % 2 != 0 {
        items.pop();
        pairs.set_array_items(items.clone())?;
    }
    for dest in items.iter().skip(1).step_by(2) {
        remap_dest(pdf, dest, surviving)?;
    }
    Ok(())
}

/// Remap a single destination through its live handle. A removed-page target
/// needs no action because [`null_removed_pages`] already replaced that page
/// object. Non-page and named destinations stay unchanged.
fn remap_dest<R: Read + Seek>(
    pdf: &mut Pdf<R>,
    dest: &ObjectHandle,
    surviving: &Surviving,
) -> Result<bool> {
    remap_dest_depth(pdf, dest, surviving, MAX_DEST_RESOLVE_DEPTH)
}

const MAX_DEST_RESOLVE_DEPTH: usize = 64;

fn remap_dest_depth<R: Read + Seek>(
    pdf: &mut Pdf<R>,
    dest: &ObjectHandle,
    surviving: &Surviving,
    depth: usize,
) -> Result<bool> {
    if depth == 0 {
        return Ok(false);
    }
    if let Some(items) = dest.try_as_array()? {
        let Some(first) = items.first() else {
            return Ok(false);
        };
        let Some(old_ref) = first.object_ref() else {
            return Ok(false);
        };
        let Some(new_ref) = surviving.remap(old_ref) else {
            return Ok(false);
        };
        if new_ref == old_ref || !surviving.is_surviving_target(old_ref) {
            return Ok(false);
        }
        dest.set_array_item(0, pdf.get_object_handle(new_ref))?;
        return Ok(true);
    }
    if dest.try_is_dictionary()? {
        let Some(value) = child_if_present(dest, b"/D")? else {
            return Ok(false);
        };
        return remap_dest_depth(pdf, &value, surviving, depth - 1);
    }
    Ok(false)
}

/// Keep every entry of a legacy `/Dests` dictionary, remapping surviving-page
/// dests (a removed-page dest is left verbatim, its page already nulled).
/// Returns the rebuilt dictionary.
fn remap_dests_dict<R: Read + Seek>(
    pdf: &mut Pdf<R>,
    dests: &ObjectHandle,
    surviving: &Surviving,
) -> Result<()> {
    for key in dests.try_get_keys()? {
        let value = dests.try_get_key(&key)?;
        remap_dest(pdf, &value, surviving)?;
    }
    Ok(())
}

/// Walk the outline sibling chain from `first_ref`, recursing into children,
/// keeping every item: remap each item's `/Dest` and `/A /GoTo /D` to its
/// surviving target's new ref (a removed target is left verbatim, its page
/// already nulled). Sibling links and `/Count` are left unchanged. Bounded by
/// `depth`/`max_depth` and a shared `visited` set (hostile-PDF guards).
fn remap_outline_tree<R: Read + Seek>(
    pdf: &mut Pdf<R>,
    first: ObjectHandle,
    depth: usize,
    max_depth: usize,
    surviving: &Surviving,
    visited: &mut BTreeSet<ObjectRef>,
) -> Result<()> {
    if depth >= max_depth {
        let first_ref = first
            .object_ref()
            .map_or_else(|| "direct".to_owned(), |object_ref| object_ref.to_string());
        return Err(Error::Unsupported(format!(
            "outline_dest_remap: depth limit {max_depth} exceeded at {first_ref}"
        )));
    }
    let mut current = Some(first);
    while let Some(item) = current {
        if let Some(item_ref) = item.object_ref() {
            if !visited.insert(item_ref) {
                break; // Cycle guard (/Next or /First back-edge).
            }
        }
        if !item.try_is_dictionary()? {
            break; // Malformed — stop this chain.
        }
        let next = indirect_child(&item, b"/Next")?;
        let first_child = indirect_child(&item, b"/First")?;

        // Remap surviving-page refs in place. Removed target pages need no
        // action here — they were already replaced with `null` by
        // [`null_removed_pages`] (the destination reference is kept verbatim).
        remap_item_dest(pdf, &item, surviving)?;

        if let Some(child_first) = first_child {
            remap_outline_tree(pdf, child_first, depth + 1, max_depth, surviving, visited)?;
        }
        current = next;
    }
    Ok(())
}

// ---------------------------------------------------------------------------
// Destination resolution helpers
// ---------------------------------------------------------------------------

/// Remap the page reference in an outline item's `/Dest` or `/A /D` field.
fn remap_item_dest<R: Read + Seek>(
    pdf: &mut Pdf<R>,
    item: &ObjectHandle,
    surviving: &Surviving,
) -> Result<()> {
    if !item.try_is_dictionary()? {
        return Ok(());
    }

    // /Dest — array, dict, or an indirect reference to either.
    if let Some(dest) = child_if_present(item, b"/Dest")? {
        remap_dest(pdf, &dest, surviving)?;
        // String/name-form dest: no page ref to remap here; the name tree was
        // already updated.
    }

    // /A /GoTo /D (action form). /A may be an indirect reference to the
    // action dict; resolve one level so an indirect GoTo action's /D is
    // still pruned/remapped.
    if let Some(action) = child_if_present(item, b"/A")? {
        remap_goto_action(pdf, &action, surviving)?;
    }
    Ok(())
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------