flpdf 0.3.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
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
//! Surgical in-place splice of the `/Pages` tree.
//!
//! Unlike [`crate::page_tree_rebuild`], which always produces a flat single-level
//! tree, [`splice_pages`] preserves the existing multi-level `/Pages` structure
//! and performs a targeted depth-first walk to insert/remove pages at a specific
//! position, updating `/Count` at every ancestor node and repointing `/Parent`
//! on inserted pages.

use crate::pages::{page_refs_with_max_depth, DEFAULT_MAX_PAGE_TREE_DEPTH};
use crate::{Error, Object, ObjectRef, Pdf, Result};
use std::io::{Read, Seek};

/// Remove `remove.len()` pages starting at 0-based document-order position
/// `remove.start`, then insert `insert` at that position.
///
/// This is a **surgical** operation: the existing multi-level `/Pages` tree
/// structure is preserved. `/Count` is updated at every ancestor of the
/// affected nodes, and `/Parent` is repointed on every inserted page.
///
/// A no-op call (`remove.is_empty() && insert.is_empty()`) returns immediately
/// without touching the document.
///
/// This function is part of the document page extraction and merge primitives.
/// The `insert` refs it splices in are typically pages copied
/// from another document with
/// [`copy_objects`](crate::object_copy::copy_objects), whose object set is first
/// computed per page by
/// [`page_object_closure`](crate::page_closure::page_object_closure). See the
/// runnable `examples/splice_pages.rs` and `examples/merge_pdfs.rs`.
///
/// # Errors
///
/// - [`Error::Unsupported`] if `remove.end` exceeds the document's page count.
/// - [`Error::Missing`] if the result would be an empty document.
pub fn splice_pages<R: Read + Seek>(
    pdf: &mut Pdf<R>,
    remove: std::ops::Range<usize>,
    insert: &[ObjectRef],
) -> Result<()> {
    splice_pages_with_max_depth(pdf, remove, insert, DEFAULT_MAX_PAGE_TREE_DEPTH)
}

/// Like [`splice_pages`] but with an explicit page-tree depth limit.
///
/// # Errors
///
/// - [`Error::Unsupported`] if `remove.start > remove.end`, if
///   `remove.end` exceeds the document's page count, if the insert position is not found in the
///   page tree, or if the `/Pages` tree is malformed (a node deeper than
///   `max_depth`, a node that is not a dictionary, or a `/Pages` node with a
///   missing or negative `/Count`).
/// - [`Error::Missing`] if the result would be an empty document, or if a
///   required structural entry is absent (`/Root`, the `/Catalog` dictionary,
///   or `/Pages`).
/// - Propagates any error from resolving objects and from collecting the page
///   refs (for example a malformed cross-reference table).
pub fn splice_pages_with_max_depth<R: Read + Seek>(
    pdf: &mut Pdf<R>,
    remove: std::ops::Range<usize>,
    insert: &[ObjectRef],
    max_depth: usize,
) -> Result<()> {
    if remove.start > remove.end {
        return Err(Error::Unsupported(format!(
            "splice: invalid range {}..{} (start > end)",
            remove.start, remove.end
        )));
    }

    // No-op guard.
    if remove.is_empty() && insert.is_empty() {
        return Ok(());
    }

    let page_count = page_refs_with_max_depth(pdf, max_depth)?.len();

    if remove.end > page_count {
        return Err(Error::Unsupported(format!(
            "splice: remove.end {} exceeds page count {}",
            remove.end, page_count
        )));
    }

    let remaining = page_count - remove.len() + insert.len();
    if remaining == 0 {
        return Err(Error::Missing("splice would result in an empty document"));
    }

    let catalog_ref = pdf.root_ref().ok_or(Error::Missing("/Root"))?;
    let pages_ref = {
        let catalog = pdf.resolve(catalog_ref)?;
        catalog
            .as_dict()
            .ok_or(Error::Missing("/Catalog dict"))?
            .get_ref("Pages")
            .ok_or(Error::Missing("/Pages"))?
    };

    let mut insert_done = false;
    splice_subtree(
        pdf,
        pages_ref,
        0,
        &remove,
        insert,
        &mut insert_done,
        0,
        max_depth,
    )?;

    if !insert_done && !insert.is_empty() {
        return Err(Error::Unsupported(format!(
            "splice: insert position {} not found in page tree",
            remove.start
        )));
    }

    Ok(())
}

/// Returns the leaf-page count contributed by `node_ref`.
/// - `/Pages` → its `/Count` value
/// - `/Page` (or anything else) → 1
fn leaf_count_of<R: Read + Seek>(pdf: &mut Pdf<R>, node_ref: ObjectRef) -> Result<usize> {
    let obj = pdf.resolve_borrowed(node_ref)?;
    let dict = obj
        .as_dict()
        .ok_or_else(|| Error::Unsupported(format!("node {node_ref} is not a dictionary")))?;
    match dict.get("Type").and_then(Object::as_name) {
        Some(b"Pages") => match dict.get("Count").and_then(Object::as_integer) {
            Some(n) if n >= 0 => Ok(n as usize),
            Some(n) => Err(Error::Unsupported(format!(
                "/Pages node {node_ref} has negative /Count {n}"
            ))),
            None => Err(Error::Unsupported(format!(
                "/Pages node {node_ref} has no /Count"
            ))),
        },
        _ => Ok(1),
    }
}

/// Sets `/Parent` on `page_ref` to point at `parent_ref`.
fn set_page_parent<R: Read + Seek>(
    pdf: &mut Pdf<R>,
    page_ref: ObjectRef,
    parent_ref: ObjectRef,
) -> Result<()> {
    let mut dict = pdf
        .resolve(page_ref)?
        .into_dict()
        .ok_or_else(|| Error::Unsupported(format!("page {page_ref} is not a dictionary")))?;
    dict.insert("Parent", Object::Reference(parent_ref));
    pdf.set_object(page_ref, Object::Dictionary(dict));
    Ok(())
}

/// DFS splice for a single `/Pages` node.
///
/// Returns the **net change** in leaf count within this subtree
/// (positive = pages added, negative = pages removed).
///
/// `base` is the document-order index of the first leaf page in this subtree.
/// `insert_done` is shared across all recursive calls; it flips to `true` when
/// the inserted pages have been placed exactly once.
#[allow(clippy::too_many_arguments)]
fn splice_subtree<R: Read + Seek>(
    pdf: &mut Pdf<R>,
    node_ref: ObjectRef,
    base: usize,
    remove: &std::ops::Range<usize>,
    insert: &[ObjectRef],
    insert_done: &mut bool,
    depth: usize,
    max_depth: usize,
) -> Result<i64> {
    if depth >= max_depth {
        return Err(Error::Unsupported(format!(
            "page-tree depth exceeds {max_depth} at {node_ref}"
        )));
    }

    // Snapshot the node's kids and count *before* any mutation so that
    // the borrow on `pdf` is released before we recurse.
    let (kids, old_count) = {
        let obj = pdf.resolve_borrowed(node_ref)?;
        let dict = obj
            .as_dict()
            .ok_or_else(|| Error::Unsupported(format!("{node_ref} is not a /Pages dictionary")))?;

        let kids: Vec<ObjectRef> = dict
            .get("Kids")
            .and_then(Object::as_array)
            .map(|arr| arr.iter().filter_map(Object::as_ref_id).collect())
            .unwrap_or_default();

        let old_count_raw = dict
            .get("Count")
            .and_then(Object::as_integer)
            .ok_or_else(|| Error::Unsupported(format!("/Pages node {node_ref} has no /Count")))?;
        if old_count_raw < 0 {
            return Err(Error::Unsupported(format!(
                "/Pages node {node_ref} has negative /Count {old_count_raw}"
            )));
        }
        let old_count = old_count_raw as usize;

        (kids, old_count)
    };

    let mut new_kids: Vec<ObjectRef> = Vec::with_capacity(kids.len() + insert.len());
    let mut net_delta: i64 = 0;
    let mut offset = base;

    for kid_ref in kids {
        let kid_leaf_count = leaf_count_of(pdf, kid_ref)?;
        let kid_start = offset;
        let kid_end = offset + kid_leaf_count;

        // Insertion point: insert BEFORE this kid.
        if !*insert_done && remove.start == kid_start {
            for &page_ref in insert {
                new_kids.push(page_ref);
                set_page_parent(pdf, page_ref, node_ref)?;
            }
            net_delta += insert.len() as i64;
            *insert_done = true;
        }

        let overlaps_remove = kid_end > remove.start && kid_start < remove.end;
        if overlaps_remove {
            // Determine kid type (Page vs Pages) without holding a borrow.
            let kid_is_pages = {
                let kid_obj = pdf.resolve_borrowed(kid_ref)?;
                kid_obj
                    .as_dict()
                    .and_then(|d| d.get("Type"))
                    .and_then(Object::as_name)
                    == Some(b"Pages")
            };

            if kid_is_pages {
                let sub_delta = splice_subtree(
                    pdf,
                    kid_ref,
                    kid_start,
                    remove,
                    insert,
                    insert_done,
                    depth + 1,
                    max_depth,
                )?;
                net_delta += sub_delta;

                // Drop now-empty intermediate nodes.
                let new_sub_count = kid_leaf_count as i64 + sub_delta;
                if new_sub_count > 0 {
                    new_kids.push(kid_ref);
                }
            } else {
                // /Page leaf inside remove range: drop it.
                net_delta -= 1;
            }
        } else {
            new_kids.push(kid_ref);
        }

        offset = kid_end;
    }

    // Append case: insertion point is at the end of this node's kids.
    if !*insert_done && remove.start == offset {
        for &page_ref in insert {
            new_kids.push(page_ref);
            set_page_parent(pdf, page_ref, node_ref)?;
        }
        net_delta += insert.len() as i64;
        *insert_done = true;
    }

    // Write back the modified node.
    let new_count = old_count as i64 + net_delta;
    if new_count < 0 {
        return Err(Error::Unsupported(format!(
            "splice: negative page count {new_count} for node {node_ref}"
        )));
    }
    let mut dict = pdf.resolve(node_ref)?.into_dict().ok_or_else(|| {
        Error::Unsupported(format!("{node_ref} is not a dictionary (re-resolve)"))
    })?;
    dict.insert("Count", Object::Integer(new_count));
    dict.insert(
        "Kids",
        Object::Array(new_kids.iter().map(|&r| Object::Reference(r)).collect()),
    );
    pdf.set_object(node_ref, Object::Dictionary(dict));

    Ok(net_delta)
}

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

#[cfg(test)]
mod tests {
    use super::*;
    use crate::pages::page_refs;
    use crate::Pdf;
    use std::collections::BTreeMap;
    use std::io::Cursor;

    /// Build a flat 3-page PDF:
    ///   1 0 R  Catalog → 2 0 R
    ///   2 0 R  Pages   /Kids [3 4 5] /Count 3
    ///   3 0 R  Page A  /Parent 2 0 R
    ///   4 0 R  Page B  /Parent 2 0 R
    ///   5 0 R  Page C  /Parent 2 0 R
    fn build_flat_pdf() -> Vec<u8> {
        let parts: &[(u32, &str)] = &[
            (1, "<< /Type /Catalog /Pages 2 0 R >>"),
            (2, "<< /Type /Pages /Kids [3 0 R 4 0 R 5 0 R] /Count 3 >>"),
            (3, "<< /Type /Page /Parent 2 0 R /MediaBox [0 0 612 792] >>"),
            (4, "<< /Type /Page /Parent 2 0 R /MediaBox [0 0 612 792] >>"),
            (5, "<< /Type /Page /Parent 2 0 R /MediaBox [0 0 612 792] >>"),
        ];
        build_pdf(parts)
    }

    /// Build a 2-level PDF with 4 pages:
    ///   1 0 R  Catalog
    ///   2 0 R  Pages root  /Kids [3 6] /Count 4
    ///   3 0 R  Pages left  /Kids [4 5] /Count 2  /Parent 2 0 R
    ///   4 0 R  Page A      /Parent 3 0 R
    ///   5 0 R  Page B      /Parent 3 0 R
    ///   6 0 R  Pages right /Kids [7 8] /Count 2  /Parent 2 0 R
    ///   7 0 R  Page C      /Parent 6 0 R
    ///   8 0 R  Page D      /Parent 6 0 R
    fn build_nested_pdf() -> Vec<u8> {
        let parts: &[(u32, &str)] = &[
            (1, "<< /Type /Catalog /Pages 2 0 R >>"),
            (2, "<< /Type /Pages /Kids [3 0 R 6 0 R] /Count 4 >>"),
            (
                3,
                "<< /Type /Pages /Parent 2 0 R /Kids [4 0 R 5 0 R] /Count 2 >>",
            ),
            (4, "<< /Type /Page /Parent 3 0 R /MediaBox [0 0 612 792] >>"),
            (5, "<< /Type /Page /Parent 3 0 R /MediaBox [0 0 612 792] >>"),
            (
                6,
                "<< /Type /Pages /Parent 2 0 R /Kids [7 0 R 8 0 R] /Count 2 >>",
            ),
            (7, "<< /Type /Page /Parent 6 0 R /MediaBox [0 0 612 792] >>"),
            (8, "<< /Type /Page /Parent 6 0 R /MediaBox [0 0 612 792] >>"),
        ];
        build_pdf(parts)
    }

    fn build_pdf(parts: &[(u32, &str)]) -> Vec<u8> {
        let mut out: Vec<u8> = b"%PDF-1.5\n".to_vec();
        let mut offs: BTreeMap<u32, u64> = BTreeMap::new();
        for (n, s) in parts {
            offs.insert(*n, out.len() as u64);
            out.extend_from_slice(format!("{n} 0 obj\n{s}\nendobj\n").as_bytes());
        }
        let max_obj = parts.iter().map(|(n, _)| n).max().copied().unwrap_or(0);
        let total = max_obj + 1;
        let xref_start = out.len() as u64;
        out.extend_from_slice(format!("xref\n0 {total}\n0000000000 65535 f \n").as_bytes());
        for i in 1..total {
            out.extend_from_slice(
                format!("{:010} 00000 n \n", offs.get(&i).copied().unwrap_or(0)).as_bytes(),
            );
        }
        out.extend_from_slice(
            format!("trailer\n<< /Size {total} /Root 1 0 R >>\nstartxref\n{xref_start}\n%%EOF\n")
                .as_bytes(),
        );
        out
    }

    fn open(bytes: Vec<u8>) -> Pdf<Cursor<Vec<u8>>> {
        Pdf::open(Cursor::new(bytes)).expect("PDF should parse")
    }

    fn page_list(pdf: &mut Pdf<Cursor<Vec<u8>>>) -> Vec<ObjectRef> {
        page_refs(pdf).expect("page_refs failed")
    }

    fn dict_of(pdf: &mut Pdf<Cursor<Vec<u8>>>, r: ObjectRef) -> crate::Dictionary {
        pdf.resolve(r)
            .unwrap()
            .into_dict()
            .expect("not a dictionary")
    }

    #[test]
    fn noop_returns_ok_and_does_not_mutate() {
        let mut pdf = open(build_flat_pdf());
        let before = page_list(&mut pdf);
        splice_pages(&mut pdf, 0..0, &[]).unwrap();
        let after = page_list(&mut pdf);
        assert_eq!(before, after);
    }

    #[test]
    fn remove_first_page_flat_tree() {
        let mut pdf = open(build_flat_pdf());
        splice_pages(&mut pdf, 0..1, &[]).unwrap();
        let pages = page_list(&mut pdf);
        assert_eq!(pages.len(), 2);
        assert_eq!(pages[0], ObjectRef::new(4, 0)); // B
        assert_eq!(pages[1], ObjectRef::new(5, 0)); // C
                                                    // Root /Pages /Count should be 2.
        let root = dict_of(&mut pdf, ObjectRef::new(2, 0));
        assert_eq!(root.get("Count"), Some(&Object::Integer(2)));
    }

    #[test]
    fn remove_last_page_flat_tree() {
        let mut pdf = open(build_flat_pdf());
        splice_pages(&mut pdf, 2..3, &[]).unwrap();
        let pages = page_list(&mut pdf);
        assert_eq!(pages.len(), 2);
        assert_eq!(pages[0], ObjectRef::new(3, 0)); // A
        assert_eq!(pages[1], ObjectRef::new(4, 0)); // B
        let root = dict_of(&mut pdf, ObjectRef::new(2, 0));
        assert_eq!(root.get("Count"), Some(&Object::Integer(2)));
    }

    #[test]
    fn insert_at_start_flat_tree() {
        let mut pdf = open(build_flat_pdf());
        let new_page = ObjectRef::new(6, 0);
        pdf.set_object(
            new_page,
            Object::Dictionary({
                let mut d = crate::Dictionary::new();
                d.insert("Type", Object::Name(b"Page".to_vec()));
                d.insert(
                    "MediaBox",
                    Object::Array(vec![
                        Object::Integer(0),
                        Object::Integer(0),
                        Object::Integer(612),
                        Object::Integer(792),
                    ]),
                );
                d
            }),
        );
        splice_pages(&mut pdf, 0..0, &[new_page]).unwrap();
        let pages = page_list(&mut pdf);
        assert_eq!(pages.len(), 4);
        assert_eq!(pages[0], new_page);
        assert_eq!(pages[1], ObjectRef::new(3, 0));
        // /Parent of new_page must point at root /Pages (2 0 R).
        let d = dict_of(&mut pdf, new_page);
        assert_eq!(d.get_ref("Parent"), Some(ObjectRef::new(2, 0)));
        // /Count = 4
        let root = dict_of(&mut pdf, ObjectRef::new(2, 0));
        assert_eq!(root.get("Count"), Some(&Object::Integer(4)));
    }

    #[test]
    fn insert_at_end_flat_tree() {
        let mut pdf = open(build_flat_pdf());
        let new_page = ObjectRef::new(6, 0);
        pdf.set_object(
            new_page,
            Object::Dictionary({
                let mut d = crate::Dictionary::new();
                d.insert("Type", Object::Name(b"Page".to_vec()));
                d.insert(
                    "MediaBox",
                    Object::Array(vec![
                        Object::Integer(0),
                        Object::Integer(0),
                        Object::Integer(612),
                        Object::Integer(792),
                    ]),
                );
                d
            }),
        );
        splice_pages(&mut pdf, 3..3, &[new_page]).unwrap();
        let pages = page_list(&mut pdf);
        assert_eq!(pages.len(), 4);
        assert_eq!(pages[3], new_page);
        let d = dict_of(&mut pdf, new_page);
        assert_eq!(d.get_ref("Parent"), Some(ObjectRef::new(2, 0)));
    }

    #[test]
    fn insert_in_middle_flat_tree() {
        let mut pdf = open(build_flat_pdf());
        let new_page = ObjectRef::new(6, 0);
        pdf.set_object(
            new_page,
            Object::Dictionary({
                let mut d = crate::Dictionary::new();
                d.insert("Type", Object::Name(b"Page".to_vec()));
                d.insert(
                    "MediaBox",
                    Object::Array(vec![
                        Object::Integer(0),
                        Object::Integer(0),
                        Object::Integer(612),
                        Object::Integer(792),
                    ]),
                );
                d
            }),
        );
        // Insert after page B (between index 1 and 2)
        splice_pages(&mut pdf, 2..2, &[new_page]).unwrap();
        let pages = page_list(&mut pdf);
        assert_eq!(pages.len(), 4);
        assert_eq!(pages[0], ObjectRef::new(3, 0)); // A
        assert_eq!(pages[1], ObjectRef::new(4, 0)); // B
        assert_eq!(pages[2], new_page); // X
        assert_eq!(pages[3], ObjectRef::new(5, 0)); // C
    }

    #[test]
    fn remove_range_flat_tree() {
        let mut pdf = open(build_flat_pdf());
        splice_pages(&mut pdf, 0..2, &[]).unwrap(); // remove A, B
        let pages = page_list(&mut pdf);
        assert_eq!(pages.len(), 1);
        assert_eq!(pages[0], ObjectRef::new(5, 0)); // C only
        let root = dict_of(&mut pdf, ObjectRef::new(2, 0));
        assert_eq!(root.get("Count"), Some(&Object::Integer(1)));
    }

    #[test]
    fn replace_middle_page_flat_tree() {
        let mut pdf = open(build_flat_pdf());
        let new_page = ObjectRef::new(6, 0);
        pdf.set_object(
            new_page,
            Object::Dictionary({
                let mut d = crate::Dictionary::new();
                d.insert("Type", Object::Name(b"Page".to_vec()));
                d.insert(
                    "MediaBox",
                    Object::Array(vec![
                        Object::Integer(0),
                        Object::Integer(0),
                        Object::Integer(612),
                        Object::Integer(792),
                    ]),
                );
                d
            }),
        );
        // Replace page B (index 1) with new_page.
        splice_pages(&mut pdf, 1..2, &[new_page]).unwrap();
        let pages = page_list(&mut pdf);
        assert_eq!(pages.len(), 3);
        assert_eq!(pages[0], ObjectRef::new(3, 0)); // A
        assert_eq!(pages[1], new_page); // X
        assert_eq!(pages[2], ObjectRef::new(5, 0)); // C
                                                    // Count stays 3.
        let root = dict_of(&mut pdf, ObjectRef::new(2, 0));
        assert_eq!(root.get("Count"), Some(&Object::Integer(3)));
    }

    /// Remove page B (index 1, in left subtree) from the nested tree.
    /// CRITICAL: intermediate nodes (3 0 R left, 6 0 R right) must STILL EXIST
    /// with their /Count updated. This is the key difference from rebuild_page_tree.
    #[test]
    fn nested_remove_updates_intermediate_count() {
        let mut pdf = open(build_nested_pdf());
        splice_pages(&mut pdf, 1..2, &[]).unwrap(); // remove B
                                                    // Page order: A C D
        let pages = page_list(&mut pdf);
        assert_eq!(pages.len(), 3);
        assert_eq!(pages[0], ObjectRef::new(4, 0)); // A
        assert_eq!(pages[1], ObjectRef::new(7, 0)); // C
        assert_eq!(pages[2], ObjectRef::new(8, 0)); // D
                                                    // Root /Count = 3
        let root = dict_of(&mut pdf, ObjectRef::new(2, 0));
        assert_eq!(root.get("Count"), Some(&Object::Integer(3)));
        // Left intermediate node /Count = 1 (only A remains)
        let left = dict_of(&mut pdf, ObjectRef::new(3, 0));
        assert_eq!(left.get("Count"), Some(&Object::Integer(1)));
        // Right intermediate node /Count = 2 (unchanged)
        let right = dict_of(&mut pdf, ObjectRef::new(6, 0));
        assert_eq!(right.get("Count"), Some(&Object::Integer(2)));
    }

    /// Remove pages B and C (indices 1 and 2), which span both left and right subtrees.
    #[test]
    fn nested_remove_spanning_subtrees() {
        let mut pdf = open(build_nested_pdf());
        splice_pages(&mut pdf, 1..3, &[]).unwrap(); // remove B (left) and C (right)
        let pages = page_list(&mut pdf);
        assert_eq!(pages.len(), 2);
        assert_eq!(pages[0], ObjectRef::new(4, 0)); // A
        assert_eq!(pages[1], ObjectRef::new(8, 0)); // D
        let root = dict_of(&mut pdf, ObjectRef::new(2, 0));
        assert_eq!(root.get("Count"), Some(&Object::Integer(2)));
        // Left subtree: only A remains → /Count = 1
        let left = dict_of(&mut pdf, ObjectRef::new(3, 0));
        assert_eq!(left.get("Count"), Some(&Object::Integer(1)));
        // Right subtree: only D remains → /Count = 1
        let right = dict_of(&mut pdf, ObjectRef::new(6, 0));
        assert_eq!(right.get("Count"), Some(&Object::Integer(1)));
    }

    #[test]
    fn error_remove_end_out_of_bounds() {
        let mut pdf = open(build_flat_pdf()); // 3 pages
        let err = splice_pages(&mut pdf, 0..4, &[]).unwrap_err();
        assert!(matches!(err, Error::Unsupported(_)), "got {err:?}");
    }

    #[test]
    fn error_empty_result_document() {
        let mut pdf = open(build_flat_pdf()); // 3 pages
        let err = splice_pages(&mut pdf, 0..3, &[]).unwrap_err();
        assert!(matches!(err, Error::Missing(_)), "got {err:?}");
    }

    /// Remove all pages in the left subtree (A and B, indices 0..2).
    /// The now-empty left intermediate node must be dropped from root /Kids.
    #[test]
    fn empty_intermediate_node_is_dropped() {
        let mut pdf = open(build_nested_pdf()); // A B C D
        splice_pages(&mut pdf, 0..2, &[]).unwrap(); // remove A, B
        let pages = page_list(&mut pdf);
        assert_eq!(pages.len(), 2);
        assert_eq!(pages[0], ObjectRef::new(7, 0)); // C
        assert_eq!(pages[1], ObjectRef::new(8, 0)); // D
        let root = dict_of(&mut pdf, ObjectRef::new(2, 0));
        // Root /Kids should only contain right subtree (6 0 R).
        let kids = root.get("Kids").and_then(Object::as_array).unwrap();
        assert_eq!(kids.len(), 1);
        assert_eq!(kids[0].as_ref_id(), Some(ObjectRef::new(6, 0)));
        assert_eq!(root.get("Count"), Some(&Object::Integer(2)));
    }

    /// Insert a new page at index 2 (between B and C, at the boundary of left and right subtrees).
    /// The new page should be inserted into the right subtree (as its first kid).
    #[test]
    fn nested_insert_at_subtree_boundary() {
        let mut pdf = open(build_nested_pdf());
        let new_page = ObjectRef::new(9, 0);
        pdf.set_object(
            new_page,
            Object::Dictionary({
                let mut d = crate::Dictionary::new();
                d.insert("Type", Object::Name(b"Page".to_vec()));
                d.insert(
                    "MediaBox",
                    Object::Array(vec![
                        Object::Integer(0),
                        Object::Integer(0),
                        Object::Integer(612),
                        Object::Integer(792),
                    ]),
                );
                d
            }),
        );
        splice_pages(&mut pdf, 2..2, &[new_page]).unwrap();
        let pages = page_list(&mut pdf);
        assert_eq!(pages.len(), 5);
        assert_eq!(pages[0], ObjectRef::new(4, 0)); // A
        assert_eq!(pages[1], ObjectRef::new(5, 0)); // B
        assert_eq!(pages[2], new_page); // X
        assert_eq!(pages[3], ObjectRef::new(7, 0)); // C
        assert_eq!(pages[4], ObjectRef::new(8, 0)); // D
                                                    // Root /Count = 5
        let root = dict_of(&mut pdf, ObjectRef::new(2, 0));
        assert_eq!(root.get("Count"), Some(&Object::Integer(5)));
        // new_page's /Parent should point at an ancestor /Pages node.
        let d = dict_of(&mut pdf, new_page);
        let parent = d.get_ref("Parent").expect("/Parent must be set");
        // Parent must be a /Pages node in the tree
        let parent_dict = dict_of(&mut pdf, parent);
        assert_eq!(
            parent_dict.get("Type").and_then(Object::as_name),
            Some(b"Pages".as_ref())
        );
    }
}