harumi 1.5.18

Pure-Rust PDF — CJK font embedding (Chinese/Japanese/Korean), OCR text overlay, text extraction, HTML→PDF, page merge/split. WASM-ready, zero C deps.
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
/// Tests for link annotations and bookmarks (v0.5 features).
mod helpers;

use harumi::{Document, Error};
use helpers::minimal_pdf_bytes;

// ---------------------------------------------------------------------------
// Link URL annotations
// ---------------------------------------------------------------------------

#[test]
fn add_link_url_basic() {
    let pdf = minimal_pdf_bytes();
    let mut doc = Document::from_bytes(&pdf).unwrap();

    doc.page(1)
        .unwrap()
        .add_link_url([72.0, 700.0, 200.0, 20.0], "https://example.com")
        .unwrap();

    let bytes = doc.save_to_bytes().unwrap();
    let reloaded = harumi::lopdf::Document::load_from(bytes.as_slice()).unwrap();

    // The page should have an /Annots entry after save.
    let page_ids = reloaded.get_pages();
    let page_id = *page_ids.get(&1).unwrap();
    let page_dict = reloaded.get_object(page_id).unwrap().as_dict().unwrap();
    assert!(
        page_dict.get(b"Annots").is_ok(),
        "/Annots must be present on page"
    );
}

#[test]
fn add_link_url_annot_subtype_is_link() {
    let pdf = minimal_pdf_bytes();
    let mut doc = Document::from_bytes(&pdf).unwrap();

    doc.page(1)
        .unwrap()
        .add_link_url([0.0, 0.0, 100.0, 20.0], "https://example.org")
        .unwrap();

    let bytes = doc.save_to_bytes().unwrap();
    let reloaded = harumi::lopdf::Document::load_from(bytes.as_slice()).unwrap();

    let page_ids = reloaded.get_pages();
    let page_id = *page_ids.get(&1).unwrap();
    let page_dict = reloaded.get_object(page_id).unwrap().as_dict().unwrap();
    let annots = page_dict.get(b"Annots").unwrap().as_array().unwrap();
    assert!(!annots.is_empty(), "Annots array must not be empty");

    // Dereference the first annotation and check its /Subtype.
    let annot_ref = annots[0].as_reference().unwrap();
    let annot = reloaded.get_object(annot_ref).unwrap().as_dict().unwrap();
    let subtype = annot.get(b"Subtype").unwrap().as_name().unwrap();
    assert_eq!(subtype, b"Link");
}

#[test]
fn add_link_url_empty_url_returns_error() {
    let pdf = minimal_pdf_bytes();
    let mut doc = Document::from_bytes(&pdf).unwrap();
    let result = doc
        .page(1)
        .unwrap()
        .add_link_url([0.0, 0.0, 100.0, 20.0], "");
    assert!(matches!(result, Err(Error::InvalidInput(_))));
}

#[test]
fn add_link_url_nan_rect_returns_error() {
    let pdf = minimal_pdf_bytes();
    let mut doc = Document::from_bytes(&pdf).unwrap();
    let result = doc
        .page(1)
        .unwrap()
        .add_link_url([f32::NAN, 0.0, 100.0, 20.0], "https://example.com");
    assert!(matches!(result, Err(Error::InvalidInput(_))));
}

#[test]
fn add_multiple_url_links_on_same_page() {
    let pdf = minimal_pdf_bytes();
    let mut doc = Document::from_bytes(&pdf).unwrap();

    doc.page(1)
        .unwrap()
        .add_link_url([0.0, 700.0, 200.0, 15.0], "https://a.example.com")
        .unwrap();
    doc.page(1)
        .unwrap()
        .add_link_url([0.0, 680.0, 200.0, 15.0], "https://b.example.com")
        .unwrap();

    let bytes = doc.save_to_bytes().unwrap();
    let reloaded = harumi::lopdf::Document::load_from(bytes.as_slice()).unwrap();

    let page_ids = reloaded.get_pages();
    let page_id = *page_ids.get(&1).unwrap();
    let page_dict = reloaded.get_object(page_id).unwrap().as_dict().unwrap();
    let annots = page_dict.get(b"Annots").unwrap().as_array().unwrap();
    assert_eq!(annots.len(), 2, "Should have exactly two annotations");
}

// ---------------------------------------------------------------------------
// Internal link annotations
// ---------------------------------------------------------------------------

#[test]
fn add_link_internal_two_page_doc() {
    // Build a 2-page document.
    let mut doc = Document::new((595.0, 842.0)).unwrap();
    doc.insert_blank_page(1, (595.0, 842.0)).unwrap();
    assert_eq!(doc.page_count(), 2);

    doc.page(1)
        .unwrap()
        .add_link_internal([72.0, 700.0, 200.0, 20.0], 2)
        .unwrap();

    let bytes = doc.save_to_bytes().unwrap();
    let reloaded = harumi::lopdf::Document::load_from(bytes.as_slice()).unwrap();

    let page_ids = reloaded.get_pages();
    let page_id = *page_ids.get(&1).unwrap();
    let page_dict = reloaded.get_object(page_id).unwrap().as_dict().unwrap();
    let annots = page_dict.get(b"Annots").unwrap().as_array().unwrap();
    assert!(!annots.is_empty());

    let annot_ref = annots[0].as_reference().unwrap();
    let annot = reloaded.get_object(annot_ref).unwrap().as_dict().unwrap();
    assert_eq!(annot.get(b"Subtype").unwrap().as_name().unwrap(), b"Link");
    // Internal links use /Dest, not /A.
    assert!(
        annot.get(b"Dest").is_ok(),
        "/Dest must be present for internal links"
    );
    assert!(
        annot.get(b"A").is_err(),
        "/A must NOT be present for internal links"
    );
}

#[test]
fn add_link_internal_out_of_range_returns_error() {
    let pdf = minimal_pdf_bytes();
    let mut doc = Document::from_bytes(&pdf).unwrap();
    let result = doc
        .page(1)
        .unwrap()
        .add_link_internal([0.0, 0.0, 100.0, 20.0], 99);
    assert!(matches!(result, Err(Error::PageNotFound(99))));
}

// ---------------------------------------------------------------------------
// Bookmarks / document outline
// ---------------------------------------------------------------------------

#[test]
fn add_bookmark_adds_outlines_to_catalog() {
    let mut doc = Document::new((595.0, 842.0)).unwrap();
    doc.add_bookmark("Introduction", 1, 800.0).unwrap();
    let bytes = doc.save_to_bytes().unwrap();

    let reloaded = harumi::lopdf::Document::load_from(bytes.as_slice()).unwrap();
    let root_ref = reloaded
        .trailer
        .get(b"Root")
        .unwrap()
        .as_reference()
        .unwrap();
    let catalog = reloaded.get_object(root_ref).unwrap().as_dict().unwrap();
    assert!(
        catalog.get(b"Outlines").is_ok(),
        "/Outlines must be present in /Catalog"
    );
}

#[test]
fn add_bookmark_count_and_linked_list() {
    let mut doc = Document::new((595.0, 842.0)).unwrap();
    doc.insert_blank_page(1, (595.0, 842.0)).unwrap();
    doc.insert_blank_page(2, (595.0, 842.0)).unwrap();
    assert_eq!(doc.page_count(), 3);

    doc.add_bookmark("Chapter 1", 1, 800.0).unwrap();
    doc.add_bookmark("Chapter 2", 2, 800.0).unwrap();
    doc.add_bookmark("Chapter 3", 3, 800.0).unwrap();

    let bytes = doc.save_to_bytes().unwrap();
    let reloaded = harumi::lopdf::Document::load_from(bytes.as_slice()).unwrap();

    let root_ref = reloaded
        .trailer
        .get(b"Root")
        .unwrap()
        .as_reference()
        .unwrap();
    let catalog = reloaded.get_object(root_ref).unwrap().as_dict().unwrap();
    let outlines_ref = catalog.get(b"Outlines").unwrap().as_reference().unwrap();
    let outlines = reloaded
        .get_object(outlines_ref)
        .unwrap()
        .as_dict()
        .unwrap();

    let count = outlines.get(b"Count").unwrap().as_i64().unwrap();
    assert_eq!(count, 3, "/Count must equal number of bookmarks");

    // First and Last must exist.
    assert!(outlines.get(b"First").is_ok());
    assert!(outlines.get(b"Last").is_ok());
}

#[test]
fn add_bookmark_first_item_has_no_prev() {
    let mut doc = Document::new((595.0, 842.0)).unwrap();
    doc.insert_blank_page(1, (595.0, 842.0)).unwrap();
    doc.add_bookmark("First", 1, 800.0).unwrap();
    doc.add_bookmark("Second", 2, 800.0).unwrap();

    let bytes = doc.save_to_bytes().unwrap();
    let reloaded = harumi::lopdf::Document::load_from(bytes.as_slice()).unwrap();

    let root_ref = reloaded
        .trailer
        .get(b"Root")
        .unwrap()
        .as_reference()
        .unwrap();
    let catalog = reloaded.get_object(root_ref).unwrap().as_dict().unwrap();
    let outlines_ref = catalog.get(b"Outlines").unwrap().as_reference().unwrap();
    let outlines = reloaded
        .get_object(outlines_ref)
        .unwrap()
        .as_dict()
        .unwrap();

    let first_ref = outlines.get(b"First").unwrap().as_reference().unwrap();
    let first = reloaded.get_object(first_ref).unwrap().as_dict().unwrap();
    assert!(first.get(b"Prev").is_err(), "First item must have no /Prev");
    assert!(first.get(b"Next").is_ok(), "First item must have a /Next");
}

#[test]
fn add_bookmark_out_of_range_page_returns_error() {
    let pdf = minimal_pdf_bytes();
    let mut doc = Document::from_bytes(&pdf).unwrap();
    let result = doc.add_bookmark("Ghost", 99, 800.0);
    assert!(matches!(result, Err(Error::PageNotFound(99))));
}

#[test]
fn add_bookmark_after_save_returns_error() {
    let mut doc = Document::new((595.0, 842.0)).unwrap();
    let font = doc
        .embed_font(include_bytes!("fixtures/NotoSansJP-Regular.ttf"))
        .unwrap();
    doc.page(1)
        .unwrap()
        .add_invisible_text("x", font, [10.0, 10.0], 10.0)
        .unwrap();
    doc.save_to_bytes().unwrap(); // triggers finalize()

    let result = doc.add_bookmark("Late", 1, 800.0);
    assert!(matches!(result, Err(Error::InvalidInput(_))));
}

#[test]
fn add_bookmark_cjk_title_roundtrips() {
    let mut doc = Document::new((595.0, 842.0)).unwrap();
    doc.add_bookmark("第1章 はじめに", 1, 800.0).unwrap();
    let bytes = doc.save_to_bytes().unwrap();

    // Just verify the document saves without error and has /Outlines.
    let reloaded = harumi::lopdf::Document::load_from(bytes.as_slice()).unwrap();
    let root_ref = reloaded
        .trailer
        .get(b"Root")
        .unwrap()
        .as_reference()
        .unwrap();
    let catalog = reloaded.get_object(root_ref).unwrap().as_dict().unwrap();
    assert!(catalog.get(b"Outlines").is_ok());
}

// ---------------------------------------------------------------------------
// Bookmarks + text ops coexist
// ---------------------------------------------------------------------------

#[test]
fn bookmark_and_text_on_same_document() {
    let mut doc = Document::new((595.0, 842.0)).unwrap();
    let font = doc
        .embed_font(include_bytes!("fixtures/NotoSansJP-Regular.ttf"))
        .unwrap();
    doc.page(1)
        .unwrap()
        .add_text("Hello", font, [72.0, 700.0], 12.0, [0.0, 0.0, 0.0])
        .unwrap();
    doc.add_bookmark("Start", 1, 800.0).unwrap();

    let bytes = doc.save_to_bytes().unwrap();
    let reloaded = harumi::lopdf::Document::load_from(bytes.as_slice()).unwrap();

    let root_ref = reloaded
        .trailer
        .get(b"Root")
        .unwrap()
        .as_reference()
        .unwrap();
    let catalog = reloaded.get_object(root_ref).unwrap().as_dict().unwrap();
    assert!(
        catalog.get(b"Outlines").is_ok(),
        "Bookmarks must survive alongside text ops"
    );
}

/// A document with only bookmarks (no text ops) must still save successfully.
#[test]
fn bookmarks_only_no_text_ops() {
    let mut doc = Document::new((595.0, 842.0)).unwrap();
    doc.add_bookmark("Single Bookmark", 1, 800.0).unwrap();
    let bytes = doc.save_to_bytes().unwrap();
    assert!(!bytes.is_empty());

    let reloaded = harumi::lopdf::Document::load_from(bytes.as_slice()).unwrap();
    let root_ref = reloaded
        .trailer
        .get(b"Root")
        .unwrap()
        .as_reference()
        .unwrap();
    let catalog = reloaded.get_object(root_ref).unwrap().as_dict().unwrap();
    assert!(catalog.get(b"Outlines").is_ok());
}

// ---------------------------------------------------------------------------
// Regression: add_bookmark must not clobber pre-existing /Outlines
// ---------------------------------------------------------------------------

/// Save a doc with one bookmark, reload it, add a second bookmark, save again.
/// Both bookmarks must be present in the final /Outlines linked list.
#[test]
fn add_bookmark_preserves_existing_outlines_on_reload() {
    // Round 1: create a document with one bookmark.
    let mut doc = Document::new((595.0, 842.0)).unwrap();
    doc.add_bookmark("First", 1, 800.0).unwrap();
    let bytes1 = doc.save_to_bytes().unwrap();

    // Round 2: reload and add a second bookmark.
    let mut doc2 = Document::from_bytes(&bytes1).unwrap();
    doc2.add_bookmark("Second", 1, 600.0).unwrap();
    let bytes2 = doc2.save_to_bytes().unwrap();

    // Verify: both bookmarks survive in the final PDF.
    let reloaded = harumi::lopdf::Document::load_from(bytes2.as_slice()).unwrap();
    let root_ref = reloaded
        .trailer
        .get(b"Root")
        .unwrap()
        .as_reference()
        .unwrap();
    let catalog = reloaded.get_object(root_ref).unwrap().as_dict().unwrap();
    let outlines_ref = catalog.get(b"Outlines").unwrap().as_reference().unwrap();
    let outlines = reloaded
        .get_object(outlines_ref)
        .unwrap()
        .as_dict()
        .unwrap();

    let count = outlines.get(b"Count").unwrap().as_i64().unwrap();
    assert_eq!(
        count, 2,
        "Both bookmarks must be present after reload+add cycle"
    );

    // Walk the linked list: First → Second.
    let first_ref = outlines.get(b"First").unwrap().as_reference().unwrap();
    let first_item = reloaded.get_object(first_ref).unwrap().as_dict().unwrap();
    let first_title = match first_item.get(b"Title").unwrap() {
        harumi::lopdf::Object::String(b, _) => b.clone(),
        _ => panic!("Expected String"),
    };
    // "First" is ASCII, stored as a literal byte string.
    assert_eq!(first_title, b"First", "First bookmark title mismatch");

    let second_ref = first_item.get(b"Next").unwrap().as_reference().unwrap();
    let second_item = reloaded.get_object(second_ref).unwrap().as_dict().unwrap();
    let second_title = match second_item.get(b"Title").unwrap() {
        harumi::lopdf::Object::String(b, _) => b.clone(),
        _ => panic!("Expected String"),
    };
    assert_eq!(second_title, b"Second", "Second bookmark title mismatch");

    // The last item must have no /Next.
    assert!(
        second_item.get(b"Next").is_err(),
        "Last item must not have /Next"
    );
}

// ---------------------------------------------------------------------------
// Semantic / round-trip correctness
// ---------------------------------------------------------------------------

/// The URI string passed to add_link_url must survive the save/reload cycle.
#[test]
fn add_link_url_uri_content_roundtrip() {
    let url = "https://example.com/harumi?v=0.5";
    let pdf = minimal_pdf_bytes();
    let mut doc = Document::from_bytes(&pdf).unwrap();
    doc.page(1)
        .unwrap()
        .add_link_url([72.0, 700.0, 200.0, 20.0], url)
        .unwrap();

    let bytes = doc.save_to_bytes().unwrap();
    let reloaded = harumi::lopdf::Document::load_from(bytes.as_slice()).unwrap();

    let page_ids = reloaded.get_pages();
    let page_id = *page_ids.get(&1).unwrap();
    let page_dict = reloaded.get_object(page_id).unwrap().as_dict().unwrap();
    let annots = page_dict.get(b"Annots").unwrap().as_array().unwrap();
    let annot_ref = annots[0].as_reference().unwrap();
    let annot = reloaded.get_object(annot_ref).unwrap().as_dict().unwrap();

    let action = annot.get(b"A").unwrap().as_dict().unwrap();
    let stored_url = action.get(b"URI").unwrap().as_str().unwrap();
    assert_eq!(
        stored_url,
        url.as_bytes(),
        "URI string must round-trip unchanged"
    );
}

/// add_link_internal must create a /Dest whose first element points to the
/// target page's object ID (not some other page).
#[test]
fn add_link_internal_dest_points_to_correct_page() {
    let mut doc = Document::new((595.0, 842.0)).unwrap();
    doc.insert_blank_page(1, (595.0, 842.0)).unwrap();
    doc.insert_blank_page(2, (595.0, 842.0)).unwrap();
    assert_eq!(doc.page_count(), 3);

    // Link from page 1 to page 3.
    doc.page(1)
        .unwrap()
        .add_link_internal([0.0, 0.0, 100.0, 20.0], 3)
        .unwrap();

    let bytes = doc.save_to_bytes().unwrap();
    let reloaded = harumi::lopdf::Document::load_from(bytes.as_slice()).unwrap();

    let page_ids = reloaded.get_pages();
    let page1_id = *page_ids.get(&1).unwrap();
    // Resolve page 3's ObjectId from the reloaded document (object IDs survive save/reload).
    let page3_id = *page_ids.get(&3).unwrap();

    let page_dict = reloaded.get_object(page1_id).unwrap().as_dict().unwrap();
    let annots = page_dict.get(b"Annots").unwrap().as_array().unwrap();
    let annot_ref = annots[0].as_reference().unwrap();
    let annot = reloaded.get_object(annot_ref).unwrap().as_dict().unwrap();

    let dest = annot.get(b"Dest").unwrap().as_array().unwrap();
    let dest_page_id = dest[0].as_reference().unwrap();
    assert_eq!(
        dest_page_id, page3_id,
        "/Dest must reference the correct page object"
    );
}

/// add_bookmark with CJK title: round-trip the /Title and verify it decodes
/// back to the original string via the same UTF-16BE decode path that
/// lopdf_string_to_rust uses.
#[test]
fn add_bookmark_cjk_title_semantic_roundtrip() {
    let title = "第1章 はじめに";
    let mut doc = Document::new((595.0, 842.0)).unwrap();
    doc.add_bookmark(title, 1, 800.0).unwrap();

    let bytes = doc.save_to_bytes().unwrap();
    let reloaded = harumi::lopdf::Document::load_from(bytes.as_slice()).unwrap();

    let root_ref = reloaded
        .trailer
        .get(b"Root")
        .unwrap()
        .as_reference()
        .unwrap();
    let catalog = reloaded.get_object(root_ref).unwrap().as_dict().unwrap();
    let outlines_ref = catalog.get(b"Outlines").unwrap().as_reference().unwrap();
    let outlines = reloaded
        .get_object(outlines_ref)
        .unwrap()
        .as_dict()
        .unwrap();
    let first_ref = outlines.get(b"First").unwrap().as_reference().unwrap();
    let first = reloaded.get_object(first_ref).unwrap().as_dict().unwrap();

    // Decode the /Title — must be UTF-16BE with BOM.
    let title_obj = first.get(b"Title").unwrap();
    let title_bytes: &[u8] = match title_obj {
        harumi::lopdf::Object::String(bytes, _) => bytes,
        _ => panic!("Expected String object for /Title"),
    };
    assert!(
        title_bytes.starts_with(&[0xFE, 0xFF]),
        "/Title must start with UTF-16BE BOM"
    );

    let units: Vec<u16> = title_bytes[2..]
        .chunks(2)
        .map(|c| u16::from_be_bytes([c[0], c.get(1).copied().unwrap_or(0)]))
        .collect();
    let decoded = String::from_utf16(&units).unwrap();
    assert_eq!(
        decoded, title,
        "UTF-16BE decoded title must match the original string"
    );
}

// ---------------------------------------------------------------------------
// Redact annotations
// ---------------------------------------------------------------------------

#[test]
fn redact_basic() {
    let pdf = minimal_pdf_bytes();
    let mut doc = Document::from_bytes(&pdf).unwrap();

    doc.page(1)
        .unwrap()
        .redact([100.0, 500.0, 200.0, 30.0])
        .unwrap();

    let bytes = doc.save_to_bytes().unwrap();
    let reloaded = harumi::lopdf::Document::load_from(bytes.as_slice()).unwrap();

    let page_ids = reloaded.get_pages();
    let page_id = *page_ids.get(&1).unwrap();
    let page_dict = reloaded.get_object(page_id).unwrap().as_dict().unwrap();
    assert!(
        page_dict.get(b"Annots").is_ok(),
        "/Annots must be present on page after redact"
    );
}

#[test]
fn redact_subtype_is_redact() {
    let pdf = minimal_pdf_bytes();
    let mut doc = Document::from_bytes(&pdf).unwrap();

    doc.page(1)
        .unwrap()
        .redact([50.0, 100.0, 150.0, 50.0])
        .unwrap();

    let bytes = doc.save_to_bytes().unwrap();
    let reloaded = harumi::lopdf::Document::load_from(bytes.as_slice()).unwrap();

    let page_ids = reloaded.get_pages();
    let page_id = *page_ids.get(&1).unwrap();
    let page_dict = reloaded.get_object(page_id).unwrap().as_dict().unwrap();
    let annots = page_dict.get(b"Annots").unwrap().as_array().unwrap();
    assert!(!annots.is_empty(), "Annots array must not be empty");

    let annot_ref = annots[0].as_reference().unwrap();
    let annot = reloaded.get_object(annot_ref).unwrap().as_dict().unwrap();
    let subtype = annot.get(b"Subtype").unwrap().as_name().unwrap();
    assert_eq!(subtype, b"Redact", "/Subtype must be /Redact");
}

#[test]
fn redact_has_appearance_stream() {
    let pdf = minimal_pdf_bytes();
    let mut doc = Document::from_bytes(&pdf).unwrap();

    doc.page(1)
        .unwrap()
        .redact([50.0, 100.0, 150.0, 50.0])
        .unwrap();

    let bytes = doc.save_to_bytes().unwrap();
    let reloaded = harumi::lopdf::Document::load_from(bytes.as_slice()).unwrap();

    let page_ids = reloaded.get_pages();
    let page_id = *page_ids.get(&1).unwrap();
    let page_dict = reloaded.get_object(page_id).unwrap().as_dict().unwrap();
    let annots = page_dict.get(b"Annots").unwrap().as_array().unwrap();
    let annot_ref = annots[0].as_reference().unwrap();
    let annot = reloaded.get_object(annot_ref).unwrap().as_dict().unwrap();

    let ap = annot.get(b"AP").unwrap().as_dict().unwrap();
    let ap_n_ref = ap.get(b"N").unwrap().as_reference().unwrap();
    let ap_stream = reloaded.get_object(ap_n_ref).unwrap();
    assert!(
        matches!(ap_stream, harumi::lopdf::Object::Stream(_)),
        "/AP /N must be a stream"
    );
}

#[test]
fn redact_zero_size_returns_error() {
    let pdf = minimal_pdf_bytes();
    let mut doc = Document::from_bytes(&pdf).unwrap();

    let result = doc.page(1).unwrap().redact([50.0, 100.0, 0.0, 50.0]);
    assert!(
        result.is_err(),
        "redact with zero width should return error"
    );

    let result = doc.page(1).unwrap().redact([50.0, 100.0, 150.0, 0.0]);
    assert!(
        result.is_err(),
        "redact with zero height should return error"
    );
}

#[test]
fn redact_nan_returns_error() {
    let pdf = minimal_pdf_bytes();
    let mut doc = Document::from_bytes(&pdf).unwrap();

    let result = doc.page(1).unwrap().redact([f32::NAN, 100.0, 150.0, 50.0]);
    assert!(
        result.is_err(),
        "redact with NaN coordinate should return error"
    );
}

#[test]
fn redact_infinity_returns_error() {
    let pdf = minimal_pdf_bytes();
    let mut doc = Document::from_bytes(&pdf).unwrap();

    let result = doc
        .page(1)
        .unwrap()
        .redact([f32::INFINITY, 100.0, 150.0, 50.0]);
    assert!(
        result.is_err(),
        "redact with Infinity coordinate should return error"
    );
}

#[test]
fn multiple_redacts_accumulate() {
    let pdf = minimal_pdf_bytes();
    let mut doc = Document::from_bytes(&pdf).unwrap();

    doc.page(1)
        .unwrap()
        .redact([50.0, 100.0, 150.0, 50.0])
        .unwrap();
    doc.page(1)
        .unwrap()
        .redact([100.0, 200.0, 200.0, 40.0])
        .unwrap();
    doc.page(1)
        .unwrap()
        .redact([200.0, 300.0, 100.0, 30.0])
        .unwrap();

    let bytes = doc.save_to_bytes().unwrap();
    let reloaded = harumi::lopdf::Document::load_from(bytes.as_slice()).unwrap();

    let page_ids = reloaded.get_pages();
    let page_id = *page_ids.get(&1).unwrap();
    let page_dict = reloaded.get_object(page_id).unwrap().as_dict().unwrap();
    let annots = page_dict.get(b"Annots").unwrap().as_array().unwrap();
    assert_eq!(
        annots.len(),
        3,
        "Three redact() calls should produce three annotations"
    );

    for annot_ref_obj in annots {
        let annot_ref = annot_ref_obj.as_reference().unwrap();
        let annot = reloaded.get_object(annot_ref).unwrap().as_dict().unwrap();
        let subtype = annot.get(b"Subtype").unwrap().as_name().unwrap();
        assert_eq!(subtype, b"Redact", "All annotations should be /Redact");
    }
}