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
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
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
use crate::error::Result;
use crate::fonts::{Font as CustomFont, FontCache};
use crate::forms::{AcroForm, FormManager};
use crate::page::Page;
use crate::page_labels::PageLabelTree;
use crate::semantic::{BoundingBox, EntityType, RelationType, SemanticEntity};
use crate::structure::{NamedDestinations, OutlineTree, StructTree};
// Alias to avoid collision with crate::fonts::FontMetrics (PDF font objects)
use crate::text::metrics::{register_custom_font_metrics, FontMetrics as TextMeasurementMetrics};
use crate::text::FontEncoding;
use crate::writer::PdfWriter;
use chrono::{DateTime, Local, Utc};
use std::collections::HashSet;
use std::sync::Arc;

mod encryption;
pub use encryption::{DocumentEncryption, EncryptionStrength};

/// A PDF document that can contain multiple pages and metadata.
///
/// # Example
///
/// ```rust
/// use oxidize_pdf::{Document, Page};
///
/// let mut doc = Document::new();
/// doc.set_title("My Document");
/// doc.set_author("John Doe");
///
/// let page = Page::a4();
/// doc.add_page(page);
///
/// doc.save("output.pdf").unwrap();
/// ```
pub struct Document {
    pub(crate) pages: Vec<Page>,
    pub(crate) metadata: DocumentMetadata,
    pub(crate) encryption: Option<DocumentEncryption>,
    pub(crate) outline: Option<OutlineTree>,
    pub(crate) named_destinations: Option<NamedDestinations>,
    pub(crate) page_labels: Option<PageLabelTree>,
    /// Default font encoding to use for fonts when no encoding is specified
    pub(crate) default_font_encoding: Option<FontEncoding>,
    /// Interactive form data (AcroForm)
    pub(crate) acro_form: Option<AcroForm>,
    /// Form manager for handling interactive forms
    pub(crate) form_manager: Option<FormManager>,
    /// Whether to compress streams when writing the PDF
    pub(crate) compress: bool,
    /// Whether to use compressed cross-reference streams (PDF 1.5+)
    pub(crate) use_xref_streams: bool,
    /// Cache for custom fonts
    pub(crate) custom_fonts: FontCache,
    /// Characters used in the document (for font subsetting)
    pub(crate) used_characters: HashSet<char>,
    /// Action to execute when the document is opened
    pub(crate) open_action: Option<crate::actions::Action>,
    /// Viewer preferences for controlling document display
    pub(crate) viewer_preferences: Option<crate::viewer_preferences::ViewerPreferences>,
    /// Semantic entities marked in the document for AI processing
    pub(crate) semantic_entities: Vec<SemanticEntity>,
    /// Document structure tree for Tagged PDF (accessibility)
    pub(crate) struct_tree: Option<StructTree>,
}

/// Metadata for a PDF document.
#[derive(Debug, Clone)]
pub struct DocumentMetadata {
    /// Document title
    pub title: Option<String>,
    /// Document author
    pub author: Option<String>,
    /// Document subject
    pub subject: Option<String>,
    /// Document keywords
    pub keywords: Option<String>,
    /// Software that created the original document
    pub creator: Option<String>,
    /// Software that produced the PDF
    pub producer: Option<String>,
    /// Date and time the document was created
    pub creation_date: Option<DateTime<Utc>>,
    /// Date and time the document was last modified
    pub modification_date: Option<DateTime<Utc>>,
}

impl Default for DocumentMetadata {
    fn default() -> Self {
        let now = Utc::now();

        let edition = "MIT";

        Self {
            title: None,
            author: None,
            subject: None,
            keywords: None,
            creator: Some("oxidize_pdf".to_string()),
            producer: Some(format!(
                "oxidize_pdf v{} ({})",
                env!("CARGO_PKG_VERSION"),
                edition
            )),
            creation_date: Some(now),
            modification_date: Some(now),
        }
    }
}

impl Document {
    /// Creates a new empty PDF document.
    pub fn new() -> Self {
        Self {
            pages: Vec::new(),
            metadata: DocumentMetadata::default(),
            encryption: None,
            outline: None,
            named_destinations: None,
            page_labels: None,
            default_font_encoding: None,
            acro_form: None,
            form_manager: None,
            compress: true,          // Enable compression by default
            use_xref_streams: false, // Disabled by default for compatibility
            custom_fonts: FontCache::new(),
            used_characters: HashSet::new(),
            open_action: None,
            viewer_preferences: None,
            semantic_entities: Vec::new(),
            struct_tree: None,
        }
    }

    /// Adds a page to the document.
    pub fn add_page(&mut self, page: Page) {
        // Collect used characters from the page
        if let Some(used_chars) = page.get_used_characters() {
            self.used_characters.extend(used_chars);
        }
        self.pages.push(page);
    }

    /// Sets the document title.
    pub fn set_title(&mut self, title: impl Into<String>) {
        self.metadata.title = Some(title.into());
    }

    /// Sets the document author.
    pub fn set_author(&mut self, author: impl Into<String>) {
        self.metadata.author = Some(author.into());
    }

    /// Sets the form manager for the document.
    pub fn set_form_manager(&mut self, form_manager: FormManager) {
        self.form_manager = Some(form_manager);
    }

    /// Sets the document subject.
    pub fn set_subject(&mut self, subject: impl Into<String>) {
        self.metadata.subject = Some(subject.into());
    }

    /// Sets the document keywords.
    pub fn set_keywords(&mut self, keywords: impl Into<String>) {
        self.metadata.keywords = Some(keywords.into());
    }

    /// Set document encryption
    pub fn set_encryption(&mut self, encryption: DocumentEncryption) {
        self.encryption = Some(encryption);
    }

    /// Set simple encryption with passwords
    pub fn encrypt_with_passwords(
        &mut self,
        user_password: impl Into<String>,
        owner_password: impl Into<String>,
    ) {
        self.encryption = Some(DocumentEncryption::with_passwords(
            user_password,
            owner_password,
        ));
    }

    /// Check if document is encrypted
    pub fn is_encrypted(&self) -> bool {
        self.encryption.is_some()
    }

    /// Set the action to execute when the document is opened
    pub fn set_open_action(&mut self, action: crate::actions::Action) {
        self.open_action = Some(action);
    }

    /// Get the document open action
    pub fn open_action(&self) -> Option<&crate::actions::Action> {
        self.open_action.as_ref()
    }

    /// Set viewer preferences for controlling document display
    pub fn set_viewer_preferences(
        &mut self,
        preferences: crate::viewer_preferences::ViewerPreferences,
    ) {
        self.viewer_preferences = Some(preferences);
    }

    /// Get viewer preferences
    pub fn viewer_preferences(&self) -> Option<&crate::viewer_preferences::ViewerPreferences> {
        self.viewer_preferences.as_ref()
    }

    /// Set the document structure tree for Tagged PDF (accessibility)
    ///
    /// Tagged PDF provides semantic information about document content,
    /// making PDFs accessible to screen readers and assistive technologies.
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// use oxidize_pdf::{Document, structure::{StructTree, StructureElement, StandardStructureType}};
    ///
    /// let mut doc = Document::new();
    /// let mut tree = StructTree::new();
    ///
    /// // Create document root
    /// let doc_elem = StructureElement::new(StandardStructureType::Document);
    /// let doc_idx = tree.set_root(doc_elem);
    ///
    /// // Add heading
    /// let h1 = StructureElement::new(StandardStructureType::H1)
    ///     .with_language("en-US")
    ///     .with_actual_text("Welcome");
    /// tree.add_child(doc_idx, h1).unwrap();
    ///
    /// doc.set_struct_tree(tree);
    /// ```
    pub fn set_struct_tree(&mut self, tree: StructTree) {
        self.struct_tree = Some(tree);
    }

    /// Get a reference to the document structure tree
    pub fn struct_tree(&self) -> Option<&StructTree> {
        self.struct_tree.as_ref()
    }

    /// Get a mutable reference to the document structure tree
    pub fn struct_tree_mut(&mut self) -> Option<&mut StructTree> {
        self.struct_tree.as_mut()
    }

    /// Initialize a new structure tree if one doesn't exist and return a mutable reference
    ///
    /// This is a convenience method for adding Tagged PDF support.
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// use oxidize_pdf::{Document, structure::{StructureElement, StandardStructureType}};
    ///
    /// let mut doc = Document::new();
    /// let tree = doc.get_or_create_struct_tree();
    ///
    /// // Create document root
    /// let doc_elem = StructureElement::new(StandardStructureType::Document);
    /// tree.set_root(doc_elem);
    /// ```
    pub fn get_or_create_struct_tree(&mut self) -> &mut StructTree {
        self.struct_tree.get_or_insert_with(StructTree::new)
    }

    /// Set document outline (bookmarks)
    pub fn set_outline(&mut self, outline: OutlineTree) {
        self.outline = Some(outline);
    }

    /// Get document outline
    pub fn outline(&self) -> Option<&OutlineTree> {
        self.outline.as_ref()
    }

    /// Get mutable document outline
    pub fn outline_mut(&mut self) -> Option<&mut OutlineTree> {
        self.outline.as_mut()
    }

    /// Set named destinations
    pub fn set_named_destinations(&mut self, destinations: NamedDestinations) {
        self.named_destinations = Some(destinations);
    }

    /// Get named destinations
    pub fn named_destinations(&self) -> Option<&NamedDestinations> {
        self.named_destinations.as_ref()
    }

    /// Get mutable named destinations
    pub fn named_destinations_mut(&mut self) -> Option<&mut NamedDestinations> {
        self.named_destinations.as_mut()
    }

    /// Set page labels
    pub fn set_page_labels(&mut self, labels: PageLabelTree) {
        self.page_labels = Some(labels);
    }

    /// Get page labels
    pub fn page_labels(&self) -> Option<&PageLabelTree> {
        self.page_labels.as_ref()
    }

    /// Get mutable page labels
    pub fn page_labels_mut(&mut self) -> Option<&mut PageLabelTree> {
        self.page_labels.as_mut()
    }

    /// Get page label for a specific page
    pub fn get_page_label(&self, page_index: u32) -> String {
        self.page_labels
            .as_ref()
            .and_then(|labels| labels.get_label(page_index))
            .unwrap_or_else(|| (page_index + 1).to_string())
    }

    /// Get all page labels
    pub fn get_all_page_labels(&self) -> Vec<String> {
        let page_count = self.pages.len() as u32;
        if let Some(labels) = &self.page_labels {
            labels.get_all_labels(page_count)
        } else {
            (1..=page_count).map(|i| i.to_string()).collect()
        }
    }

    /// Sets the document creator (software that created the original document).
    pub fn set_creator(&mut self, creator: impl Into<String>) {
        self.metadata.creator = Some(creator.into());
    }

    /// Sets the document producer (software that produced the PDF).
    pub fn set_producer(&mut self, producer: impl Into<String>) {
        self.metadata.producer = Some(producer.into());
    }

    /// Sets the document creation date.
    pub fn set_creation_date(&mut self, date: DateTime<Utc>) {
        self.metadata.creation_date = Some(date);
    }

    /// Sets the document creation date using local time.
    pub fn set_creation_date_local(&mut self, date: DateTime<Local>) {
        self.metadata.creation_date = Some(date.with_timezone(&Utc));
    }

    /// Sets the document modification date.
    pub fn set_modification_date(&mut self, date: DateTime<Utc>) {
        self.metadata.modification_date = Some(date);
    }

    /// Sets the document modification date using local time.
    pub fn set_modification_date_local(&mut self, date: DateTime<Local>) {
        self.metadata.modification_date = Some(date.with_timezone(&Utc));
    }

    /// Sets the modification date to the current time.
    pub fn update_modification_date(&mut self) {
        self.metadata.modification_date = Some(Utc::now());
    }

    /// Sets the default font encoding for fonts that don't specify an encoding.
    ///
    /// This encoding will be applied to fonts in the PDF font dictionary when
    /// no explicit encoding is specified. Setting this to `None` (the default)
    /// means no encoding metadata will be added to fonts unless explicitly specified.
    ///
    /// # Example
    ///
    /// ```rust
    /// use oxidize_pdf::{Document, text::FontEncoding};
    ///
    /// let mut doc = Document::new();
    /// doc.set_default_font_encoding(Some(FontEncoding::WinAnsiEncoding));
    /// ```
    pub fn set_default_font_encoding(&mut self, encoding: Option<FontEncoding>) {
        self.default_font_encoding = encoding;
    }

    /// Gets the current default font encoding.
    pub fn default_font_encoding(&self) -> Option<FontEncoding> {
        self.default_font_encoding
    }

    /// Add a custom font from a file path
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// use oxidize_pdf::Document;
    ///
    /// let mut doc = Document::new();
    /// doc.add_font("MyFont", "path/to/font.ttf").unwrap();
    /// ```
    pub fn add_font(
        &mut self,
        name: impl Into<String>,
        path: impl AsRef<std::path::Path>,
    ) -> Result<()> {
        let name = name.into();
        let font = CustomFont::from_file(&name, path)?;
        self.custom_fonts.add_font(name, font)?;
        Ok(())
    }

    /// Add a custom font from byte data
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// use oxidize_pdf::Document;
    ///
    /// let mut doc = Document::new();
    /// let font_data = vec![0; 1000]; // Your font data
    /// doc.add_font_from_bytes("MyFont", font_data).unwrap();
    /// ```
    pub fn add_font_from_bytes(&mut self, name: impl Into<String>, data: Vec<u8>) -> Result<()> {
        let name = name.into();
        let font = CustomFont::from_bytes(&name, data)?;

        // Extract glyph widths before moving font into the cache
        // Convert from font units to 1/1000 em units used by text::metrics
        let units_per_em = font.metrics.units_per_em as f64;
        let char_width_map: std::collections::HashMap<char, u16> = font
            .glyph_mapping
            .char_widths_iter()
            .map(|(ch, width_font_units)| {
                let width_1000 = ((width_font_units as f64 * 1000.0) / units_per_em).round() as u16;
                (ch, width_1000)
            })
            .collect();

        // Add to font cache first — if this fails, no metrics are registered (consistent state)
        self.custom_fonts.add_font(name.clone(), font)?;

        // Register text measurement metrics only after successful cache insertion
        if !char_width_map.is_empty() {
            let sum: u32 = char_width_map.values().map(|&w| w as u32).sum();
            let default_width = (sum / char_width_map.len() as u32) as u16;
            let text_metrics = TextMeasurementMetrics::from_char_map(char_width_map, default_width);
            register_custom_font_metrics(name, text_metrics);
        }

        Ok(())
    }

    /// Get a custom font by name
    pub(crate) fn get_custom_font(&self, name: &str) -> Option<Arc<CustomFont>> {
        self.custom_fonts.get_font(name)
    }

    /// Check if a custom font is loaded
    pub fn has_custom_font(&self, name: &str) -> bool {
        self.custom_fonts.has_font(name)
    }

    /// Get all loaded custom font names
    pub fn custom_font_names(&self) -> Vec<String> {
        self.custom_fonts.font_names()
    }

    /// Gets the number of pages in the document.
    pub fn page_count(&self) -> usize {
        self.pages.len()
    }

    /// Gets a reference to the AcroForm (interactive form) if present.
    pub fn acro_form(&self) -> Option<&AcroForm> {
        self.acro_form.as_ref()
    }

    /// Gets a mutable reference to the AcroForm (interactive form) if present.
    pub fn acro_form_mut(&mut self) -> Option<&mut AcroForm> {
        self.acro_form.as_mut()
    }

    /// Enables interactive forms by creating a FormManager if not already present.
    /// The FormManager handles both the AcroForm and the connection with page widgets.
    pub fn enable_forms(&mut self) -> &mut FormManager {
        if self.acro_form.is_none() {
            self.acro_form = Some(AcroForm::new());
        }
        self.form_manager.get_or_insert_with(FormManager::new)
    }

    /// Disables interactive forms by removing both the AcroForm and FormManager.
    pub fn disable_forms(&mut self) {
        self.acro_form = None;
        self.form_manager = None;
    }

    /// Saves the document to a file.
    ///
    /// # Errors
    ///
    /// Returns an error if the file cannot be created or written.
    pub fn save(&mut self, path: impl AsRef<std::path::Path>) -> Result<()> {
        // Update modification date before saving
        self.update_modification_date();

        // Create writer config with document's compression setting
        let config = crate::writer::WriterConfig {
            use_xref_streams: self.use_xref_streams,
            use_object_streams: false, // For now, keep object streams disabled by default
            pdf_version: if self.use_xref_streams { "1.5" } else { "1.7" }.to_string(),
            compress_streams: self.compress,
            incremental_update: false,
        };

        use std::io::BufWriter;
        let file = std::fs::File::create(path)?;
        // Use 512KB buffer for better I/O performance (vs default 8KB)
        // Reduces syscalls by ~98% for typical PDFs
        let writer = BufWriter::with_capacity(512 * 1024, file);
        let mut pdf_writer = PdfWriter::with_config(writer, config);

        pdf_writer.write_document(self)?;
        Ok(())
    }

    /// Saves the document to a file with custom writer configuration.
    ///
    /// # Errors
    ///
    /// Returns an error if the file cannot be created or written.
    pub fn save_with_config(
        &mut self,
        path: impl AsRef<std::path::Path>,
        config: crate::writer::WriterConfig,
    ) -> Result<()> {
        use std::io::BufWriter;

        // Update modification date before saving
        self.update_modification_date();

        // Use the config as provided (don't override compress_streams)

        let file = std::fs::File::create(path)?;
        // Use 512KB buffer for better I/O performance (vs default 8KB)
        let writer = BufWriter::with_capacity(512 * 1024, file);
        let mut pdf_writer = PdfWriter::with_config(writer, config);
        pdf_writer.write_document(self)?;
        Ok(())
    }

    /// Saves the document to a file with custom values for headers/footers.
    ///
    /// This method processes all pages to replace custom placeholders in headers
    /// and footers before saving the document.
    ///
    /// # Arguments
    ///
    /// * `path` - The path where the document should be saved
    /// * `custom_values` - A map of placeholder names to their replacement values
    ///
    /// # Errors
    ///
    /// Returns an error if the file cannot be created or written.
    pub fn save_with_custom_values(
        &mut self,
        path: impl AsRef<std::path::Path>,
        custom_values: &std::collections::HashMap<String, String>,
    ) -> Result<()> {
        // Process all pages with custom values
        let total_pages = self.pages.len();
        for (index, page) in self.pages.iter_mut().enumerate() {
            // Generate content with page info and custom values
            let page_content = page.generate_content_with_page_info(
                Some(index + 1),
                Some(total_pages),
                Some(custom_values),
            )?;
            // Update the page content
            page.set_content(page_content);
        }

        // Save the document normally
        self.save(path)
    }

    /// Writes the document to a buffer.
    ///
    /// # Errors
    ///
    /// Returns an error if the PDF cannot be generated.
    pub fn write(&mut self, buffer: &mut Vec<u8>) -> Result<()> {
        // Update modification date before writing
        self.update_modification_date();

        let mut writer = PdfWriter::new_with_writer(buffer);
        writer.write_document(self)?;
        Ok(())
    }

    /// Enables or disables compression for PDF streams.
    ///
    /// When compression is enabled (default), content streams and XRef streams are compressed
    /// using Flate/Zlib compression to reduce file size. When disabled, streams are written
    /// uncompressed, making the PDF larger but easier to debug.
    ///
    /// # Arguments
    ///
    /// * `compress` - Whether to enable compression
    ///
    /// # Example
    ///
    /// ```rust
    /// use oxidize_pdf::{Document, Page};
    ///
    /// let mut doc = Document::new();
    ///
    /// // Disable compression for debugging
    /// doc.set_compress(false);
    ///
    /// doc.set_title("My Document");
    /// doc.add_page(Page::a4());
    ///
    /// let pdf_bytes = doc.to_bytes().unwrap();
    /// println!("Uncompressed PDF size: {} bytes", pdf_bytes.len());
    /// ```
    pub fn set_compress(&mut self, compress: bool) {
        self.compress = compress;
    }

    /// Enable or disable compressed cross-reference streams (PDF 1.5+).
    ///
    /// Cross-reference streams provide more compact representation of the cross-reference
    /// table and support additional features like compressed object streams.
    ///
    /// # Arguments
    ///
    /// * `enable` - Whether to enable compressed cross-reference streams
    ///
    /// # Example
    ///
    /// ```rust
    /// use oxidize_pdf::Document;
    ///
    /// let mut doc = Document::new();
    /// doc.enable_xref_streams(true);
    /// ```
    pub fn enable_xref_streams(&mut self, enable: bool) -> &mut Self {
        self.use_xref_streams = enable;
        self
    }

    /// Gets the current compression setting.
    ///
    /// # Returns
    ///
    /// Returns `true` if compression is enabled, `false` otherwise.
    pub fn get_compress(&self) -> bool {
        self.compress
    }

    /// Generates the PDF document as bytes in memory.
    ///
    /// This method provides in-memory PDF generation without requiring file I/O.
    /// The document is serialized to bytes and returned as a `Vec<u8>`.
    ///
    /// # Returns
    ///
    /// Returns the PDF document as bytes on success.
    ///
    /// # Errors
    ///
    /// Returns an error if the document cannot be serialized.
    ///
    /// # Example
    ///
    /// ```rust
    /// use oxidize_pdf::{Document, Page};
    ///
    /// let mut doc = Document::new();
    /// doc.set_title("My Document");
    ///
    /// let page = Page::a4();
    /// doc.add_page(page);
    ///
    /// let pdf_bytes = doc.to_bytes().unwrap();
    /// println!("Generated PDF size: {} bytes", pdf_bytes.len());
    /// ```
    pub fn to_bytes(&mut self) -> Result<Vec<u8>> {
        // Update modification date before serialization
        self.update_modification_date();

        // Create a buffer to write the PDF data to
        let mut buffer = Vec::new();

        // Create writer config with document's compression setting
        let config = crate::writer::WriterConfig {
            use_xref_streams: self.use_xref_streams,
            use_object_streams: false, // For now, keep object streams disabled by default
            pdf_version: if self.use_xref_streams { "1.5" } else { "1.7" }.to_string(),
            compress_streams: self.compress,
            incremental_update: false,
        };

        // Use PdfWriter with the buffer as output and config
        let mut writer = PdfWriter::with_config(&mut buffer, config);
        writer.write_document(self)?;

        Ok(buffer)
    }

    /// Generates the PDF document as bytes with custom writer configuration.
    ///
    /// This method allows customizing the PDF output (e.g., using XRef streams)
    /// while still generating the document in memory.
    ///
    /// # Arguments
    ///
    /// * `config` - Writer configuration options
    ///
    /// # Returns
    ///
    /// Returns the PDF document as bytes on success.
    ///
    /// # Errors
    ///
    /// Returns an error if the document cannot be serialized.
    ///
    /// # Example
    ///
    /// ```rust
    /// use oxidize_pdf::{Document, Page};
    /// use oxidize_pdf::writer::WriterConfig;
    ///
    /// let mut doc = Document::new();
    /// doc.set_title("My Document");
    ///
    /// let page = Page::a4();
    /// doc.add_page(page);
    ///
    /// let config = WriterConfig {
    ///     use_xref_streams: true,
    ///     use_object_streams: false,
    ///     pdf_version: "1.5".to_string(),
    ///     compress_streams: true,
    ///     incremental_update: false,
    /// };
    ///
    /// let pdf_bytes = doc.to_bytes_with_config(config).unwrap();
    /// println!("Generated PDF size: {} bytes", pdf_bytes.len());
    /// ```
    pub fn to_bytes_with_config(&mut self, config: crate::writer::WriterConfig) -> Result<Vec<u8>> {
        // Update modification date before serialization
        self.update_modification_date();

        // Use the config as provided (don't override compress_streams)

        // Create a buffer to write the PDF data to
        let mut buffer = Vec::new();

        // Use PdfWriter with the buffer as output and custom config
        let mut writer = PdfWriter::with_config(&mut buffer, config);
        writer.write_document(self)?;

        Ok(buffer)
    }

    // ==================== Semantic Entity Methods ====================

    /// Mark a region of the PDF with semantic meaning for AI processing.
    ///
    /// This creates an AI-Ready PDF that contains machine-readable metadata
    /// alongside the visual content, enabling automated document processing.
    ///
    /// # Example
    ///
    /// ```rust
    /// use oxidize_pdf::{Document, semantic::{EntityType, BoundingBox}};
    ///
    /// let mut doc = Document::new();
    ///
    /// // Mark an invoice number region
    /// let entity_id = doc.mark_entity(
    ///     "invoice_001".to_string(),
    ///     EntityType::InvoiceNumber,
    ///     BoundingBox::new(100.0, 700.0, 150.0, 20.0, 1)
    /// );
    ///
    /// // Add content and metadata
    /// doc.set_entity_content(&entity_id, "INV-2024-001");
    /// doc.add_entity_metadata(&entity_id, "confidence", "0.98");
    /// ```
    pub fn mark_entity(
        &mut self,
        id: impl Into<String>,
        entity_type: EntityType,
        bounds: BoundingBox,
    ) -> String {
        let entity_id = id.into();
        let entity = SemanticEntity::new(entity_id.clone(), entity_type, bounds);
        self.semantic_entities.push(entity);
        entity_id
    }

    /// Set the content text for an entity
    pub fn set_entity_content(&mut self, entity_id: &str, content: impl Into<String>) -> bool {
        if let Some(entity) = self
            .semantic_entities
            .iter_mut()
            .find(|e| e.id == entity_id)
        {
            entity.content = content.into();
            true
        } else {
            false
        }
    }

    /// Add metadata to an entity
    pub fn add_entity_metadata(
        &mut self,
        entity_id: &str,
        key: impl Into<String>,
        value: impl Into<String>,
    ) -> bool {
        if let Some(entity) = self
            .semantic_entities
            .iter_mut()
            .find(|e| e.id == entity_id)
        {
            entity.metadata.properties.insert(key.into(), value.into());
            true
        } else {
            false
        }
    }

    /// Set confidence score for an entity
    pub fn set_entity_confidence(&mut self, entity_id: &str, confidence: f32) -> bool {
        if let Some(entity) = self
            .semantic_entities
            .iter_mut()
            .find(|e| e.id == entity_id)
        {
            entity.metadata.confidence = Some(confidence.clamp(0.0, 1.0));
            true
        } else {
            false
        }
    }

    /// Add a relationship between two entities
    pub fn relate_entities(
        &mut self,
        from_id: &str,
        to_id: &str,
        relation_type: RelationType,
    ) -> bool {
        // First check if target entity exists
        let target_exists = self.semantic_entities.iter().any(|e| e.id == to_id);
        if !target_exists {
            return false;
        }

        // Then add the relationship
        if let Some(entity) = self.semantic_entities.iter_mut().find(|e| e.id == from_id) {
            entity.relationships.push(crate::semantic::EntityRelation {
                target_id: to_id.to_string(),
                relation_type,
            });
            true
        } else {
            false
        }
    }

    /// Get all semantic entities in the document
    pub fn get_semantic_entities(&self) -> &[SemanticEntity] {
        &self.semantic_entities
    }

    /// Get entities by type
    pub fn get_entities_by_type(&self, entity_type: EntityType) -> Vec<&SemanticEntity> {
        self.semantic_entities
            .iter()
            .filter(|e| e.entity_type == entity_type)
            .collect()
    }

    /// Export semantic entities as JSON
    #[cfg(feature = "semantic")]
    pub fn export_semantic_entities_json(&self) -> Result<String> {
        serde_json::to_string_pretty(&self.semantic_entities)
            .map_err(|e| crate::error::PdfError::SerializationError(e.to_string()))
    }

    /// Export semantic entities as JSON-LD with Schema.org context
    ///
    /// This creates a machine-readable export compatible with Schema.org vocabularies,
    /// making the PDF data accessible to AI/ML processing pipelines.
    ///
    /// # Example
    ///
    /// ```rust
    /// use oxidize_pdf::{Document, semantic::{EntityType, BoundingBox}};
    ///
    /// let mut doc = Document::new();
    ///
    /// // Mark an invoice
    /// let inv_id = doc.mark_entity(
    ///     "invoice_1".to_string(),
    ///     EntityType::Invoice,
    ///     BoundingBox::new(50.0, 50.0, 500.0, 700.0, 1)
    /// );
    /// doc.set_entity_content(&inv_id, "Invoice #INV-001");
    /// doc.add_entity_metadata(&inv_id, "totalPrice", "1234.56");
    ///
    /// // Export as JSON-LD
    /// let json_ld = doc.export_semantic_entities_json_ld().unwrap();
    /// println!("{}", json_ld);
    /// ```
    #[cfg(feature = "semantic")]
    pub fn export_semantic_entities_json_ld(&self) -> Result<String> {
        use crate::semantic::{Entity, EntityMap};

        let mut entity_map = EntityMap::new();

        // Convert SemanticEntity to Entity (backward compatibility)
        for sem_entity in &self.semantic_entities {
            let entity = Entity {
                id: sem_entity.id.clone(),
                entity_type: sem_entity.entity_type.clone(),
                bounds: (
                    sem_entity.bounds.x as f64,
                    sem_entity.bounds.y as f64,
                    sem_entity.bounds.width as f64,
                    sem_entity.bounds.height as f64,
                ),
                page: (sem_entity.bounds.page - 1) as usize, // Convert 1-indexed to 0-indexed
                metadata: sem_entity.metadata.clone(),
            };
            entity_map.add_entity(entity);
        }

        // Add document metadata
        if let Some(title) = &self.metadata.title {
            entity_map
                .document_metadata
                .insert("name".to_string(), title.clone());
        }
        if let Some(author) = &self.metadata.author {
            entity_map
                .document_metadata
                .insert("author".to_string(), author.clone());
        }

        entity_map
            .to_json_ld()
            .map_err(|e| crate::error::PdfError::SerializationError(e.to_string()))
    }

    /// Find an entity by ID
    pub fn find_entity(&self, entity_id: &str) -> Option<&SemanticEntity> {
        self.semantic_entities.iter().find(|e| e.id == entity_id)
    }

    /// Remove an entity by ID
    pub fn remove_entity(&mut self, entity_id: &str) -> bool {
        if let Some(pos) = self
            .semantic_entities
            .iter()
            .position(|e| e.id == entity_id)
        {
            self.semantic_entities.remove(pos);
            // Also remove any relationships pointing to this entity
            for entity in &mut self.semantic_entities {
                entity.relationships.retain(|r| r.target_id != entity_id);
            }
            true
        } else {
            false
        }
    }

    /// Get the count of semantic entities
    pub fn semantic_entity_count(&self) -> usize {
        self.semantic_entities.len()
    }

    /// Create XMP metadata from document metadata
    ///
    /// Generates an XMP metadata object from the document's metadata.
    /// The XMP metadata can be serialized and embedded in the PDF.
    ///
    /// # Returns
    /// XMP metadata object populated with document information
    pub fn create_xmp_metadata(&self) -> crate::metadata::XmpMetadata {
        let mut xmp = crate::metadata::XmpMetadata::new();

        // Add Dublin Core metadata
        if let Some(title) = &self.metadata.title {
            xmp.set_text(crate::metadata::XmpNamespace::DublinCore, "title", title);
        }
        if let Some(author) = &self.metadata.author {
            xmp.set_text(crate::metadata::XmpNamespace::DublinCore, "creator", author);
        }
        if let Some(subject) = &self.metadata.subject {
            xmp.set_text(
                crate::metadata::XmpNamespace::DublinCore,
                "description",
                subject,
            );
        }

        // Add XMP Basic metadata
        if let Some(creator) = &self.metadata.creator {
            xmp.set_text(
                crate::metadata::XmpNamespace::XmpBasic,
                "CreatorTool",
                creator,
            );
        }
        if let Some(creation_date) = &self.metadata.creation_date {
            xmp.set_date(
                crate::metadata::XmpNamespace::XmpBasic,
                "CreateDate",
                creation_date.to_rfc3339(),
            );
        }
        if let Some(mod_date) = &self.metadata.modification_date {
            xmp.set_date(
                crate::metadata::XmpNamespace::XmpBasic,
                "ModifyDate",
                mod_date.to_rfc3339(),
            );
        }

        // Add PDF specific metadata
        if let Some(producer) = &self.metadata.producer {
            xmp.set_text(crate::metadata::XmpNamespace::Pdf, "Producer", producer);
        }

        xmp
    }

    /// Get XMP packet as string
    ///
    /// Returns the XMP metadata packet that can be embedded in the PDF.
    /// This is a convenience method that creates XMP from document metadata
    /// and serializes it to XML.
    ///
    /// # Returns
    /// XMP packet as XML string
    pub fn get_xmp_packet(&self) -> String {
        self.create_xmp_metadata().to_xmp_packet()
    }

    /// Extract text content from all pages (placeholder implementation)
    pub fn extract_text(&self) -> Result<String> {
        // Placeholder implementation - in a real PDF reader this would
        // parse content streams and extract text operators
        let mut text = String::new();
        for (i, _page) in self.pages.iter().enumerate() {
            text.push_str(&format!("Text from page {} (placeholder)\n", i + 1));
        }
        Ok(text)
    }

    /// Extract text content from a specific page (placeholder implementation)
    pub fn extract_page_text(&self, page_index: usize) -> Result<String> {
        if page_index < self.pages.len() {
            Ok(format!("Text from page {} (placeholder)", page_index + 1))
        } else {
            Err(crate::error::PdfError::InvalidReference(format!(
                "Page index {} out of bounds",
                page_index
            )))
        }
    }
}

impl Default for Document {
    fn default() -> Self {
        Self::new()
    }
}

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

    #[test]
    fn test_document_new() {
        let doc = Document::new();
        assert!(doc.pages.is_empty());
        assert!(doc.metadata.title.is_none());
        assert!(doc.metadata.author.is_none());
        assert!(doc.metadata.subject.is_none());
        assert!(doc.metadata.keywords.is_none());
        assert_eq!(doc.metadata.creator, Some("oxidize_pdf".to_string()));
        assert!(doc
            .metadata
            .producer
            .as_ref()
            .unwrap()
            .starts_with("oxidize_pdf"));
    }

    #[test]
    fn test_document_default() {
        let doc = Document::default();
        assert!(doc.pages.is_empty());
    }

    #[test]
    fn test_add_page() {
        let mut doc = Document::new();
        let page1 = Page::a4();
        let page2 = Page::letter();

        doc.add_page(page1);
        assert_eq!(doc.pages.len(), 1);

        doc.add_page(page2);
        assert_eq!(doc.pages.len(), 2);
    }

    #[test]
    fn test_set_title() {
        let mut doc = Document::new();
        assert!(doc.metadata.title.is_none());

        doc.set_title("Test Document");
        assert_eq!(doc.metadata.title, Some("Test Document".to_string()));

        doc.set_title(String::from("Another Title"));
        assert_eq!(doc.metadata.title, Some("Another Title".to_string()));
    }

    #[test]
    fn test_set_author() {
        let mut doc = Document::new();
        assert!(doc.metadata.author.is_none());

        doc.set_author("John Doe");
        assert_eq!(doc.metadata.author, Some("John Doe".to_string()));
    }

    #[test]
    fn test_set_subject() {
        let mut doc = Document::new();
        assert!(doc.metadata.subject.is_none());

        doc.set_subject("Test Subject");
        assert_eq!(doc.metadata.subject, Some("Test Subject".to_string()));
    }

    #[test]
    fn test_set_keywords() {
        let mut doc = Document::new();
        assert!(doc.metadata.keywords.is_none());

        doc.set_keywords("test, pdf, rust");
        assert_eq!(doc.metadata.keywords, Some("test, pdf, rust".to_string()));
    }

    #[test]
    fn test_metadata_default() {
        let metadata = DocumentMetadata::default();
        assert!(metadata.title.is_none());
        assert!(metadata.author.is_none());
        assert!(metadata.subject.is_none());
        assert!(metadata.keywords.is_none());
        assert_eq!(metadata.creator, Some("oxidize_pdf".to_string()));
        assert!(metadata
            .producer
            .as_ref()
            .unwrap()
            .starts_with("oxidize_pdf"));
    }

    #[test]
    fn test_write_to_buffer() {
        let mut doc = Document::new();
        doc.set_title("Buffer Test");
        doc.add_page(Page::a4());

        let mut buffer = Vec::new();
        let result = doc.write(&mut buffer);

        assert!(result.is_ok());
        assert!(!buffer.is_empty());
        assert!(buffer.starts_with(b"%PDF-1.7"));
    }

    #[test]
    fn test_document_with_multiple_pages() {
        let mut doc = Document::new();
        doc.set_title("Multi-page Document");
        doc.set_author("Test Author");
        doc.set_subject("Testing multiple pages");
        doc.set_keywords("test, multiple, pages");

        for _ in 0..5 {
            doc.add_page(Page::a4());
        }

        assert_eq!(doc.pages.len(), 5);
        assert_eq!(doc.metadata.title, Some("Multi-page Document".to_string()));
        assert_eq!(doc.metadata.author, Some("Test Author".to_string()));
    }

    #[test]
    fn test_empty_document_write() {
        let mut doc = Document::new();
        let mut buffer = Vec::new();

        // Empty document should still produce valid PDF
        let result = doc.write(&mut buffer);
        assert!(result.is_ok());
        assert!(!buffer.is_empty());
        assert!(buffer.starts_with(b"%PDF-1.7"));
    }

    // Integration tests for Document ↔ Writer ↔ Parser interactions
    mod integration_tests {
        use super::*;
        use crate::graphics::Color;
        use crate::text::Font;
        use std::fs;
        use tempfile::TempDir;

        #[test]
        fn test_document_writer_roundtrip() {
            let temp_dir = TempDir::new().unwrap();
            let file_path = temp_dir.path().join("test.pdf");

            // Create document with content
            let mut doc = Document::new();
            doc.set_title("Integration Test");
            doc.set_author("Test Author");
            doc.set_subject("Writer Integration");
            doc.set_keywords("test, writer, integration");

            let mut page = Page::a4();
            page.text()
                .set_font(Font::Helvetica, 12.0)
                .at(100.0, 700.0)
                .write("Integration Test Content")
                .unwrap();

            doc.add_page(page);

            // Write to file
            let result = doc.save(&file_path);
            assert!(result.is_ok());

            // Verify file exists and has content
            assert!(file_path.exists());
            let metadata = fs::metadata(&file_path).unwrap();
            assert!(metadata.len() > 0);

            // Read file back to verify PDF format
            let content = fs::read(&file_path).unwrap();
            assert!(content.starts_with(b"%PDF-1.7"));
            // Check for %%EOF with or without newline
            assert!(content.ends_with(b"%%EOF\n") || content.ends_with(b"%%EOF"));
        }

        #[test]
        fn test_document_with_complex_content() {
            let temp_dir = TempDir::new().unwrap();
            let file_path = temp_dir.path().join("complex.pdf");

            let mut doc = Document::new();
            doc.set_title("Complex Content Test");

            // Create page with mixed content
            let mut page = Page::a4();

            // Add text
            page.text()
                .set_font(Font::Helvetica, 14.0)
                .at(50.0, 750.0)
                .write("Complex Content Test")
                .unwrap();

            // Add graphics
            page.graphics()
                .set_fill_color(Color::rgb(0.8, 0.2, 0.2))
                .rectangle(50.0, 500.0, 200.0, 100.0)
                .fill();

            page.graphics()
                .set_stroke_color(Color::rgb(0.2, 0.2, 0.8))
                .set_line_width(2.0)
                .move_to(50.0, 400.0)
                .line_to(250.0, 400.0)
                .stroke();

            doc.add_page(page);

            // Write and verify
            let result = doc.save(&file_path);
            assert!(result.is_ok());
            assert!(file_path.exists());
        }

        #[test]
        fn test_document_multiple_pages_integration() {
            let temp_dir = TempDir::new().unwrap();
            let file_path = temp_dir.path().join("multipage.pdf");

            let mut doc = Document::new();
            doc.set_title("Multi-page Integration Test");

            // Create multiple pages with different content
            for i in 1..=5 {
                let mut page = Page::a4();

                page.text()
                    .set_font(Font::Helvetica, 16.0)
                    .at(50.0, 750.0)
                    .write(&format!("Page {i}"))
                    .unwrap();

                page.text()
                    .set_font(Font::Helvetica, 12.0)
                    .at(50.0, 700.0)
                    .write(&format!("This is the content for page {i}"))
                    .unwrap();

                // Add unique graphics for each page
                let color = match i % 3 {
                    0 => Color::rgb(1.0, 0.0, 0.0),
                    1 => Color::rgb(0.0, 1.0, 0.0),
                    _ => Color::rgb(0.0, 0.0, 1.0),
                };

                page.graphics()
                    .set_fill_color(color)
                    .rectangle(50.0, 600.0, 100.0, 50.0)
                    .fill();

                doc.add_page(page);
            }

            // Write and verify
            let result = doc.save(&file_path);
            assert!(result.is_ok());
            assert!(file_path.exists());

            // Verify file size is reasonable for 5 pages
            let metadata = fs::metadata(&file_path).unwrap();
            assert!(metadata.len() > 1000); // Should be substantial
        }

        #[test]
        fn test_document_metadata_persistence() {
            let temp_dir = TempDir::new().unwrap();
            let file_path = temp_dir.path().join("metadata.pdf");

            let mut doc = Document::new();
            doc.set_title("Metadata Persistence Test");
            doc.set_author("Test Author");
            doc.set_subject("Testing metadata preservation");
            doc.set_keywords("metadata, persistence, test");

            doc.add_page(Page::a4());

            // Write to file
            let result = doc.save(&file_path);
            assert!(result.is_ok());

            // Read file content to verify metadata is present
            let content = fs::read(&file_path).unwrap();
            let content_str = String::from_utf8_lossy(&content);

            // Check that metadata appears in the PDF
            assert!(content_str.contains("Metadata Persistence Test"));
            assert!(content_str.contains("Test Author"));
        }

        #[test]
        fn test_document_writer_error_handling() {
            let mut doc = Document::new();
            doc.add_page(Page::a4());

            // Test writing to invalid path
            let result = doc.save("/invalid/path/test.pdf");
            assert!(result.is_err());
        }

        #[test]
        fn test_document_page_integration() {
            let mut doc = Document::new();

            // Test different page configurations
            let page1 = Page::a4();
            let page2 = Page::letter();
            let mut page3 = Page::new(500.0, 400.0);

            // Add content to custom page
            page3
                .text()
                .set_font(Font::Helvetica, 10.0)
                .at(25.0, 350.0)
                .write("Custom size page")
                .unwrap();

            doc.add_page(page1);
            doc.add_page(page2);
            doc.add_page(page3);

            assert_eq!(doc.pages.len(), 3);

            // Verify pages maintain their properties (actual dimensions may vary)
            assert!(doc.pages[0].width() > 500.0); // A4 width is reasonable
            assert!(doc.pages[0].height() > 700.0); // A4 height is reasonable
            assert!(doc.pages[1].width() > 500.0); // Letter width is reasonable
            assert!(doc.pages[1].height() > 700.0); // Letter height is reasonable
            assert_eq!(doc.pages[2].width(), 500.0); // Custom width
            assert_eq!(doc.pages[2].height(), 400.0); // Custom height
        }

        #[test]
        fn test_document_content_generation() {
            let temp_dir = TempDir::new().unwrap();
            let file_path = temp_dir.path().join("content.pdf");

            let mut doc = Document::new();
            doc.set_title("Content Generation Test");

            let mut page = Page::a4();

            // Generate content programmatically
            for i in 0..10 {
                let y_pos = 700.0 - (i as f64 * 30.0);
                page.text()
                    .set_font(Font::Helvetica, 12.0)
                    .at(50.0, y_pos)
                    .write(&format!("Generated line {}", i + 1))
                    .unwrap();
            }

            doc.add_page(page);

            // Write and verify
            let result = doc.save(&file_path);
            assert!(result.is_ok());
            assert!(file_path.exists());

            // Verify content was generated
            let metadata = fs::metadata(&file_path).unwrap();
            assert!(metadata.len() > 500); // Should contain substantial content
        }

        #[test]
        fn test_document_buffer_vs_file_write() {
            let temp_dir = TempDir::new().unwrap();
            let file_path = temp_dir.path().join("buffer_vs_file.pdf");

            let mut doc = Document::new();
            doc.set_title("Buffer vs File Test");
            doc.add_page(Page::a4());

            // Write to buffer
            let mut buffer = Vec::new();
            let buffer_result = doc.write(&mut buffer);
            assert!(buffer_result.is_ok());

            // Write to file
            let file_result = doc.save(&file_path);
            assert!(file_result.is_ok());

            // Read file back
            let file_content = fs::read(&file_path).unwrap();

            // Both should be valid PDFs with same structure (timestamps may differ)
            assert!(buffer.starts_with(b"%PDF-1.7"));
            assert!(file_content.starts_with(b"%PDF-1.7"));
            assert!(buffer.ends_with(b"%%EOF\n"));
            assert!(file_content.ends_with(b"%%EOF\n"));

            // Both should contain the same title
            let buffer_str = String::from_utf8_lossy(&buffer);
            let file_str = String::from_utf8_lossy(&file_content);
            assert!(buffer_str.contains("Buffer vs File Test"));
            assert!(file_str.contains("Buffer vs File Test"));
        }

        #[test]
        fn test_document_large_content_handling() {
            let temp_dir = TempDir::new().unwrap();
            let file_path = temp_dir.path().join("large_content.pdf");

            let mut doc = Document::new();
            doc.set_title("Large Content Test");

            let mut page = Page::a4();

            // Add large amount of text content - make it much larger
            let large_text =
                "Lorem ipsum dolor sit amet, consectetur adipiscing elit. ".repeat(200);
            page.text()
                .set_font(Font::Helvetica, 10.0)
                .at(50.0, 750.0)
                .write(&large_text)
                .unwrap();

            doc.add_page(page);

            // Write and verify
            let result = doc.save(&file_path);
            assert!(result.is_ok());
            assert!(file_path.exists());

            // Verify large content was handled properly - reduce expectation
            let metadata = fs::metadata(&file_path).unwrap();
            assert!(metadata.len() > 500); // Should be substantial but realistic
        }

        #[test]
        fn test_document_incremental_building() {
            let temp_dir = TempDir::new().unwrap();
            let file_path = temp_dir.path().join("incremental.pdf");

            let mut doc = Document::new();

            // Build document incrementally
            doc.set_title("Incremental Building Test");

            // Add first page
            let mut page1 = Page::a4();
            page1
                .text()
                .set_font(Font::Helvetica, 12.0)
                .at(50.0, 750.0)
                .write("First page content")
                .unwrap();
            doc.add_page(page1);

            // Add metadata
            doc.set_author("Incremental Author");
            doc.set_subject("Incremental Subject");

            // Add second page
            let mut page2 = Page::a4();
            page2
                .text()
                .set_font(Font::Helvetica, 12.0)
                .at(50.0, 750.0)
                .write("Second page content")
                .unwrap();
            doc.add_page(page2);

            // Add more metadata
            doc.set_keywords("incremental, building, test");

            // Final write
            let result = doc.save(&file_path);
            assert!(result.is_ok());
            assert!(file_path.exists());

            // Verify final state
            assert_eq!(doc.pages.len(), 2);
            assert_eq!(
                doc.metadata.title,
                Some("Incremental Building Test".to_string())
            );
            assert_eq!(doc.metadata.author, Some("Incremental Author".to_string()));
            assert_eq!(
                doc.metadata.subject,
                Some("Incremental Subject".to_string())
            );
            assert_eq!(
                doc.metadata.keywords,
                Some("incremental, building, test".to_string())
            );
        }

        #[test]
        fn test_document_concurrent_page_operations() {
            let mut doc = Document::new();
            doc.set_title("Concurrent Operations Test");

            // Simulate concurrent-like operations
            let mut pages = Vec::new();

            // Create multiple pages
            for i in 0..5 {
                let mut page = Page::a4();
                page.text()
                    .set_font(Font::Helvetica, 12.0)
                    .at(50.0, 750.0)
                    .write(&format!("Concurrent page {i}"))
                    .unwrap();
                pages.push(page);
            }

            // Add all pages
            for page in pages {
                doc.add_page(page);
            }

            assert_eq!(doc.pages.len(), 5);

            // Verify each page maintains its content
            let temp_dir = TempDir::new().unwrap();
            let file_path = temp_dir.path().join("concurrent.pdf");
            let result = doc.save(&file_path);
            assert!(result.is_ok());
        }

        #[test]
        fn test_document_memory_efficiency() {
            let mut doc = Document::new();
            doc.set_title("Memory Efficiency Test");

            // Add multiple pages with content
            for i in 0..10 {
                let mut page = Page::a4();
                page.text()
                    .set_font(Font::Helvetica, 12.0)
                    .at(50.0, 700.0)
                    .write(&format!("Memory test page {i}"))
                    .unwrap();
                doc.add_page(page);
            }

            // Write to buffer to test memory usage
            let mut buffer = Vec::new();
            let result = doc.write(&mut buffer);
            assert!(result.is_ok());
            assert!(!buffer.is_empty());

            // Buffer should be reasonable size
            assert!(buffer.len() < 1_000_000); // Should be less than 1MB for simple content
        }

        #[test]
        fn test_document_creator_producer() {
            let mut doc = Document::new();

            // Default values
            assert_eq!(doc.metadata.creator, Some("oxidize_pdf".to_string()));
            assert!(doc
                .metadata
                .producer
                .as_ref()
                .unwrap()
                .contains("oxidize_pdf"));

            // Set custom values
            doc.set_creator("My Application");
            doc.set_producer("My PDF Library v1.0");

            assert_eq!(doc.metadata.creator, Some("My Application".to_string()));
            assert_eq!(
                doc.metadata.producer,
                Some("My PDF Library v1.0".to_string())
            );
        }

        #[test]
        fn test_document_dates() {
            use chrono::{TimeZone, Utc};

            let mut doc = Document::new();

            // Check default dates are set
            assert!(doc.metadata.creation_date.is_some());
            assert!(doc.metadata.modification_date.is_some());

            // Set specific dates
            let creation_date = Utc.with_ymd_and_hms(2023, 1, 1, 12, 0, 0).unwrap();
            let mod_date = Utc.with_ymd_and_hms(2023, 6, 15, 18, 30, 0).unwrap();

            doc.set_creation_date(creation_date);
            doc.set_modification_date(mod_date);

            assert_eq!(doc.metadata.creation_date, Some(creation_date));
            assert_eq!(doc.metadata.modification_date, Some(mod_date));
        }

        #[test]
        fn test_document_dates_local() {
            use chrono::{Local, TimeZone};

            let mut doc = Document::new();

            // Test setting dates with local time
            let local_date = Local.with_ymd_and_hms(2023, 12, 25, 10, 30, 0).unwrap();
            doc.set_creation_date_local(local_date);

            // Verify it was converted to UTC
            assert!(doc.metadata.creation_date.is_some());
            // Just verify the date was set, don't compare exact values due to timezone complexities
            assert!(doc.metadata.creation_date.is_some());
        }

        #[test]
        fn test_update_modification_date() {
            let mut doc = Document::new();

            let initial_mod_date = doc.metadata.modification_date;
            assert!(initial_mod_date.is_some());

            // Sleep briefly to ensure time difference
            std::thread::sleep(std::time::Duration::from_millis(10));

            doc.update_modification_date();

            let new_mod_date = doc.metadata.modification_date;
            assert!(new_mod_date.is_some());
            assert!(new_mod_date.unwrap() > initial_mod_date.unwrap());
        }

        #[test]
        fn test_document_save_updates_modification_date() {
            let temp_dir = TempDir::new().unwrap();
            let file_path = temp_dir.path().join("mod_date_test.pdf");

            let mut doc = Document::new();
            doc.add_page(Page::a4());

            let initial_mod_date = doc.metadata.modification_date;

            // Sleep briefly to ensure time difference
            std::thread::sleep(std::time::Duration::from_millis(10));

            doc.save(&file_path).unwrap();

            // Modification date should be updated
            assert!(doc.metadata.modification_date.unwrap() > initial_mod_date.unwrap());
        }

        #[test]
        fn test_document_metadata_complete() {
            let mut doc = Document::new();

            // Set all metadata fields
            doc.set_title("Complete Metadata Test");
            doc.set_author("Test Author");
            doc.set_subject("Testing all metadata fields");
            doc.set_keywords("test, metadata, complete");
            doc.set_creator("Test Application v1.0");
            doc.set_producer("oxidize_pdf Test Suite");

            // Verify all fields
            assert_eq!(
                doc.metadata.title,
                Some("Complete Metadata Test".to_string())
            );
            assert_eq!(doc.metadata.author, Some("Test Author".to_string()));
            assert_eq!(
                doc.metadata.subject,
                Some("Testing all metadata fields".to_string())
            );
            assert_eq!(
                doc.metadata.keywords,
                Some("test, metadata, complete".to_string())
            );
            assert_eq!(
                doc.metadata.creator,
                Some("Test Application v1.0".to_string())
            );
            assert_eq!(
                doc.metadata.producer,
                Some("oxidize_pdf Test Suite".to_string())
            );
            assert!(doc.metadata.creation_date.is_some());
            assert!(doc.metadata.modification_date.is_some());
        }

        #[test]
        fn test_document_to_bytes() {
            let mut doc = Document::new();
            doc.set_title("Test Document");
            doc.set_author("Test Author");

            let page = Page::a4();
            doc.add_page(page);

            // Generate PDF as bytes
            let pdf_bytes = doc.to_bytes().unwrap();

            // Basic validation
            assert!(!pdf_bytes.is_empty());
            assert!(pdf_bytes.len() > 100); // Should be reasonable size

            // Check PDF header
            let header = &pdf_bytes[0..5];
            assert_eq!(header, b"%PDF-");

            // Check for some basic PDF structure
            let pdf_str = String::from_utf8_lossy(&pdf_bytes);
            assert!(pdf_str.contains("Test Document"));
            assert!(pdf_str.contains("Test Author"));
        }

        #[test]
        fn test_document_to_bytes_with_config() {
            let mut doc = Document::new();
            doc.set_title("Test Document XRef");

            let page = Page::a4();
            doc.add_page(page);

            let config = crate::writer::WriterConfig {
                use_xref_streams: true,
                use_object_streams: false,
                pdf_version: "1.5".to_string(),
                compress_streams: true,
                incremental_update: false,
            };

            // Generate PDF with custom config
            let pdf_bytes = doc.to_bytes_with_config(config).unwrap();

            // Basic validation
            assert!(!pdf_bytes.is_empty());
            assert!(pdf_bytes.len() > 100);

            // Check PDF header with correct version
            let header = String::from_utf8_lossy(&pdf_bytes[0..8]);
            assert!(header.contains("PDF-1.5"));
        }

        #[test]
        fn test_to_bytes_vs_save_equivalence() {
            use std::fs;
            use tempfile::NamedTempFile;

            // Create two identical documents
            let mut doc1 = Document::new();
            doc1.set_title("Equivalence Test");
            doc1.add_page(Page::a4());

            let mut doc2 = Document::new();
            doc2.set_title("Equivalence Test");
            doc2.add_page(Page::a4());

            // Generate bytes
            let pdf_bytes = doc1.to_bytes().unwrap();

            // Save to file
            let temp_file = NamedTempFile::new().unwrap();
            doc2.save(temp_file.path()).unwrap();
            let file_bytes = fs::read(temp_file.path()).unwrap();

            // Both should generate similar structure (lengths may vary due to timestamps)
            assert!(!pdf_bytes.is_empty());
            assert!(!file_bytes.is_empty());
            assert_eq!(&pdf_bytes[0..5], &file_bytes[0..5]); // PDF headers should match
        }

        #[test]
        fn test_document_set_compress() {
            let mut doc = Document::new();
            doc.set_title("Compression Test");
            doc.add_page(Page::a4());

            // Default should be compressed
            assert!(doc.get_compress());

            // Test with compression enabled
            doc.set_compress(true);
            let compressed_bytes = doc.to_bytes().unwrap();

            // Test with compression disabled
            doc.set_compress(false);
            let uncompressed_bytes = doc.to_bytes().unwrap();

            // Uncompressed should generally be larger (though not always guaranteed)
            assert!(!compressed_bytes.is_empty());
            assert!(!uncompressed_bytes.is_empty());

            // Both should be valid PDFs
            assert_eq!(&compressed_bytes[0..5], b"%PDF-");
            assert_eq!(&uncompressed_bytes[0..5], b"%PDF-");
        }

        #[test]
        fn test_document_compression_config_inheritance() {
            let mut doc = Document::new();
            doc.set_title("Config Inheritance Test");
            doc.add_page(Page::a4());

            // Set document compression to false
            doc.set_compress(false);

            // Create config with compression true (should be overridden)
            let config = crate::writer::WriterConfig {
                use_xref_streams: false,
                use_object_streams: false,
                pdf_version: "1.7".to_string(),
                compress_streams: true,
                incremental_update: false,
            };

            // Document setting should take precedence
            let pdf_bytes = doc.to_bytes_with_config(config).unwrap();

            // Should be valid PDF
            assert!(!pdf_bytes.is_empty());
            assert_eq!(&pdf_bytes[0..5], b"%PDF-");
        }

        #[test]
        fn test_document_metadata_all_fields() {
            let mut doc = Document::new();

            // Set all metadata fields
            doc.set_title("Test Document");
            doc.set_author("John Doe");
            doc.set_subject("Testing PDF metadata");
            doc.set_keywords("test, pdf, metadata");
            doc.set_creator("Test Suite");
            doc.set_producer("oxidize_pdf tests");

            // Verify all fields are set
            assert_eq!(doc.metadata.title.as_deref(), Some("Test Document"));
            assert_eq!(doc.metadata.author.as_deref(), Some("John Doe"));
            assert_eq!(
                doc.metadata.subject.as_deref(),
                Some("Testing PDF metadata")
            );
            assert_eq!(
                doc.metadata.keywords.as_deref(),
                Some("test, pdf, metadata")
            );
            assert_eq!(doc.metadata.creator.as_deref(), Some("Test Suite"));
            assert_eq!(doc.metadata.producer.as_deref(), Some("oxidize_pdf tests"));
            assert!(doc.metadata.creation_date.is_some());
            assert!(doc.metadata.modification_date.is_some());
        }

        #[test]
        fn test_document_add_pages() {
            let mut doc = Document::new();

            // Initially empty
            assert_eq!(doc.page_count(), 0);

            // Add pages
            let page1 = Page::a4();
            let page2 = Page::letter();
            let page3 = Page::legal();

            doc.add_page(page1);
            assert_eq!(doc.page_count(), 1);

            doc.add_page(page2);
            assert_eq!(doc.page_count(), 2);

            doc.add_page(page3);
            assert_eq!(doc.page_count(), 3);

            // Verify we can convert to PDF with multiple pages
            let result = doc.to_bytes();
            assert!(result.is_ok());
        }

        #[test]
        fn test_document_default_font_encoding() {
            let mut doc = Document::new();

            // Initially no default encoding
            assert!(doc.default_font_encoding.is_none());

            // Set default encoding
            doc.set_default_font_encoding(Some(FontEncoding::WinAnsiEncoding));
            assert_eq!(
                doc.default_font_encoding(),
                Some(FontEncoding::WinAnsiEncoding)
            );

            // Change encoding
            doc.set_default_font_encoding(Some(FontEncoding::MacRomanEncoding));
            assert_eq!(
                doc.default_font_encoding(),
                Some(FontEncoding::MacRomanEncoding)
            );
        }

        #[test]
        fn test_document_compression_setting() {
            let mut doc = Document::new();

            // Default should compress
            assert!(doc.compress);

            // Disable compression
            doc.set_compress(false);
            assert!(!doc.compress);

            // Re-enable compression
            doc.set_compress(true);
            assert!(doc.compress);
        }

        #[test]
        fn test_document_with_empty_pages() {
            let mut doc = Document::new();

            // Add empty page
            doc.add_page(Page::a4());

            // Should be able to convert to bytes
            let result = doc.to_bytes();
            assert!(result.is_ok());

            let pdf_bytes = result.unwrap();
            assert!(!pdf_bytes.is_empty());
            assert!(pdf_bytes.starts_with(b"%PDF-"));
        }

        #[test]
        fn test_document_with_multiple_page_sizes() {
            let mut doc = Document::new();

            // Add pages with different sizes
            doc.add_page(Page::a4()); // 595 x 842
            doc.add_page(Page::letter()); // 612 x 792
            doc.add_page(Page::legal()); // 612 x 1008
            doc.add_page(Page::a4()); // Another A4
            doc.add_page(Page::new(200.0, 300.0)); // Custom size

            assert_eq!(doc.page_count(), 5);

            // Verify we have 5 pages
            // Note: Direct page access is not available in public API
            // We verify by successful PDF generation
            let result = doc.to_bytes();
            assert!(result.is_ok());
        }

        #[test]
        fn test_document_metadata_dates() {
            use chrono::Duration;

            let doc = Document::new();

            // Should have creation and modification dates
            assert!(doc.metadata.creation_date.is_some());
            assert!(doc.metadata.modification_date.is_some());

            if let (Some(created), Some(modified)) =
                (doc.metadata.creation_date, doc.metadata.modification_date)
            {
                // Dates should be very close (created during construction)
                let diff = modified - created;
                assert!(diff < Duration::seconds(1));
            }
        }

        #[test]
        fn test_document_builder_pattern() {
            // Test fluent API style
            let mut doc = Document::new();
            doc.set_title("Fluent");
            doc.set_author("Builder");
            doc.set_compress(true);

            assert_eq!(doc.metadata.title.as_deref(), Some("Fluent"));
            assert_eq!(doc.metadata.author.as_deref(), Some("Builder"));
            assert!(doc.compress);
        }

        #[test]
        fn test_xref_streams_functionality() {
            use crate::{Document, Font, Page};

            // Test with xref streams disabled (default)
            let mut doc = Document::new();
            assert!(!doc.use_xref_streams);

            let mut page = Page::a4();
            page.text()
                .set_font(Font::Helvetica, 12.0)
                .at(100.0, 700.0)
                .write("Testing XRef Streams")
                .unwrap();

            doc.add_page(page);

            // Generate PDF without xref streams
            let pdf_without_xref = doc.to_bytes().unwrap();

            // Verify traditional xref is used
            let pdf_str = String::from_utf8_lossy(&pdf_without_xref);
            assert!(pdf_str.contains("xref"), "Traditional xref table not found");
            assert!(
                !pdf_str.contains("/Type /XRef"),
                "XRef stream found when it shouldn't be"
            );

            // Test with xref streams enabled
            doc.enable_xref_streams(true);
            assert!(doc.use_xref_streams);

            // Generate PDF with xref streams
            let pdf_with_xref = doc.to_bytes().unwrap();

            // Verify xref streams are used
            let pdf_str = String::from_utf8_lossy(&pdf_with_xref);
            // XRef streams replace traditional xref tables in PDF 1.5+
            assert!(
                pdf_str.contains("/Type /XRef") || pdf_str.contains("stream"),
                "XRef stream not found when enabled"
            );

            // Verify PDF version is set correctly
            assert!(
                pdf_str.contains("PDF-1.5"),
                "PDF version not set to 1.5 for xref streams"
            );

            // Test fluent interface
            let mut doc2 = Document::new();
            doc2.enable_xref_streams(true);
            doc2.set_title("XRef Streams Test");
            doc2.set_author("oxidize-pdf");

            assert!(doc2.use_xref_streams);
            assert_eq!(doc2.metadata.title.as_deref(), Some("XRef Streams Test"));
            assert_eq!(doc2.metadata.author.as_deref(), Some("oxidize-pdf"));
        }

        #[test]
        fn test_document_save_to_vec() {
            let mut doc = Document::new();
            doc.set_title("Test Save");
            doc.add_page(Page::a4());

            // Test to_bytes
            let bytes_result = doc.to_bytes();
            assert!(bytes_result.is_ok());

            let bytes = bytes_result.unwrap();
            assert!(!bytes.is_empty());
            assert!(bytes.starts_with(b"%PDF-"));
            assert!(bytes.ends_with(b"%%EOF") || bytes.ends_with(b"%%EOF\n"));
        }

        #[test]
        fn test_document_unicode_metadata() {
            let mut doc = Document::new();

            // Set metadata with Unicode characters
            doc.set_title("日本語のタイトル");
            doc.set_author("作者名 😀");
            doc.set_subject("Тема документа");
            doc.set_keywords("كلمات, מפתח, 关键词");

            assert_eq!(doc.metadata.title.as_deref(), Some("日本語のタイトル"));
            assert_eq!(doc.metadata.author.as_deref(), Some("作者名 😀"));
            assert_eq!(doc.metadata.subject.as_deref(), Some("Тема документа"));
            assert_eq!(
                doc.metadata.keywords.as_deref(),
                Some("كلمات, מפתח, 关键词")
            );
        }

        #[test]
        fn test_document_page_iteration() {
            let mut doc = Document::new();

            // Add multiple pages
            for i in 0..5 {
                let mut page = Page::a4();
                let gc = page.graphics();
                gc.begin_text();
                let _ = gc.show_text(&format!("Page {}", i + 1));
                gc.end_text();
                doc.add_page(page);
            }

            // Verify page count
            assert_eq!(doc.page_count(), 5);

            // Verify we can generate PDF with all pages
            let result = doc.to_bytes();
            assert!(result.is_ok());
        }

        #[test]
        fn test_document_with_graphics_content() {
            let mut doc = Document::new();

            let mut page = Page::a4();
            {
                let gc = page.graphics();

                // Add various graphics operations
                gc.save_state();

                // Draw rectangle
                gc.rectangle(100.0, 100.0, 200.0, 150.0);
                gc.stroke();

                // Draw circle (approximated)
                gc.move_to(300.0, 300.0);
                gc.circle(300.0, 300.0, 50.0);
                gc.fill();

                // Add text
                gc.begin_text();
                gc.set_text_position(100.0, 500.0);
                let _ = gc.show_text("Graphics Test");
                gc.end_text();

                gc.restore_state();
            }

            doc.add_page(page);

            // Should produce valid PDF
            let result = doc.to_bytes();
            assert!(result.is_ok());
        }

        #[test]
        fn test_document_producer_version() {
            let doc = Document::new();

            // Producer should contain version
            assert!(doc.metadata.producer.is_some());
            if let Some(producer) = &doc.metadata.producer {
                assert!(producer.contains("oxidize_pdf"));
                assert!(producer.contains(env!("CARGO_PKG_VERSION")));
            }
        }

        #[test]
        fn test_document_empty_metadata_fields() {
            let mut doc = Document::new();

            // Set empty strings
            doc.set_title("");
            doc.set_author("");
            doc.set_subject("");
            doc.set_keywords("");

            // Empty strings should be stored as Some("")
            assert_eq!(doc.metadata.title.as_deref(), Some(""));
            assert_eq!(doc.metadata.author.as_deref(), Some(""));
            assert_eq!(doc.metadata.subject.as_deref(), Some(""));
            assert_eq!(doc.metadata.keywords.as_deref(), Some(""));
        }

        #[test]
        fn test_document_very_long_metadata() {
            let mut doc = Document::new();

            // Create very long strings
            let long_title = "A".repeat(1000);
            let long_author = "B".repeat(500);
            let long_keywords = vec!["keyword"; 100].join(", ");

            doc.set_title(&long_title);
            doc.set_author(&long_author);
            doc.set_keywords(&long_keywords);

            assert_eq!(doc.metadata.title.as_deref(), Some(long_title.as_str()));
            assert_eq!(doc.metadata.author.as_deref(), Some(long_author.as_str()));
            assert!(doc.metadata.keywords.as_ref().unwrap().len() > 500);
        }
    }
}