oxidize-pdf 2.5.1

A pure Rust PDF generation and manipulation library with zero external dependencies
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
//! Viewer preferences control how the PDF document is displayed in the viewer
//!
//! These preferences are defined in ISO 32000-1:2008 and provide control over
//! the user interface and display behavior when the document is opened.

use crate::objects::{Dictionary, Object};

/// Page layout modes
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum PageLayout {
    /// Display one page at a time
    SinglePage,
    /// Display pages in one column
    OneColumn,
    /// Display pages in two columns, odd-numbered pages on the left
    TwoColumnLeft,
    /// Display pages in two columns, odd-numbered pages on the right  
    TwoColumnRight,
    /// Display pages in two columns, cover page displayed alone
    TwoPageLeft,
    /// Display pages in two columns, cover page displayed alone
    TwoPageRight,
}

impl PageLayout {
    /// Convert to PDF name
    pub fn to_pdf_name(&self) -> &'static str {
        match self {
            PageLayout::SinglePage => "SinglePage",
            PageLayout::OneColumn => "OneColumn",
            PageLayout::TwoColumnLeft => "TwoColumnLeft",
            PageLayout::TwoColumnRight => "TwoColumnRight",
            PageLayout::TwoPageLeft => "TwoPageLeft",
            PageLayout::TwoPageRight => "TwoPageRight",
        }
    }
}

/// Page mode - how to display the document when opened
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum PageMode {
    /// Neither document outline nor thumbnail images visible
    UseNone,
    /// Document outline visible
    UseOutlines,
    /// Thumbnail images visible
    UseThumbs,
    /// Full-screen mode, hiding all menu bars, window controls, etc.
    FullScreen,
    /// Optional content group panel visible
    UseOC,
    /// Attachments panel visible
    UseAttachments,
}

impl PageMode {
    /// Convert to PDF name
    pub fn to_pdf_name(&self) -> &'static str {
        match self {
            PageMode::UseNone => "UseNone",
            PageMode::UseOutlines => "UseOutlines",
            PageMode::UseThumbs => "UseThumbs",
            PageMode::FullScreen => "FullScreen",
            PageMode::UseOC => "UseOC",
            PageMode::UseAttachments => "UseAttachments",
        }
    }
}

/// Non-full-screen page mode
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum NonFullScreenPageMode {
    /// Neither document outline nor thumbnail images visible
    UseNone,
    /// Document outline visible
    UseOutlines,
    /// Thumbnail images visible
    UseThumbs,
    /// Optional content group panel visible
    UseOC,
}

impl NonFullScreenPageMode {
    /// Convert to PDF name
    pub fn to_pdf_name(&self) -> &'static str {
        match self {
            NonFullScreenPageMode::UseNone => "UseNone",
            NonFullScreenPageMode::UseOutlines => "UseOutlines",
            NonFullScreenPageMode::UseThumbs => "UseThumbs",
            NonFullScreenPageMode::UseOC => "UseOC",
        }
    }
}

/// Direction for reading order
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum Direction {
    /// Left to right
    L2R,
    /// Right to left
    R2L,
}

impl Direction {
    /// Convert to PDF name
    pub fn to_pdf_name(&self) -> &'static str {
        match self {
            Direction::L2R => "L2R",
            Direction::R2L => "R2L",
        }
    }
}

/// Print scaling options
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum PrintScaling {
    /// No scaling
    None,
    /// Scale to fit page
    AppDefault,
}

impl PrintScaling {
    /// Convert to PDF name
    pub fn to_pdf_name(&self) -> &'static str {
        match self {
            PrintScaling::None => "None",
            PrintScaling::AppDefault => "AppDefault",
        }
    }
}

/// Duplex printing modes
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum Duplex {
    /// Single-sided printing
    Simplex,
    /// Double-sided printing, flip on short edge
    DuplexFlipShortEdge,
    /// Double-sided printing, flip on long edge
    DuplexFlipLongEdge,
}

impl Duplex {
    /// Convert to PDF name
    pub fn to_pdf_name(&self) -> &'static str {
        match self {
            Duplex::Simplex => "Simplex",
            Duplex::DuplexFlipShortEdge => "DuplexFlipShortEdge",
            Duplex::DuplexFlipLongEdge => "DuplexFlipLongEdge",
        }
    }
}

/// Viewer preferences for controlling document display
#[derive(Debug, Clone, Default)]
pub struct ViewerPreferences {
    /// Hide the application's tool bars
    pub hide_toolbar: Option<bool>,
    /// Hide the application's menu bar
    pub hide_menubar: Option<bool>,
    /// Hide user interface elements like scroll bars, navigation controls
    pub hide_window_ui: Option<bool>,
    /// Resize document window to fit the size of the first displayed page
    pub fit_window: Option<bool>,
    /// Center the document window on the screen
    pub center_window: Option<bool>,
    /// Display document title instead of filename in window title bar
    pub display_doc_title: Option<bool>,
    /// Page layout to use when document is opened
    pub page_layout: Option<PageLayout>,
    /// How to display document when opened
    pub page_mode: Option<PageMode>,
    /// Page mode after exiting full-screen mode
    pub non_full_screen_page_mode: Option<NonFullScreenPageMode>,
    /// Reading order direction
    pub direction: Option<Direction>,
    /// Area of default page to display when document is opened
    pub view_area: Option<String>,
    /// Area of page to use for clipping when displaying
    pub view_clip: Option<String>,
    /// Area to use for printing
    pub print_area: Option<String>,
    /// Area to use for clipping when printing
    pub print_clip: Option<String>,
    /// Print scaling mode
    pub print_scaling: Option<PrintScaling>,
    /// Duplex printing mode
    pub duplex: Option<Duplex>,
    /// Page ranges for printing
    pub print_page_range: Option<Vec<(u32, u32)>>,
    /// Number of copies
    pub num_copies: Option<u32>,
    /// Whether to pick tray by PDF size
    pub pick_tray_by_pdf_size: Option<bool>,
}

impl ViewerPreferences {
    /// Create new viewer preferences with default values
    pub fn new() -> Self {
        ViewerPreferences::default()
    }

    /// Hide toolbar
    pub fn hide_toolbar(mut self, hide: bool) -> Self {
        self.hide_toolbar = Some(hide);
        self
    }

    /// Hide menubar
    pub fn hide_menubar(mut self, hide: bool) -> Self {
        self.hide_menubar = Some(hide);
        self
    }

    /// Hide window UI elements
    pub fn hide_window_ui(mut self, hide: bool) -> Self {
        self.hide_window_ui = Some(hide);
        self
    }

    /// Fit window to page size
    pub fn fit_window(mut self, fit: bool) -> Self {
        self.fit_window = Some(fit);
        self
    }

    /// Center window on screen
    pub fn center_window(mut self, center: bool) -> Self {
        self.center_window = Some(center);
        self
    }

    /// Display document title in window title bar
    pub fn display_doc_title(mut self, display: bool) -> Self {
        self.display_doc_title = Some(display);
        self
    }

    /// Set page layout
    pub fn page_layout(mut self, layout: PageLayout) -> Self {
        self.page_layout = Some(layout);
        self
    }

    /// Set page mode
    pub fn page_mode(mut self, mode: PageMode) -> Self {
        self.page_mode = Some(mode);
        self
    }

    /// Set non-full-screen page mode
    pub fn non_full_screen_page_mode(mut self, mode: NonFullScreenPageMode) -> Self {
        self.non_full_screen_page_mode = Some(mode);
        self
    }

    /// Set reading direction
    pub fn direction(mut self, direction: Direction) -> Self {
        self.direction = Some(direction);
        self
    }

    /// Set print scaling
    pub fn print_scaling(mut self, scaling: PrintScaling) -> Self {
        self.print_scaling = Some(scaling);
        self
    }

    /// Set duplex mode
    pub fn duplex(mut self, duplex: Duplex) -> Self {
        self.duplex = Some(duplex);
        self
    }

    /// Set number of copies for printing
    pub fn num_copies(mut self, copies: u32) -> Self {
        self.num_copies = Some(copies.max(1));
        self
    }

    /// Pick tray by PDF size
    pub fn pick_tray_by_pdf_size(mut self, pick: bool) -> Self {
        self.pick_tray_by_pdf_size = Some(pick);
        self
    }

    /// Add page range for printing
    pub fn add_print_page_range(mut self, start: u32, end: u32) -> Self {
        if self.print_page_range.is_none() {
            self.print_page_range = Some(Vec::new());
        }
        if let Some(ref mut ranges) = self.print_page_range {
            ranges.push((start.min(end), start.max(end)));
        }
        self
    }

    /// Convert to PDF dictionary
    pub fn to_dict(&self) -> Dictionary {
        let mut dict = Dictionary::new();

        if let Some(hide) = self.hide_toolbar {
            dict.set("HideToolbar", Object::Boolean(hide));
        }

        if let Some(hide) = self.hide_menubar {
            dict.set("HideMenubar", Object::Boolean(hide));
        }

        if let Some(hide) = self.hide_window_ui {
            dict.set("HideWindowUI", Object::Boolean(hide));
        }

        if let Some(fit) = self.fit_window {
            dict.set("FitWindow", Object::Boolean(fit));
        }

        if let Some(center) = self.center_window {
            dict.set("CenterWindow", Object::Boolean(center));
        }

        if let Some(display) = self.display_doc_title {
            dict.set("DisplayDocTitle", Object::Boolean(display));
        }

        if let Some(layout) = self.page_layout {
            dict.set("PageLayout", Object::Name(layout.to_pdf_name().to_string()));
        }

        if let Some(mode) = self.page_mode {
            dict.set("PageMode", Object::Name(mode.to_pdf_name().to_string()));
        }

        if let Some(mode) = self.non_full_screen_page_mode {
            dict.set(
                "NonFullScreenPageMode",
                Object::Name(mode.to_pdf_name().to_string()),
            );
        }

        if let Some(direction) = self.direction {
            dict.set(
                "Direction",
                Object::Name(direction.to_pdf_name().to_string()),
            );
        }

        if let Some(ref area) = self.view_area {
            dict.set("ViewArea", Object::Name(area.clone()));
        }

        if let Some(ref clip) = self.view_clip {
            dict.set("ViewClip", Object::Name(clip.clone()));
        }

        if let Some(ref area) = self.print_area {
            dict.set("PrintArea", Object::Name(area.clone()));
        }

        if let Some(ref clip) = self.print_clip {
            dict.set("PrintClip", Object::Name(clip.clone()));
        }

        if let Some(scaling) = self.print_scaling {
            dict.set(
                "PrintScaling",
                Object::Name(scaling.to_pdf_name().to_string()),
            );
        }

        if let Some(duplex) = self.duplex {
            dict.set("Duplex", Object::Name(duplex.to_pdf_name().to_string()));
        }

        if let Some(ref ranges) = self.print_page_range {
            let range_array: Vec<Object> = ranges
                .iter()
                .flat_map(|(start, end)| {
                    vec![Object::Integer(*start as i64), Object::Integer(*end as i64)]
                })
                .collect();
            dict.set("PrintPageRange", Object::Array(range_array));
        }

        if let Some(copies) = self.num_copies {
            dict.set("NumCopies", Object::Integer(copies as i64));
        }

        if let Some(pick) = self.pick_tray_by_pdf_size {
            dict.set("PickTrayByPDFSize", Object::Boolean(pick));
        }

        dict
    }

    // Convenience constructors

    /// Create preferences for presentation mode
    pub fn presentation() -> Self {
        ViewerPreferences::new()
            .page_mode(PageMode::FullScreen)
            .hide_toolbar(true)
            .hide_menubar(true)
            .hide_window_ui(true)
            .fit_window(true)
            .center_window(true)
    }

    /// Create preferences for reading mode
    pub fn reading() -> Self {
        ViewerPreferences::new()
            .page_layout(PageLayout::TwoColumnRight)
            .page_mode(PageMode::UseOutlines)
            .fit_window(true)
            .display_doc_title(true)
    }

    /// Create preferences for printing
    pub fn printing() -> Self {
        ViewerPreferences::new()
            .print_scaling(PrintScaling::None)
            .duplex(Duplex::DuplexFlipLongEdge)
            .pick_tray_by_pdf_size(true)
    }

    /// Create minimal UI preferences
    pub fn minimal_ui() -> Self {
        ViewerPreferences::new()
            .hide_toolbar(true)
            .hide_menubar(true)
            .center_window(true)
            .fit_window(true)
    }
}

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

    #[test]
    fn test_page_layout_names() {
        assert_eq!(PageLayout::SinglePage.to_pdf_name(), "SinglePage");
        assert_eq!(PageLayout::OneColumn.to_pdf_name(), "OneColumn");
        assert_eq!(PageLayout::TwoColumnLeft.to_pdf_name(), "TwoColumnLeft");
        assert_eq!(PageLayout::TwoColumnRight.to_pdf_name(), "TwoColumnRight");
        assert_eq!(PageLayout::TwoPageLeft.to_pdf_name(), "TwoPageLeft");
        assert_eq!(PageLayout::TwoPageRight.to_pdf_name(), "TwoPageRight");
    }

    #[test]
    fn test_page_mode_names() {
        assert_eq!(PageMode::UseNone.to_pdf_name(), "UseNone");
        assert_eq!(PageMode::UseOutlines.to_pdf_name(), "UseOutlines");
        assert_eq!(PageMode::UseThumbs.to_pdf_name(), "UseThumbs");
        assert_eq!(PageMode::FullScreen.to_pdf_name(), "FullScreen");
        assert_eq!(PageMode::UseOC.to_pdf_name(), "UseOC");
        assert_eq!(PageMode::UseAttachments.to_pdf_name(), "UseAttachments");
    }

    #[test]
    fn test_basic_preferences() {
        let prefs = ViewerPreferences::new()
            .hide_toolbar(true)
            .hide_menubar(true)
            .fit_window(true);

        let dict = prefs.to_dict();
        assert_eq!(dict.get("HideToolbar"), Some(&Object::Boolean(true)));
        assert_eq!(dict.get("HideMenubar"), Some(&Object::Boolean(true)));
        assert_eq!(dict.get("FitWindow"), Some(&Object::Boolean(true)));
    }

    #[test]
    fn test_page_layout_preference() {
        let prefs = ViewerPreferences::new().page_layout(PageLayout::TwoColumnLeft);

        let dict = prefs.to_dict();
        assert_eq!(
            dict.get("PageLayout"),
            Some(&Object::Name("TwoColumnLeft".to_string()))
        );
    }

    #[test]
    fn test_print_preferences() {
        let prefs = ViewerPreferences::new()
            .print_scaling(PrintScaling::None)
            .duplex(Duplex::DuplexFlipLongEdge)
            .num_copies(3);

        let dict = prefs.to_dict();
        assert_eq!(
            dict.get("PrintScaling"),
            Some(&Object::Name("None".to_string()))
        );
        assert_eq!(
            dict.get("Duplex"),
            Some(&Object::Name("DuplexFlipLongEdge".to_string()))
        );
        assert_eq!(dict.get("NumCopies"), Some(&Object::Integer(3)));
    }

    #[test]
    fn test_print_page_ranges() {
        let prefs = ViewerPreferences::new()
            .add_print_page_range(1, 5)
            .add_print_page_range(10, 15);

        let dict = prefs.to_dict();
        if let Some(Object::Array(ranges)) = dict.get("PrintPageRange") {
            assert_eq!(ranges.len(), 4); // Two ranges, each with start and end
            assert_eq!(ranges[0], Object::Integer(1));
            assert_eq!(ranges[1], Object::Integer(5));
            assert_eq!(ranges[2], Object::Integer(10));
            assert_eq!(ranges[3], Object::Integer(15));
        } else {
            panic!("Expected PrintPageRange array");
        }
    }

    #[test]
    fn test_convenience_constructors() {
        let presentation = ViewerPreferences::presentation();
        assert_eq!(presentation.page_mode, Some(PageMode::FullScreen));
        assert_eq!(presentation.hide_toolbar, Some(true));

        let reading = ViewerPreferences::reading();
        assert_eq!(reading.page_layout, Some(PageLayout::TwoColumnRight));
        assert_eq!(reading.page_mode, Some(PageMode::UseOutlines));

        let printing = ViewerPreferences::printing();
        assert_eq!(printing.print_scaling, Some(PrintScaling::None));
        assert_eq!(printing.duplex, Some(Duplex::DuplexFlipLongEdge));

        let minimal = ViewerPreferences::minimal_ui();
        assert_eq!(minimal.hide_toolbar, Some(true));
        assert_eq!(minimal.hide_menubar, Some(true));
    }

    #[test]
    fn test_num_copies_bounds() {
        let prefs = ViewerPreferences::new().num_copies(0);
        assert_eq!(prefs.num_copies, Some(1)); // Should be clamped to minimum 1
    }

    #[test]
    fn test_direction() {
        assert_eq!(Direction::L2R.to_pdf_name(), "L2R");
        assert_eq!(Direction::R2L.to_pdf_name(), "R2L");
    }

    #[test]
    fn test_print_scaling() {
        assert_eq!(PrintScaling::None.to_pdf_name(), "None");
        assert_eq!(PrintScaling::AppDefault.to_pdf_name(), "AppDefault");
    }

    #[test]
    fn test_duplex_modes() {
        assert_eq!(Duplex::Simplex.to_pdf_name(), "Simplex");
        assert_eq!(
            Duplex::DuplexFlipShortEdge.to_pdf_name(),
            "DuplexFlipShortEdge"
        );
        assert_eq!(
            Duplex::DuplexFlipLongEdge.to_pdf_name(),
            "DuplexFlipLongEdge"
        );
    }

    #[test]
    fn test_empty_preferences() {
        let prefs = ViewerPreferences::new();
        let dict = prefs.to_dict();
        assert!(dict.is_empty()); // No preferences set, should result in empty dictionary
    }

    #[test]
    fn test_non_full_screen_page_mode_names() {
        assert_eq!(NonFullScreenPageMode::UseNone.to_pdf_name(), "UseNone");
        assert_eq!(
            NonFullScreenPageMode::UseOutlines.to_pdf_name(),
            "UseOutlines"
        );
        assert_eq!(NonFullScreenPageMode::UseThumbs.to_pdf_name(), "UseThumbs");
        assert_eq!(NonFullScreenPageMode::UseOC.to_pdf_name(), "UseOC");
    }

    #[test]
    fn test_non_full_screen_page_mode_to_dict() {
        let prefs =
            ViewerPreferences::new().non_full_screen_page_mode(NonFullScreenPageMode::UseOutlines);
        let dict = prefs.to_dict();
        assert_eq!(
            dict.get("NonFullScreenPageMode"),
            Some(&Object::Name("UseOutlines".to_string()))
        );
    }

    #[test]
    fn test_direction_to_dict() {
        let prefs_l2r = ViewerPreferences::new().direction(Direction::L2R);
        let dict_l2r = prefs_l2r.to_dict();
        assert_eq!(
            dict_l2r.get("Direction"),
            Some(&Object::Name("L2R".to_string()))
        );

        let prefs_r2l = ViewerPreferences::new().direction(Direction::R2L);
        let dict_r2l = prefs_r2l.to_dict();
        assert_eq!(
            dict_r2l.get("Direction"),
            Some(&Object::Name("R2L".to_string()))
        );
    }

    #[test]
    fn test_view_area_and_clip() {
        let mut prefs = ViewerPreferences::new();
        prefs.view_area = Some("MediaBox".to_string());
        prefs.view_clip = Some("CropBox".to_string());

        let dict = prefs.to_dict();
        assert_eq!(
            dict.get("ViewArea"),
            Some(&Object::Name("MediaBox".to_string()))
        );
        assert_eq!(
            dict.get("ViewClip"),
            Some(&Object::Name("CropBox".to_string()))
        );
    }

    #[test]
    fn test_print_area_and_clip() {
        let mut prefs = ViewerPreferences::new();
        prefs.print_area = Some("BleedBox".to_string());
        prefs.print_clip = Some("TrimBox".to_string());

        let dict = prefs.to_dict();
        assert_eq!(
            dict.get("PrintArea"),
            Some(&Object::Name("BleedBox".to_string()))
        );
        assert_eq!(
            dict.get("PrintClip"),
            Some(&Object::Name("TrimBox".to_string()))
        );
    }

    #[test]
    fn test_pick_tray_by_pdf_size_to_dict() {
        let prefs = ViewerPreferences::new().pick_tray_by_pdf_size(true);
        let dict = prefs.to_dict();
        assert_eq!(dict.get("PickTrayByPDFSize"), Some(&Object::Boolean(true)));

        let prefs_false = ViewerPreferences::new().pick_tray_by_pdf_size(false);
        let dict_false = prefs_false.to_dict();
        assert_eq!(
            dict_false.get("PickTrayByPDFSize"),
            Some(&Object::Boolean(false))
        );
    }

    #[test]
    fn test_hide_window_ui_to_dict() {
        let prefs = ViewerPreferences::new().hide_window_ui(true);
        let dict = prefs.to_dict();
        assert_eq!(dict.get("HideWindowUI"), Some(&Object::Boolean(true)));
    }

    #[test]
    fn test_center_window_to_dict() {
        let prefs = ViewerPreferences::new().center_window(true);
        let dict = prefs.to_dict();
        assert_eq!(dict.get("CenterWindow"), Some(&Object::Boolean(true)));
    }

    #[test]
    fn test_display_doc_title_to_dict() {
        let prefs = ViewerPreferences::new().display_doc_title(true);
        let dict = prefs.to_dict();
        assert_eq!(dict.get("DisplayDocTitle"), Some(&Object::Boolean(true)));
    }

    #[test]
    fn test_page_mode_to_dict() {
        let prefs = ViewerPreferences::new().page_mode(PageMode::UseAttachments);
        let dict = prefs.to_dict();
        assert_eq!(
            dict.get("PageMode"),
            Some(&Object::Name("UseAttachments".to_string()))
        );
    }

    #[test]
    fn test_print_page_range_reversed() {
        // Test that reversed ranges are handled correctly (start > end)
        let prefs = ViewerPreferences::new().add_print_page_range(10, 5);
        let dict = prefs.to_dict();
        if let Some(Object::Array(ranges)) = dict.get("PrintPageRange") {
            assert_eq!(ranges[0], Object::Integer(5)); // min(10, 5) = 5
            assert_eq!(ranges[1], Object::Integer(10)); // max(10, 5) = 10
        } else {
            panic!("Expected PrintPageRange array");
        }
    }

    #[test]
    fn test_all_page_layouts_to_dict() {
        let layouts = [
            (PageLayout::SinglePage, "SinglePage"),
            (PageLayout::OneColumn, "OneColumn"),
            (PageLayout::TwoColumnLeft, "TwoColumnLeft"),
            (PageLayout::TwoColumnRight, "TwoColumnRight"),
            (PageLayout::TwoPageLeft, "TwoPageLeft"),
            (PageLayout::TwoPageRight, "TwoPageRight"),
        ];

        for (layout, expected_name) in layouts {
            let prefs = ViewerPreferences::new().page_layout(layout);
            let dict = prefs.to_dict();
            assert_eq!(
                dict.get("PageLayout"),
                Some(&Object::Name(expected_name.to_string()))
            );
        }
    }

    #[test]
    fn test_print_scaling_app_default_to_dict() {
        let prefs = ViewerPreferences::new().print_scaling(PrintScaling::AppDefault);
        let dict = prefs.to_dict();
        assert_eq!(
            dict.get("PrintScaling"),
            Some(&Object::Name("AppDefault".to_string()))
        );
    }

    #[test]
    fn test_duplex_simplex_to_dict() {
        let prefs = ViewerPreferences::new().duplex(Duplex::Simplex);
        let dict = prefs.to_dict();
        assert_eq!(
            dict.get("Duplex"),
            Some(&Object::Name("Simplex".to_string()))
        );
    }

    #[test]
    fn test_duplex_flip_short_edge_to_dict() {
        let prefs = ViewerPreferences::new().duplex(Duplex::DuplexFlipShortEdge);
        let dict = prefs.to_dict();
        assert_eq!(
            dict.get("Duplex"),
            Some(&Object::Name("DuplexFlipShortEdge".to_string()))
        );
    }

    #[test]
    fn test_full_preferences_to_dict() {
        let mut prefs = ViewerPreferences::new()
            .hide_toolbar(true)
            .hide_menubar(true)
            .hide_window_ui(true)
            .fit_window(true)
            .center_window(true)
            .display_doc_title(true)
            .page_layout(PageLayout::TwoColumnRight)
            .page_mode(PageMode::UseOutlines)
            .non_full_screen_page_mode(NonFullScreenPageMode::UseThumbs)
            .direction(Direction::R2L)
            .print_scaling(PrintScaling::None)
            .duplex(Duplex::DuplexFlipLongEdge)
            .num_copies(2)
            .pick_tray_by_pdf_size(true)
            .add_print_page_range(1, 10);

        prefs.view_area = Some("MediaBox".to_string());
        prefs.view_clip = Some("CropBox".to_string());
        prefs.print_area = Some("BleedBox".to_string());
        prefs.print_clip = Some("TrimBox".to_string());

        let dict = prefs.to_dict();

        // Verify all fields are present
        assert!(dict.contains_key("HideToolbar"));
        assert!(dict.contains_key("HideMenubar"));
        assert!(dict.contains_key("HideWindowUI"));
        assert!(dict.contains_key("FitWindow"));
        assert!(dict.contains_key("CenterWindow"));
        assert!(dict.contains_key("DisplayDocTitle"));
        assert!(dict.contains_key("PageLayout"));
        assert!(dict.contains_key("PageMode"));
        assert!(dict.contains_key("NonFullScreenPageMode"));
        assert!(dict.contains_key("Direction"));
        assert!(dict.contains_key("ViewArea"));
        assert!(dict.contains_key("ViewClip"));
        assert!(dict.contains_key("PrintArea"));
        assert!(dict.contains_key("PrintClip"));
        assert!(dict.contains_key("PrintScaling"));
        assert!(dict.contains_key("Duplex"));
        assert!(dict.contains_key("PrintPageRange"));
        assert!(dict.contains_key("NumCopies"));
        assert!(dict.contains_key("PickTrayByPDFSize"));
    }
}