pdf_oxide 0.3.38

The fastest Rust PDF library with text extraction: 0.8ms mean, 100% pass rate on 3,830 PDFs. 5× faster than pdf_extract, 17× faster than oxidize_pdf. Extract, create, and edit 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
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
//! Special annotations for PDF generation.
//!
//! This module provides support for special PDF annotation types:
//! - Popup (Section 12.5.6.14): Pop-up windows for other annotations
//! - Caret (Section 12.5.6.11): Insertion point markers
//! - FileAttachment (Section 12.5.6.15): Embedded file attachments
//! - Redact (Section 12.5.6.23): Redaction annotations
//!
//! # Example
//!
//! ```ignore
//! use pdf_oxide::writer::{CaretAnnotation, RedactAnnotation};
//!
//! // Create a caret for text insertion
//! let caret = CaretAnnotation::new(Rect::new(100.0, 700.0, 10.0, 10.0))
//!     .with_symbol(CaretSymbol::Paragraph);
//!
//! // Create a redact annotation
//! let redact = RedactAnnotation::new(Rect::new(72.0, 500.0, 200.0, 20.0))
//!     .with_overlay_text("REDACTED");
//! ```

use crate::annotation_types::{AnnotationColor, AnnotationFlags};
use crate::geometry::Rect;
use crate::object::{Object, ObjectRef};
use std::collections::HashMap;

// ============================================================================
// Popup Annotation (Section 12.5.6.14)
// ============================================================================

/// A Popup annotation per PDF spec Section 12.5.6.14.
///
/// Popup annotations do not appear alone; they are associated with a markup
/// annotation and are used to display text in a pop-up window.
#[derive(Debug, Clone)]
pub struct PopupAnnotation {
    /// Bounding rectangle for the popup window
    pub rect: Rect,
    /// Whether the popup is initially open
    pub open: bool,
    /// Annotation flags
    pub flags: AnnotationFlags,
}

impl PopupAnnotation {
    /// Create a new popup annotation.
    pub fn new(rect: Rect) -> Self {
        Self {
            rect,
            open: false,
            flags: AnnotationFlags::empty(),
        }
    }

    /// Create a popup that is initially open.
    pub fn open(rect: Rect) -> Self {
        Self {
            rect,
            open: true,
            flags: AnnotationFlags::empty(),
        }
    }

    /// Set whether the popup is initially open.
    pub fn with_open(mut self, open: bool) -> Self {
        self.open = open;
        self
    }

    /// Set annotation flags.
    pub fn with_flags(mut self, flags: AnnotationFlags) -> Self {
        self.flags = flags;
        self
    }

    /// Build the annotation dictionary.
    pub fn build(&self, _page_refs: &[ObjectRef]) -> HashMap<String, Object> {
        let mut dict = HashMap::new();

        dict.insert("Type".to_string(), Object::Name("Annot".to_string()));
        dict.insert("Subtype".to_string(), Object::Name("Popup".to_string()));

        // Rectangle
        dict.insert(
            "Rect".to_string(),
            Object::Array(vec![
                Object::Real(self.rect.x as f64),
                Object::Real(self.rect.y as f64),
                Object::Real((self.rect.x + self.rect.width) as f64),
                Object::Real((self.rect.y + self.rect.height) as f64),
            ]),
        );

        // Open flag
        dict.insert("Open".to_string(), Object::Boolean(self.open));

        // Flags
        if self.flags.bits() != 0 {
            dict.insert("F".to_string(), Object::Integer(self.flags.bits() as i64));
        }

        dict
    }

    /// Get the bounding rectangle.
    pub fn rect(&self) -> Rect {
        self.rect
    }
}

impl Default for PopupAnnotation {
    fn default() -> Self {
        Self::new(Rect::new(0.0, 0.0, 200.0, 150.0))
    }
}

// ============================================================================
// Caret Annotation (Section 12.5.6.11)
// ============================================================================

/// Caret symbol type for caret annotations.
#[derive(Debug, Clone, Copy, PartialEq, Default)]
pub enum CaretSymbol {
    /// No symbol (default)
    #[default]
    None,
    /// Paragraph symbol (¶)
    Paragraph,
}

impl CaretSymbol {
    /// Get the PDF name for this symbol.
    pub fn pdf_name(&self) -> &'static str {
        match self {
            CaretSymbol::None => "None",
            CaretSymbol::Paragraph => "P",
        }
    }
}

/// A Caret annotation per PDF spec Section 12.5.6.11.
///
/// Caret annotations mark a position in the document text where content
/// should be inserted or where a correction is needed.
#[derive(Debug, Clone)]
pub struct CaretAnnotation {
    /// Bounding rectangle for the caret
    pub rect: Rect,
    /// Symbol to display (None or Paragraph)
    pub symbol: CaretSymbol,
    /// Optional rectangle difference for the caret
    pub rd: Option<(f64, f64, f64, f64)>,
    /// Contents/comment
    pub contents: Option<String>,
    /// Author
    pub author: Option<String>,
    /// Subject
    pub subject: Option<String>,
    /// Color
    pub color: Option<AnnotationColor>,
    /// Annotation flags
    pub flags: AnnotationFlags,
}

impl CaretAnnotation {
    /// Create a new caret annotation.
    pub fn new(rect: Rect) -> Self {
        Self {
            rect,
            symbol: CaretSymbol::None,
            rd: None,
            contents: None,
            author: None,
            subject: None,
            color: Some(AnnotationColor::Rgb(0.0, 0.0, 1.0)), // Blue by default
            flags: AnnotationFlags::printable(),
        }
    }

    /// Create a caret with paragraph symbol.
    pub fn paragraph(rect: Rect) -> Self {
        Self {
            symbol: CaretSymbol::Paragraph,
            ..Self::new(rect)
        }
    }

    /// Set the caret symbol.
    pub fn with_symbol(mut self, symbol: CaretSymbol) -> Self {
        self.symbol = symbol;
        self
    }

    /// Set the rectangle difference (RD).
    pub fn with_rd(mut self, left: f64, bottom: f64, right: f64, top: f64) -> Self {
        self.rd = Some((left, bottom, right, top));
        self
    }

    /// Set contents/comment.
    pub fn with_contents(mut self, contents: impl Into<String>) -> Self {
        self.contents = Some(contents.into());
        self
    }

    /// Set the author.
    pub fn with_author(mut self, author: impl Into<String>) -> Self {
        self.author = Some(author.into());
        self
    }

    /// Set the subject.
    pub fn with_subject(mut self, subject: impl Into<String>) -> Self {
        self.subject = Some(subject.into());
        self
    }

    /// Set the color.
    pub fn with_color(mut self, color: AnnotationColor) -> Self {
        self.color = Some(color);
        self
    }

    /// Set annotation flags.
    pub fn with_flags(mut self, flags: AnnotationFlags) -> Self {
        self.flags = flags;
        self
    }

    /// Build the annotation dictionary.
    pub fn build(&self, _page_refs: &[ObjectRef]) -> HashMap<String, Object> {
        let mut dict = HashMap::new();

        dict.insert("Type".to_string(), Object::Name("Annot".to_string()));
        dict.insert("Subtype".to_string(), Object::Name("Caret".to_string()));

        // Rectangle
        dict.insert(
            "Rect".to_string(),
            Object::Array(vec![
                Object::Real(self.rect.x as f64),
                Object::Real(self.rect.y as f64),
                Object::Real((self.rect.x + self.rect.width) as f64),
                Object::Real((self.rect.y + self.rect.height) as f64),
            ]),
        );

        // Symbol (Sy)
        dict.insert("Sy".to_string(), Object::Name(self.symbol.pdf_name().to_string()));

        // Rectangle Difference (RD)
        if let Some((l, b, r, t)) = self.rd {
            dict.insert(
                "RD".to_string(),
                Object::Array(vec![
                    Object::Real(l),
                    Object::Real(b),
                    Object::Real(r),
                    Object::Real(t),
                ]),
            );
        }

        // Contents
        if let Some(ref contents) = self.contents {
            dict.insert("Contents".to_string(), Object::String(contents.as_bytes().to_vec()));
        }

        // Color
        if let Some(ref color) = self.color {
            if let Some(color_array) = color.to_array() {
                if !color_array.is_empty() {
                    dict.insert(
                        "C".to_string(),
                        Object::Array(
                            color_array
                                .into_iter()
                                .map(|v| Object::Real(v as f64))
                                .collect(),
                        ),
                    );
                }
            }
        }

        // Flags
        if self.flags.bits() != 0 {
            dict.insert("F".to_string(), Object::Integer(self.flags.bits() as i64));
        }

        // Author
        if let Some(ref author) = self.author {
            dict.insert("T".to_string(), Object::String(author.as_bytes().to_vec()));
        }

        // Subject
        if let Some(ref subject) = self.subject {
            dict.insert("Subj".to_string(), Object::String(subject.as_bytes().to_vec()));
        }

        dict
    }

    /// Get the bounding rectangle.
    pub fn rect(&self) -> Rect {
        self.rect
    }
}

impl Default for CaretAnnotation {
    fn default() -> Self {
        Self::new(Rect::new(0.0, 0.0, 10.0, 10.0))
    }
}

// ============================================================================
// FileAttachment Annotation (Section 12.5.6.15)
// ============================================================================

/// Icon type for file attachment annotations.
#[derive(Debug, Clone, Copy, PartialEq, Default)]
pub enum FileAttachmentIcon {
    /// Push pin icon (default)
    #[default]
    PushPin,
    /// Graph/chart icon
    Graph,
    /// Paperclip icon
    Paperclip,
    /// Tag icon
    Tag,
}

impl FileAttachmentIcon {
    /// Get the PDF name for this icon.
    pub fn pdf_name(&self) -> &'static str {
        match self {
            FileAttachmentIcon::PushPin => "PushPin",
            FileAttachmentIcon::Graph => "Graph",
            FileAttachmentIcon::Paperclip => "Paperclip",
            FileAttachmentIcon::Tag => "Tag",
        }
    }
}

/// A FileAttachment annotation per PDF spec Section 12.5.6.15.
///
/// File attachment annotations contain a reference to an embedded file.
#[derive(Debug, Clone)]
pub struct FileAttachmentAnnotation {
    /// Bounding rectangle for the icon
    pub rect: Rect,
    /// Icon to display
    pub icon: FileAttachmentIcon,
    /// File name
    pub file_name: String,
    /// Description of the file
    pub description: Option<String>,
    /// Contents/comment
    pub contents: Option<String>,
    /// Author
    pub author: Option<String>,
    /// Color
    pub color: Option<AnnotationColor>,
    /// Annotation flags
    pub flags: AnnotationFlags,
}

impl FileAttachmentAnnotation {
    /// Create a new file attachment annotation.
    pub fn new(rect: Rect, file_name: impl Into<String>) -> Self {
        Self {
            rect,
            icon: FileAttachmentIcon::PushPin,
            file_name: file_name.into(),
            description: None,
            contents: None,
            author: None,
            color: None,
            flags: AnnotationFlags::printable(),
        }
    }

    /// Set the icon.
    pub fn with_icon(mut self, icon: FileAttachmentIcon) -> Self {
        self.icon = icon;
        self
    }

    /// Set the file description.
    pub fn with_description(mut self, description: impl Into<String>) -> Self {
        self.description = Some(description.into());
        self
    }

    /// Set contents/comment.
    pub fn with_contents(mut self, contents: impl Into<String>) -> Self {
        self.contents = Some(contents.into());
        self
    }

    /// Set the author.
    pub fn with_author(mut self, author: impl Into<String>) -> Self {
        self.author = Some(author.into());
        self
    }

    /// Set the color.
    pub fn with_color(mut self, color: AnnotationColor) -> Self {
        self.color = Some(color);
        self
    }

    /// Set annotation flags.
    pub fn with_flags(mut self, flags: AnnotationFlags) -> Self {
        self.flags = flags;
        self
    }

    /// Build the annotation dictionary.
    ///
    /// Note: This creates a basic file specification. The actual file embedding
    /// requires additional structure in the PDF that should be handled by PdfWriter.
    pub fn build(&self, _page_refs: &[ObjectRef]) -> HashMap<String, Object> {
        let mut dict = HashMap::new();

        dict.insert("Type".to_string(), Object::Name("Annot".to_string()));
        dict.insert("Subtype".to_string(), Object::Name("FileAttachment".to_string()));

        // Rectangle
        dict.insert(
            "Rect".to_string(),
            Object::Array(vec![
                Object::Real(self.rect.x as f64),
                Object::Real(self.rect.y as f64),
                Object::Real((self.rect.x + self.rect.width) as f64),
                Object::Real((self.rect.y + self.rect.height) as f64),
            ]),
        );

        // Icon name (Name)
        dict.insert("Name".to_string(), Object::Name(self.icon.pdf_name().to_string()));

        // File Specification (FS) - simplified version
        let mut fs_dict = HashMap::new();
        fs_dict.insert("Type".to_string(), Object::Name("Filespec".to_string()));
        fs_dict.insert("F".to_string(), Object::String(self.file_name.as_bytes().to_vec()));
        if let Some(ref desc) = self.description {
            fs_dict.insert("Desc".to_string(), Object::String(desc.as_bytes().to_vec()));
        }
        dict.insert("FS".to_string(), Object::Dictionary(fs_dict));

        // Contents
        if let Some(ref contents) = self.contents {
            dict.insert("Contents".to_string(), Object::String(contents.as_bytes().to_vec()));
        }

        // Color
        if let Some(ref color) = self.color {
            if let Some(color_array) = color.to_array() {
                if !color_array.is_empty() {
                    dict.insert(
                        "C".to_string(),
                        Object::Array(
                            color_array
                                .into_iter()
                                .map(|v| Object::Real(v as f64))
                                .collect(),
                        ),
                    );
                }
            }
        }

        // Flags
        if self.flags.bits() != 0 {
            dict.insert("F".to_string(), Object::Integer(self.flags.bits() as i64));
        }

        // Author
        if let Some(ref author) = self.author {
            dict.insert("T".to_string(), Object::String(author.as_bytes().to_vec()));
        }

        dict
    }

    /// Get the bounding rectangle.
    pub fn rect(&self) -> Rect {
        self.rect
    }
}

// ============================================================================
// Redact Annotation (Section 12.5.6.23)
// ============================================================================

/// A Redact annotation per PDF spec Section 12.5.6.23.
///
/// Redact annotations mark content that is intended to be permanently removed.
/// When applied, the marked content is deleted and optionally replaced with
/// overlay text or a colored box.
#[derive(Debug, Clone)]
pub struct RedactAnnotation {
    /// Bounding rectangle
    pub rect: Rect,
    /// QuadPoints defining precise redaction areas
    pub quad_points: Option<Vec<[f64; 8]>>,
    /// Interior color (fill color after redaction)
    pub interior_color: Option<AnnotationColor>,
    /// Overlay text to display after redaction
    pub overlay_text: Option<String>,
    /// Alignment of overlay text (0=left, 1=center, 2=right)
    pub overlay_text_alignment: i32,
    /// Default appearance for overlay text
    pub default_appearance: Option<String>,
    /// Contents/comment
    pub contents: Option<String>,
    /// Author
    pub author: Option<String>,
    /// Subject
    pub subject: Option<String>,
    /// Annotation flags
    pub flags: AnnotationFlags,
}

impl RedactAnnotation {
    /// Create a new redact annotation.
    pub fn new(rect: Rect) -> Self {
        Self {
            rect,
            quad_points: None,
            interior_color: Some(AnnotationColor::black()),
            overlay_text: None,
            overlay_text_alignment: 0,
            default_appearance: None,
            contents: None,
            author: None,
            subject: None,
            flags: AnnotationFlags::new(AnnotationFlags::PRINT | AnnotationFlags::LOCKED),
        }
    }

    /// Create a redact annotation with quad points.
    pub fn with_quads(rect: Rect, quad_points: Vec<[f64; 8]>) -> Self {
        Self {
            quad_points: Some(quad_points),
            ..Self::new(rect)
        }
    }

    /// Set the interior/fill color.
    pub fn with_interior_color(mut self, color: AnnotationColor) -> Self {
        self.interior_color = Some(color);
        self
    }

    /// Set the overlay text.
    pub fn with_overlay_text(mut self, text: impl Into<String>) -> Self {
        self.overlay_text = Some(text.into());
        self
    }

    /// Set the overlay text alignment (0=left, 1=center, 2=right).
    pub fn with_overlay_text_alignment(mut self, alignment: i32) -> Self {
        self.overlay_text_alignment = alignment.clamp(0, 2);
        self
    }

    /// Set the default appearance string.
    pub fn with_default_appearance(mut self, da: impl Into<String>) -> Self {
        self.default_appearance = Some(da.into());
        self
    }

    /// Set contents/comment.
    pub fn with_contents(mut self, contents: impl Into<String>) -> Self {
        self.contents = Some(contents.into());
        self
    }

    /// Set the author.
    pub fn with_author(mut self, author: impl Into<String>) -> Self {
        self.author = Some(author.into());
        self
    }

    /// Set the subject.
    pub fn with_subject(mut self, subject: impl Into<String>) -> Self {
        self.subject = Some(subject.into());
        self
    }

    /// Set annotation flags.
    pub fn with_flags(mut self, flags: AnnotationFlags) -> Self {
        self.flags = flags;
        self
    }

    /// Build the annotation dictionary.
    pub fn build(&self, _page_refs: &[ObjectRef]) -> HashMap<String, Object> {
        let mut dict = HashMap::new();

        dict.insert("Type".to_string(), Object::Name("Annot".to_string()));
        dict.insert("Subtype".to_string(), Object::Name("Redact".to_string()));

        // Rectangle
        dict.insert(
            "Rect".to_string(),
            Object::Array(vec![
                Object::Real(self.rect.x as f64),
                Object::Real(self.rect.y as f64),
                Object::Real((self.rect.x + self.rect.width) as f64),
                Object::Real((self.rect.y + self.rect.height) as f64),
            ]),
        );

        // QuadPoints
        if let Some(ref quads) = self.quad_points {
            let mut all_points = Vec::new();
            for quad in quads {
                for point in quad {
                    all_points.push(Object::Real(*point));
                }
            }
            dict.insert("QuadPoints".to_string(), Object::Array(all_points));
        }

        // Interior color (IC)
        if let Some(ref color) = self.interior_color {
            if let Some(color_array) = color.to_array() {
                dict.insert(
                    "IC".to_string(),
                    Object::Array(
                        color_array
                            .into_iter()
                            .map(|v| Object::Real(v as f64))
                            .collect(),
                    ),
                );
            }
        }

        // Overlay text
        if let Some(ref text) = self.overlay_text {
            dict.insert("OverlayText".to_string(), Object::String(text.as_bytes().to_vec()));
        }

        // Overlay text alignment (Q)
        if self.overlay_text_alignment != 0 {
            dict.insert("Q".to_string(), Object::Integer(self.overlay_text_alignment as i64));
        }

        // Default appearance
        if let Some(ref da) = self.default_appearance {
            dict.insert("DA".to_string(), Object::String(da.as_bytes().to_vec()));
        }

        // Contents
        if let Some(ref contents) = self.contents {
            dict.insert("Contents".to_string(), Object::String(contents.as_bytes().to_vec()));
        }

        // Flags
        if self.flags.bits() != 0 {
            dict.insert("F".to_string(), Object::Integer(self.flags.bits() as i64));
        }

        // Author
        if let Some(ref author) = self.author {
            dict.insert("T".to_string(), Object::String(author.as_bytes().to_vec()));
        }

        // Subject
        if let Some(ref subject) = self.subject {
            dict.insert("Subj".to_string(), Object::String(subject.as_bytes().to_vec()));
        }

        dict
    }

    /// Get the bounding rectangle.
    pub fn rect(&self) -> Rect {
        self.rect
    }
}

impl Default for RedactAnnotation {
    fn default() -> Self {
        Self::new(Rect::new(0.0, 0.0, 100.0, 20.0))
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    // Popup tests
    #[test]
    fn test_popup_new() {
        let rect = Rect::new(300.0, 500.0, 200.0, 150.0);
        let popup = PopupAnnotation::new(rect);

        assert_eq!(popup.rect, rect);
        assert!(!popup.open);
    }

    #[test]
    fn test_popup_open() {
        let rect = Rect::new(300.0, 500.0, 200.0, 150.0);
        let popup = PopupAnnotation::open(rect);

        assert!(popup.open);
    }

    #[test]
    fn test_popup_build() {
        let popup = PopupAnnotation::new(Rect::new(100.0, 100.0, 200.0, 150.0));

        let dict = popup.build(&[]);

        assert_eq!(dict.get("Subtype"), Some(&Object::Name("Popup".to_string())));
        assert_eq!(dict.get("Open"), Some(&Object::Boolean(false)));
    }

    // Caret tests
    #[test]
    fn test_caret_new() {
        let rect = Rect::new(100.0, 700.0, 10.0, 10.0);
        let caret = CaretAnnotation::new(rect);

        assert_eq!(caret.rect, rect);
        assert_eq!(caret.symbol, CaretSymbol::None);
    }

    #[test]
    fn test_caret_paragraph() {
        let rect = Rect::new(100.0, 700.0, 10.0, 10.0);
        let caret = CaretAnnotation::paragraph(rect);

        assert_eq!(caret.symbol, CaretSymbol::Paragraph);
    }

    #[test]
    fn test_caret_symbol_names() {
        assert_eq!(CaretSymbol::None.pdf_name(), "None");
        assert_eq!(CaretSymbol::Paragraph.pdf_name(), "P");
    }

    #[test]
    fn test_caret_build() {
        let caret =
            CaretAnnotation::new(Rect::new(100.0, 700.0, 10.0, 10.0)).with_contents("Insert here");

        let dict = caret.build(&[]);

        assert_eq!(dict.get("Subtype"), Some(&Object::Name("Caret".to_string())));
        assert_eq!(dict.get("Sy"), Some(&Object::Name("None".to_string())));
        assert!(dict.contains_key("Contents"));
    }

    #[test]
    fn test_caret_fluent_builder() {
        let caret = CaretAnnotation::new(Rect::new(100.0, 700.0, 10.0, 10.0))
            .with_symbol(CaretSymbol::Paragraph)
            .with_contents("Insert paragraph")
            .with_author("Editor");

        assert_eq!(caret.symbol, CaretSymbol::Paragraph);
        assert_eq!(caret.contents, Some("Insert paragraph".to_string()));
        assert_eq!(caret.author, Some("Editor".to_string()));
    }

    // FileAttachment tests
    #[test]
    fn test_file_attachment_new() {
        let rect = Rect::new(72.0, 600.0, 24.0, 24.0);
        let file = FileAttachmentAnnotation::new(rect, "document.pdf");

        assert_eq!(file.rect, rect);
        assert_eq!(file.file_name, "document.pdf");
        assert_eq!(file.icon, FileAttachmentIcon::PushPin);
    }

    #[test]
    fn test_file_attachment_icons() {
        assert_eq!(FileAttachmentIcon::PushPin.pdf_name(), "PushPin");
        assert_eq!(FileAttachmentIcon::Graph.pdf_name(), "Graph");
        assert_eq!(FileAttachmentIcon::Paperclip.pdf_name(), "Paperclip");
        assert_eq!(FileAttachmentIcon::Tag.pdf_name(), "Tag");
    }

    #[test]
    fn test_file_attachment_build() {
        let file = FileAttachmentAnnotation::new(Rect::new(72.0, 600.0, 24.0, 24.0), "report.xlsx")
            .with_icon(FileAttachmentIcon::Paperclip)
            .with_description("Monthly report");

        let dict = file.build(&[]);

        assert_eq!(dict.get("Subtype"), Some(&Object::Name("FileAttachment".to_string())));
        assert_eq!(dict.get("Name"), Some(&Object::Name("Paperclip".to_string())));
        assert!(dict.contains_key("FS"));
    }

    // Redact tests
    #[test]
    fn test_redact_new() {
        let rect = Rect::new(72.0, 500.0, 200.0, 20.0);
        let redact = RedactAnnotation::new(rect);

        assert_eq!(redact.rect, rect);
        assert!(redact.interior_color.is_some());
    }

    #[test]
    fn test_redact_with_overlay() {
        let redact = RedactAnnotation::new(Rect::new(72.0, 500.0, 200.0, 20.0))
            .with_overlay_text("REDACTED")
            .with_overlay_text_alignment(1); // Center

        assert_eq!(redact.overlay_text, Some("REDACTED".to_string()));
        assert_eq!(redact.overlay_text_alignment, 1);
    }

    #[test]
    fn test_redact_build() {
        let redact = RedactAnnotation::new(Rect::new(72.0, 500.0, 200.0, 20.0))
            .with_overlay_text("CONFIDENTIAL")
            .with_interior_color(AnnotationColor::black());

        let dict = redact.build(&[]);

        assert_eq!(dict.get("Subtype"), Some(&Object::Name("Redact".to_string())));
        assert!(dict.contains_key("IC"));
        assert!(dict.contains_key("OverlayText"));
    }

    #[test]
    fn test_redact_with_quads() {
        let rect = Rect::new(72.0, 500.0, 200.0, 20.0);
        let quads = vec![[72.0, 500.0, 272.0, 500.0, 272.0, 520.0, 72.0, 520.0]];
        let redact = RedactAnnotation::with_quads(rect, quads);

        assert!(redact.quad_points.is_some());

        let dict = redact.build(&[]);
        assert!(dict.contains_key("QuadPoints"));
    }
}