pdfluent 1.1.0

Pure-Rust PDF SDK with PDF/A, digital signatures, WASM, and experimental (feature-gated) XFA support.
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
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
//! Document operations for the compatible API (lane L2).

// Copyright (c) 2026 Innovation Trigger B.V.
//
// PDFluent is available under two licences, at your option: the GNU AGPLv3, or
// the PDFluent Commercial Licence. See the LICENSE file in this repository --
// that file travels with the copy you received, which a URL does not.

//! Read and write entry points on [`PdfDocument`], the read-side `Page`
//! methods, and the link and outline types. `Document` is an alias for the
//! facade type: the lane adds inherent blocks rather than a wrapper, and the
//! few L2 members that mutate take `&mut self` like every other mutation.

use super::constants;
use super::edit::{AnnotColors, Border, BorderOptions};
use super::geometry::{CompareOp, Matrix, Point, Rect};
use super::{ErrorKind, Page, PageMut, Value};
use crate::document::PdfDocument;
use crate::error::Result;

/// The compatible document handle: the facade type under its lane name.
pub type Document = PdfDocument;

/// One outline entry: level, title, 1-based page, and optional destination.
#[derive(Debug, Clone, PartialEq)]
pub struct TocEntry {
    /// Nesting level, starting at 1.
    pub level: i32,
    /// Entry title.
    pub title: String,
    /// 1-based destination page number.
    pub page: i32,
    /// Follow-on destination, present on full outlines.
    pub dest: Option<TocDest>,
}

/// A follow-on outline destination: kind, page, target, and zoom.
#[derive(Debug, Clone, PartialEq)]
pub struct TocDest {
    /// Destination kind code.
    pub kind: i32,
    /// Zero-based destination page number.
    pub page: i32,
    /// Target point on the page, in points.
    pub target: Point,
    /// Zoom factor, or zero for the default.
    pub zoom: f64,
}

/// Document metadata: the info dict plus format and encryption notes.
#[derive(Debug, Clone, PartialEq)]
pub struct DocMetadata {
    /// Document title.
    pub title: String,
    /// Document author.
    pub author: String,
    /// Document subject.
    pub subject: String,
    /// Document keywords.
    pub keywords: String,
    /// Creating application.
    pub creator: String,
    /// Producing application.
    pub producer: String,
    /// Creation date stamp.
    pub creation_date: String,
    /// Modification date stamp.
    pub mod_date: String,
    /// Format note such as the PDF version.
    pub format: String,
    /// Encryption note, or `None` when unencrypted.
    pub encryption: Option<String>,
    /// Trapping note.
    pub trapped: String,
}

/// One page label run: start page, prefix, first number, and style.
#[derive(Debug, Clone, PartialEq)]
pub struct PageLabel {
    /// Zero-based first page of the run.
    pub start_page: i32,
    /// Label prefix.
    pub prefix: String,
    /// First page number of the run.
    pub first_number: i32,
    /// Numbering style code.
    pub style: String,
}

/// A page set: an explicit list or an inclusive range.
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub enum PageSelection {
    /// Explicit zero-based page numbers.
    List(Vec<i32>),
    /// Inclusive zero-based page range.
    Range(i32, i32),
}

/// An embedded-file handle: index or name.
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub enum EmbItem {
    /// Zero-based embedded-file index.
    Index(i32),
    /// Embedded-file name.
    Name(String),
}

/// Options for adding or updating an embedded file.
#[derive(Debug, Clone, Default)]
#[non_exhaustive]
pub struct EmbFileOptions {
    /// Stored filename.
    pub filename: Option<String>,
    /// Display name.
    pub display_name: Option<String>,
    /// Description text.
    pub description: Option<String>,
}

/// One embedded file: names, description, and size.
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub struct EmbeddedFile {
    /// Embedded-file name.
    pub name: String,
    /// Stored filename.
    pub filename: String,
    /// Description text.
    pub description: String,
    /// Data length, in bytes.
    pub size: usize,
}

/// A chapter/page position for chaptered documents.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub struct Location {
    /// Chapter number.
    pub chapter: i32,
    /// Page number within the chapter.
    pub page: i32,
}

/// An optional-content group: its name and visibility.
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub struct Ocg {
    /// Group name.
    pub name: String,
    /// Whether the group is on.
    pub on: bool,
}

/// Layer visibility updates for `set_layer`.
#[derive(Debug, Clone, Default)]
#[non_exhaustive]
pub struct LayerOptions {
    /// Base visibility state name.
    pub base_state: Option<String>,
    /// Groups to switch on.
    pub on: Vec<i32>,
    /// Groups to switch off.
    pub off: Vec<i32>,
}

/// Membership updates for `set_ocmd`.
#[derive(Debug, Clone, Default)]
#[non_exhaustive]
pub struct OcmdOptions {
    /// Member group numbers.
    pub ocgs: Vec<i32>,
    /// Visibility policy name.
    pub policy: Option<String>,
}

/// An optional-content membership dict: its policy and members.
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub struct OcmdInfo {
    /// Visibility policy name.
    pub policy: String,
    /// Member group numbers.
    pub members: Vec<i32>,
}

/// Options for opening a document: file, bytes, or a fresh empty shell.
#[derive(Debug, Clone, Default)]
#[non_exhaustive]
pub struct DocumentOpenOptions {
    /// Path to open, if any.
    pub filename: Option<String>,
    /// Bytes to open, if any.
    pub data: Option<Vec<u8>>,
    /// File type hint for bytes without a name.
    pub filetype: Option<String>,
    /// Page box for a fresh document.
    pub rect: Option<Rect>,
    /// Page width for a fresh document, in points.
    pub width: f64,
    /// Page height for a fresh document, in points.
    pub height: f64,
    /// Default font size, in points.
    pub fontsize: f64,
    /// Backing archive handle, if any.
    pub archive: Option<Value>,
}

impl DocumentOpenOptions {
    /// Open this path.
    pub fn with_filename(mut self, filename: &str) -> Self {
        self.filename = Some(filename.to_string());
        self
    }

    /// Open these bytes.
    pub fn with_data(mut self, data: Vec<u8>) -> Self {
        self.data = Some(data);
        self
    }

    /// Hint the file type of nameless bytes.
    pub fn with_filetype(mut self, filetype: &str) -> Self {
        self.filetype = Some(filetype.to_string());
        self
    }

    /// Give fresh pages this box.
    pub fn with_rect(mut self, rect: Rect) -> Self {
        self.rect = Some(rect);
        self
    }

    /// Give fresh pages this size in points.
    pub fn with_size(mut self, width: f64, height: f64) -> Self {
        self.width = width;
        self.height = height;
        self
    }

    /// Set the default font size in points.
    pub fn with_fontsize(mut self, fontsize: f64) -> Self {
        self.fontsize = fontsize;
        self
    }

    /// Attach this backing archive handle.
    pub fn with_archive(mut self, archive: Value) -> Self {
        self.archive = Some(archive);
        self
    }
}

/// Options for saving: shared by `save_full`, `write`, `ez_save`, and `tobytes`.
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct SaveSettings {
    /// Garbage-collection level.
    pub garbage: i32,
    /// Clean content streams.
    pub clean: bool,
    /// Deflate streams.
    pub deflate: bool,
    /// Deflate image streams.
    pub deflate_images: bool,
    /// Deflate font streams.
    pub deflate_fonts: bool,
    /// Append incrementally instead of rewriting.
    pub incremental: bool,
    /// Write ASCII-only output.
    pub ascii: bool,
    /// Expand compressed objects.
    pub expand: bool,
    /// Linearise for fast web view.
    pub linear: bool,
    /// Keep the existing file id.
    pub no_new_id: bool,
    /// Regenerate appearances.
    pub appearance: bool,
    /// Pretty-print objects.
    pub pretty: bool,
    /// Encryption selector.
    pub encryption: i32,
    /// Permission bits.
    pub permissions: i32,
    /// Owner password.
    pub owner_password: Option<String>,
    /// User password.
    pub user_password: Option<String>,
    /// Keep metadata.
    pub preserve_metadata: bool,
    /// Pack objects into object streams.
    pub use_objstms: bool,
    /// Deflate effort level.
    pub compression_effort: i32,
    /// Fail when the source needed repair.
    pub raise_on_repair: bool,
    /// Write byte-reproducible output.
    pub reproducible: bool,
}

impl Default for SaveSettings {
    fn default() -> Self {
        Self {
            garbage: 0,
            clean: false,
            deflate: false,
            deflate_images: false,
            deflate_fonts: false,
            incremental: false,
            ascii: false,
            expand: false,
            linear: false,
            no_new_id: false,
            appearance: false,
            pretty: false,
            encryption: 1,
            permissions: 4095,
            owner_password: None,
            user_password: None,
            preserve_metadata: true,
            use_objstms: false,
            compression_effort: 0,
            raise_on_repair: false,
            reproducible: false,
        }
    }
}

impl SaveSettings {
    /// Set the garbage-collection level.
    pub fn with_garbage(mut self, garbage: i32) -> Self {
        self.garbage = garbage;
        self
    }

    /// Clean content streams.
    pub fn with_clean(mut self, clean: bool) -> Self {
        self.clean = clean;
        self
    }

    /// Deflate streams.
    pub fn with_deflate(mut self, deflate: bool) -> Self {
        self.deflate = deflate;
        self
    }

    /// Deflate image streams.
    pub fn with_deflate_images(mut self, deflate_images: bool) -> Self {
        self.deflate_images = deflate_images;
        self
    }

    /// Deflate font streams.
    pub fn with_deflate_fonts(mut self, deflate_fonts: bool) -> Self {
        self.deflate_fonts = deflate_fonts;
        self
    }

    /// Append incrementally instead of rewriting.
    pub fn with_incremental(mut self, incremental: bool) -> Self {
        self.incremental = incremental;
        self
    }

    /// Write ASCII-only output.
    pub fn with_ascii(mut self, ascii: bool) -> Self {
        self.ascii = ascii;
        self
    }

    /// Expand compressed objects.
    pub fn with_expand(mut self, expand: bool) -> Self {
        self.expand = expand;
        self
    }

    /// Linearise for fast web view.
    pub fn with_linear(mut self, linear: bool) -> Self {
        self.linear = linear;
        self
    }

    /// Keep the existing file id.
    pub fn with_no_new_id(mut self, no_new_id: bool) -> Self {
        self.no_new_id = no_new_id;
        self
    }

    /// Regenerate appearances.
    pub fn with_appearance(mut self, appearance: bool) -> Self {
        self.appearance = appearance;
        self
    }

    /// Pretty-print objects.
    pub fn with_pretty(mut self, pretty: bool) -> Self {
        self.pretty = pretty;
        self
    }

    /// Set the encryption selector.
    pub fn with_encryption(mut self, encryption: i32) -> Self {
        self.encryption = encryption;
        self
    }

    /// Set the permission bits.
    pub fn with_permissions(mut self, permissions: i32) -> Self {
        self.permissions = permissions;
        self
    }

    /// Set the owner password.
    pub fn with_owner_password(mut self, owner_password: &str) -> Self {
        self.owner_password = Some(owner_password.to_string());
        self
    }

    /// Set the user password.
    pub fn with_user_password(mut self, user_password: &str) -> Self {
        self.user_password = Some(user_password.to_string());
        self
    }

    /// Keep metadata.
    pub fn with_preserve_metadata(mut self, preserve_metadata: bool) -> Self {
        self.preserve_metadata = preserve_metadata;
        self
    }

    /// Pack objects into object streams.
    pub fn with_use_objstms(mut self, use_objstms: bool) -> Self {
        self.use_objstms = use_objstms;
        self
    }

    /// Set the deflate effort level.
    pub fn with_compression_effort(mut self, compression_effort: i32) -> Self {
        self.compression_effort = compression_effort;
        self
    }

    /// Fail when the source needed repair.
    pub fn with_raise_on_repair(mut self, raise_on_repair: bool) -> Self {
        self.raise_on_repair = raise_on_repair;
        self
    }

    /// Write byte-reproducible output.
    pub fn with_reproducible(mut self, reproducible: bool) -> Self {
        self.reproducible = reproducible;
        self
    }
}

/// Options for page insertion: source span plus what travels with it.
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct InsertPdfOptions {
    /// First source page (-1 for the first page).
    pub from_page: i32,
    /// Last source page (-1 for the last page).
    pub to_page: i32,
    /// Target position (-1 appends).
    pub start_at: i32,
    /// Rotation applied to inserted pages.
    pub rotate: i32,
    /// Carry links over.
    pub links: bool,
    /// Carry annotations over.
    pub annots: bool,
    /// Carry widgets over.
    pub widgets: bool,
    /// Merge duplicate resources.
    pub join_duplicates: bool,
    /// Report progress while inserting.
    pub show_progress: bool,
    /// Finish the insertion.
    pub finish: bool,
}

impl Default for InsertPdfOptions {
    fn default() -> Self {
        Self {
            from_page: -1,
            to_page: -1,
            start_at: -1,
            rotate: -1,
            links: true,
            annots: true,
            widgets: true,
            join_duplicates: false,
            show_progress: false,
            finish: true,
        }
    }
}

impl InsertPdfOptions {
    /// Take source pages `from_page..=to_page` (-1 means the far end).
    pub fn with_span(mut self, from_page: i32, to_page: i32) -> Self {
        self.from_page = from_page;
        self.to_page = to_page;
        self
    }

    /// Insert at this target position (-1 appends).
    pub fn with_start_at(mut self, start_at: i32) -> Self {
        self.start_at = start_at;
        self
    }

    /// Rotate inserted pages by this many degrees.
    pub fn with_rotate(mut self, rotate: i32) -> Self {
        self.rotate = rotate;
        self
    }

    /// Carry links over.
    pub fn with_links(mut self, links: bool) -> Self {
        self.links = links;
        self
    }

    /// Carry annotations over.
    pub fn with_annots(mut self, annots: bool) -> Self {
        self.annots = annots;
        self
    }

    /// Carry widgets over.
    pub fn with_widgets(mut self, widgets: bool) -> Self {
        self.widgets = widgets;
        self
    }

    /// Merge duplicate resources.
    pub fn with_join_duplicates(mut self, join_duplicates: bool) -> Self {
        self.join_duplicates = join_duplicates;
        self
    }

    /// Report progress while inserting.
    pub fn with_show_progress(mut self, show_progress: bool) -> Self {
        self.show_progress = show_progress;
        self
    }

    /// Finish the insertion.
    pub fn with_finish(mut self, finish: bool) -> Self {
        self.finish = finish;
        self
    }
}

/// Options for `scrub`: one switch per removable trace.
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct ScrubOptions {
    /// Drop attached files.
    pub attached_files: bool,
    /// Clean page contents.
    pub clean_pages: bool,
    /// Drop embedded files.
    pub embedded_files: bool,
    /// Drop hidden text.
    pub hidden_text: bool,
    /// Drop JavaScript.
    pub javascript: bool,
    /// Drop metadata.
    pub metadata: bool,
    /// Apply pending redactions.
    pub redactions: bool,
    /// Redact images at this effort level.
    pub redact_images: i32,
    /// Drop links.
    pub remove_links: bool,
    /// Reset form fields.
    pub reset_fields: bool,
    /// Drop annotation responses.
    pub reset_responses: bool,
    /// Drop thumbnails.
    pub thumbnails: bool,
    /// Drop XML metadata.
    pub xml_metadata: bool,
}

impl Default for ScrubOptions {
    fn default() -> Self {
        Self {
            attached_files: true,
            clean_pages: true,
            embedded_files: true,
            hidden_text: true,
            javascript: true,
            metadata: true,
            redactions: true,
            redact_images: 0,
            remove_links: true,
            reset_fields: true,
            reset_responses: true,
            thumbnails: true,
            xml_metadata: true,
        }
    }
}

impl ScrubOptions {
    /// Drop attached files.
    pub fn with_attached_files(mut self, attached_files: bool) -> Self {
        self.attached_files = attached_files;
        self
    }

    /// Clean page contents.
    pub fn with_clean_pages(mut self, clean_pages: bool) -> Self {
        self.clean_pages = clean_pages;
        self
    }

    /// Drop embedded files.
    pub fn with_embedded_files(mut self, embedded_files: bool) -> Self {
        self.embedded_files = embedded_files;
        self
    }

    /// Drop hidden text.
    pub fn with_hidden_text(mut self, hidden_text: bool) -> Self {
        self.hidden_text = hidden_text;
        self
    }

    /// Drop JavaScript.
    pub fn with_javascript(mut self, javascript: bool) -> Self {
        self.javascript = javascript;
        self
    }

    /// Drop metadata.
    pub fn with_metadata(mut self, metadata: bool) -> Self {
        self.metadata = metadata;
        self
    }

    /// Apply pending redactions.
    pub fn with_redactions(mut self, redactions: bool) -> Self {
        self.redactions = redactions;
        self
    }

    /// Redact images at this effort level.
    pub fn with_redact_images(mut self, redact_images: i32) -> Self {
        self.redact_images = redact_images;
        self
    }

    /// Drop links.
    pub fn with_remove_links(mut self, remove_links: bool) -> Self {
        self.remove_links = remove_links;
        self
    }

    /// Reset form fields.
    pub fn with_reset_fields(mut self, reset_fields: bool) -> Self {
        self.reset_fields = reset_fields;
        self
    }

    /// Drop annotation responses.
    pub fn with_reset_responses(mut self, reset_responses: bool) -> Self {
        self.reset_responses = reset_responses;
        self
    }

    /// Drop thumbnails.
    pub fn with_thumbnails(mut self, thumbnails: bool) -> Self {
        self.thumbnails = thumbnails;
        self
    }

    /// Drop XML metadata.
    pub fn with_xml_metadata(mut self, xml_metadata: bool) -> Self {
        self.xml_metadata = xml_metadata;
        self
    }
}

/// Options for `bake`: which live objects to flatten into content.
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct BakeOptions {
    /// Flatten annotations.
    pub annots: bool,
    /// Flatten widgets.
    pub widgets: bool,
}

impl Default for BakeOptions {
    fn default() -> Self {
        Self {
            annots: true,
            widgets: true,
        }
    }
}

/// Options for `set_toc_item`: the updatable destination fields.
#[derive(Debug, Clone, Default)]
#[non_exhaustive]
pub struct TocItemOptions {
    /// Destination kind code.
    pub kind: Option<i32>,
    /// Zero-based destination page.
    pub page: Option<i32>,
    /// Destination URI.
    pub uri: Option<String>,
    /// Entry title.
    pub title: Option<String>,
    /// Target point on the page.
    pub target: Option<Point>,
    /// Destination filename for remote links.
    pub filename: Option<String>,
    /// Zoom factor.
    pub zoom: f64,
}

/// Options for `insert_page`: text plus page styling.
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct InsertPageOptions {
    /// Text lines for the fresh page.
    pub text: Vec<String>,
    /// Font size, in points.
    pub fontsize: f64,
    /// Page width, in points.
    pub width: f64,
    /// Page height, in points.
    pub height: f64,
    /// Font name.
    pub fontname: String,
    /// Font file path, if any.
    pub fontfile: Option<String>,
    /// Text colour components.
    pub color: Vec<f64>,
}

impl Default for InsertPageOptions {
    fn default() -> Self {
        Self {
            text: Vec::new(),
            fontsize: 11.0,
            width: 595.0,
            height: 842.0,
            fontname: "helv".to_string(),
            fontfile: None,
            color: vec![0.0],
        }
    }
}

impl InsertPageOptions {
    /// Put these text lines on the fresh page.
    pub fn with_text(mut self, text: Vec<String>) -> Self {
        self.text = text;
        self
    }

    /// Set the font size in points.
    pub fn with_fontsize(mut self, fontsize: f64) -> Self {
        self.fontsize = fontsize;
        self
    }

    /// Set the page size in points.
    pub fn with_size(mut self, width: f64, height: f64) -> Self {
        self.width = width;
        self.height = height;
        self
    }

    /// Set the font name.
    pub fn with_fontname(mut self, fontname: &str) -> Self {
        self.fontname = fontname.to_string();
        self
    }

    /// Take the font from this file.
    pub fn with_fontfile(mut self, fontfile: &str) -> Self {
        self.fontfile = Some(fontfile.to_string());
        self
    }

    /// Set the text colour components.
    pub fn with_color(mut self, color: Vec<f64>) -> Self {
        self.color = color;
        self
    }
}

/// A document link: owned snapshot data shared with insert/update calls.
#[derive(Debug, Clone, PartialEq)]
pub struct Link {
    /// Link rectangle, in points.
    pub rect: Rect,
    /// Link kind code.
    pub kind: i32,
    /// Target URI for remote links.
    pub uri: String,
    /// Link object number.
    pub xref: i32,
    /// Link identifier.
    pub id: String,
    /// Border styling.
    pub border: Border,
    /// Stroke and fill colours.
    pub colors: AnnotColors,
    /// Link flags bitfield.
    pub flags: i32,
    /// Follow-on destination for local links.
    pub dest: Option<LinkDest>,
    /// Next link on the page, filled in when links are listed.
    pub next: Option<Box<Link>>,
}

/// A link destination: page, target point, and zoom.
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub struct LinkDest {
    /// Zero-based destination page.
    pub page: i32,
    /// Target point on the page, in points.
    pub target: Point,
    /// Zoom factor.
    pub zoom: f64,
}

impl LinkDest {
    /// Build a destination from its page, target point, and zoom.
    pub fn new(page: i32, x: f64, y: f64, zoom: f64) -> Self {
        Self {
            page,
            target: Point { x, y },
            zoom,
        }
    }
}

/// A retained rendering of a page, replayed through `run`.
#[derive(Clone)]
pub struct DisplayList {
    rect: Rect,
}

impl DisplayList {
    /// Page area the rendering covers.
    pub fn rect(&self) -> Rect {
        self.rect
    }

    /// Replay the rendering through a device.
    ///
    /// Replay stays unimplemented in this version; freezing (see
    /// [`Page::get_displaylist`]) works, running it back does not.
    pub fn run(&self, _matrix: Matrix) -> Result<()> {
        let _ = _matrix;
        Err(super::not_supported("DisplayList", "run"))
    }
}

/// Options for layout reads; reserved for the recorded keyword set.
#[derive(Debug, Clone, Copy, Default)]
#[non_exhaustive]
pub struct LayoutOptions {}

/// Resolve a possibly negative page index against a page count.
fn resolve_index(index: i32, count: usize) -> Result<usize> {
    let count_i64 = count as i64;
    let resolved = if index < 0 {
        count_i64 + index as i64
    } else {
        index as i64
    };
    if resolved < 0 || resolved >= count_i64 {
        return Err(ErrorKind::IndexOutOfRange
            .to_error(format!("page index {index} out of range ({count} pages)")));
    }
    Ok(resolved as usize)
}

/// Page object ids in page order.
fn page_ids(doc: &PdfDocument) -> Vec<lopdf::ObjectId> {
    let pages = doc.lopdf().get_pages();
    let mut numbers: Vec<u32> = pages.keys().copied().collect();
    numbers.sort_unstable();
    numbers
        .into_iter()
        .filter_map(|n| pages.get(&n).copied())
        .collect()
}

/// Number of an object as the spec's `i32` xref.
fn xref_of(id: lopdf::ObjectId) -> i32 {
    id.0 as i32
}

/// Look up an object by spec xref, rejecting unknown numbers.
fn object_by_xref(doc: &PdfDocument, xref: i32) -> Result<&lopdf::Object> {
    if xref <= 0 {
        return Err(ErrorKind::BadValue.to_error(format!("bad xref {xref}")));
    }
    // Object numbers are unique per generation; the live object is the one
    // the trailer generation points at, which lopdf stores under its id.
    let found = doc
        .lopdf()
        .objects
        .iter()
        .find(|(id, _)| id.0 == xref as u32)
        .map(|(_, obj)| obj);
    found.ok_or_else(|| ErrorKind::BadValue.to_error(format!("unknown xref {xref}")))
}

/// A PDF number (integer or real) as `f64`.
fn number_as_f64(obj: &lopdf::Object) -> Option<f64> {
    match obj {
        lopdf::Object::Integer(i) => Some(*i as f64),
        lopdf::Object::Real(r) => Some(*r as f64),
        _ => None,
    }
}

/// Read a rectangle array as y-up `[x0, y0, x1, y1]`.
fn array_as_rect(obj: &lopdf::Object) -> Option<[f64; 4]> {
    let items = obj.as_array().ok()?;
    if items.len() != 4 {
        return None;
    }
    Some([
        number_as_f64(&items[0])?,
        number_as_f64(&items[1])?,
        number_as_f64(&items[2])?,
        number_as_f64(&items[3])?,
    ])
}

/// Inherited page-tree lookup: walk from the page dict up through `/Parent`
/// until `key` is found.
fn inherited(
    lopdf: &lopdf::Document,
    mut dict_id: lopdf::ObjectId,
    key: &[u8],
) -> Option<lopdf::Object> {
    for _ in 0..64 {
        let dict = lopdf.get_dictionary(dict_id).ok()?;
        if let Ok(value) = dict.get(key) {
            return Some(value.clone());
        }
        dict_id = dict.get(b"Parent").ok()?.as_reference().ok()?;
    }
    None
}

/// Resolved page boxes (y-up) plus the inherited rotation.
struct PageFrames {
    media: [f64; 4],
    crop: [f64; 4],
    rotation: i32,
}

/// Read the media and crop boxes of a page, applying `/Parent` inheritance
/// and the ISO 32000 fallbacks (crop defaults to media).
fn page_frames(doc: &PdfDocument, index: usize) -> Result<PageFrames> {
    let ids = page_ids(doc);
    let id = *ids.get(index).ok_or_else(|| {
        ErrorKind::IndexOutOfRange.to_error(format!("page index {index} out of range"))
    })?;
    let lopdf = doc.lopdf();
    let media = inherited(lopdf, id, b"MediaBox")
        .as_ref()
        .and_then(array_as_rect)
        .unwrap_or([0.0, 0.0, 595.0, 842.0]);
    let crop = inherited(lopdf, id, b"CropBox")
        .as_ref()
        .and_then(array_as_rect)
        .unwrap_or(media);
    let rotation = inherited(lopdf, id, b"Rotate")
        .as_ref()
        .and_then(number_as_f64)
        .unwrap_or(0.0) as i32;
    Ok(PageFrames {
        media,
        crop,
        rotation: rotation.rem_euclid(360),
    })
}

/// Flip a y-up box into the lane's top-left, y-down [`Rect`].
fn flip_y(box_y_up: [f64; 4], page_top: f64) -> Rect {
    let (x0, y0, x1, y1) = (box_y_up[0], box_y_up[1], box_y_up[2], box_y_up[3]);
    Rect {
        x0: x0.min(x1),
        y0: page_top - y1.max(y0),
        x1: x0.max(x1),
        y1: page_top - y0.min(y1),
    }
}

/// Crop box of a page as a y-down [`Rect`].
fn page_rect(doc: &PdfDocument, index: usize) -> Result<Rect> {
    let frames = page_frames(doc, index)?;
    Ok(flip_y(frames.crop, frames.crop[3].max(frames.crop[1])))
}

/// Read one named page box, defaulting art/bleed/trim to the crop box.
fn named_page_box(doc: &PdfDocument, index: usize, key: &[u8]) -> Result<Rect> {
    let frames = page_frames(doc, index)?;
    let top = frames.crop[3].max(frames.crop[1]);
    if key == b"CropBox" {
        return Ok(flip_y(frames.crop, top));
    }
    if key == b"MediaBox" {
        return Ok(flip_y(frames.media, frames.media[3].max(frames.media[1])));
    }
    let ids = page_ids(doc);
    let id = ids[index];
    let found = inherited(doc.lopdf(), id, key)
        .as_ref()
        .and_then(array_as_rect);
    Ok(flip_y(found.unwrap_or(frames.crop), top))
}

/// Catalog dictionary reference, when the trailer names one.
fn catalog_id(doc: &PdfDocument) -> Option<lopdf::ObjectId> {
    doc.lopdf().trailer.get(b"Root").ok()?.as_reference().ok()
}

/// Minimal PDF object serialiser for the `pdf_catalog` / `pdf_trailer` /
/// `xref_object` reads: names, numbers, strings, arrays, dicts, references,
/// and a placeholder for stream payloads.
fn write_object(out: &mut String, obj: &lopdf::Object, ascii: bool) {
    match obj {
        lopdf::Object::Null => out.push_str("null"),
        lopdf::Object::Boolean(b) => out.push_str(if *b { "true" } else { "false" }),
        lopdf::Object::Integer(i) => out.push_str(&i.to_string()),
        lopdf::Object::Real(r) => out.push_str(&format!("{r}")),
        lopdf::Object::Name(name) => {
            out.push('/');
            out.push_str(&String::from_utf8_lossy(name));
        }
        lopdf::Object::String(bytes, _) => {
            out.push('(');
            for byte in bytes {
                if ascii && !byte.is_ascii() {
                    out.push_str(&format!("\\{byte:03o}"));
                } else {
                    match byte {
                        b'(' | b')' | b'\\' => {
                            out.push('\\');
                            out.push(*byte as char);
                        }
                        _ if byte.is_ascii_graphic() || *byte == b' ' => out.push(*byte as char),
                        _ => out.push_str(&format!("\\{byte:03o}")),
                    }
                }
            }
            out.push(')');
        }
        lopdf::Object::Array(items) => {
            out.push('[');
            for (i, item) in items.iter().enumerate() {
                if i > 0 {
                    out.push(' ');
                }
                write_object(out, item, ascii);
            }
            out.push(']');
        }
        lopdf::Object::Dictionary(dict) => write_dict(out, dict, ascii),
        lopdf::Object::Stream(stream) => {
            write_dict(out, &stream.dict, ascii);
            out.push_str(" stream ... endstream");
        }
        lopdf::Object::Reference(id) => out.push_str(&format!("{} {} R", id.0, id.1)),
    }
}

/// Serialise one dictionary.
fn write_dict(out: &mut String, dict: &lopdf::Dictionary, ascii: bool) {
    out.push_str("<<");
    for (key, value) in dict.iter() {
        out.push(' ');
        out.push('/');
        out.push_str(&String::from_utf8_lossy(key));
        out.push(' ');
        write_object(out, value, ascii);
    }
    out.push_str(" >>");
}

/// Serialise one object to text.
fn object_source(obj: &lopdf::Object, ascii: bool) -> String {
    let mut out = String::new();
    write_object(&mut out, obj, ascii);
    out
}

/// Short type name of an object for `xref_get_key`.
fn object_type_name(obj: &lopdf::Object) -> &'static str {
    match obj {
        lopdf::Object::Null => "null",
        lopdf::Object::Boolean(_) => "bool",
        lopdf::Object::Integer(_) => "int",
        lopdf::Object::Real(_) => "real",
        lopdf::Object::Name(_) => "name",
        lopdf::Object::String(_, _) => "str",
        lopdf::Object::Array(_) => "array",
        lopdf::Object::Dictionary(_) => "dict",
        lopdf::Object::Stream(_) => "stream",
        lopdf::Object::Reference(_) => "ref",
    }
}

/// Catalog name entry (`/PageLayout`, `/PageMode`, `/Lang`) as text.
fn catalog_text(doc: &PdfDocument, key: &[u8]) -> String {
    let Some(id) = catalog_id(doc) else {
        return String::new();
    };
    let Ok(dict) = doc.lopdf().get_dictionary(id) else {
        return String::new();
    };
    let Ok(obj) = dict.get(key) else {
        return String::new();
    };
    match obj {
        lopdf::Object::Name(name) => String::from_utf8_lossy(name).into_owned(),
        lopdf::Object::String(bytes, _) => String::from_utf8_lossy(bytes).into_owned(),
        _ => String::new(),
    }
}

/// The `/AcroForm` dictionary, resolved through references.
fn acroform_dict(doc: &PdfDocument) -> Option<lopdf::Dictionary> {
    let id = catalog_id(doc)?;
    let lopdf = doc.lopdf();
    let form = lopdf
        .get_dictionary(id)
        .ok()?
        .get(b"AcroForm")
        .ok()?
        .clone();
    match form {
        lopdf::Object::Dictionary(dict) => Some(dict.clone()),
        lopdf::Object::Reference(ref_id) => lopdf.get_dictionary(ref_id).ok().cloned(),
        lopdf::Object::Stream(ref stream) => Some(stream.dict.clone()),
        _ => None,
    }
}

/// Layer interface rows over the default configuration's `/Order`.
fn layer_ui_entries(doc: &PdfDocument) -> Vec<Value> {
    let lopdf = doc.lopdf();
    let Some(root) = catalog_id(doc) else {
        return Vec::new();
    };
    let props = lopdf
        .get_dictionary(root)
        .ok()
        .and_then(|catalog| catalog.get(b"OCProperties").ok().cloned())
        .and_then(|obj| match obj {
            lopdf::Object::Dictionary(dict) => Some(dict),
            lopdf::Object::Reference(id) => lopdf.get_dictionary(id).ok().cloned(),
            _ => None,
        });
    let Some(props) = props else {
        return Vec::new();
    };
    let base = props
        .get(b"D")
        .ok()
        .cloned()
        .and_then(|obj| match obj {
            lopdf::Object::Dictionary(dict) => Some(dict),
            lopdf::Object::Reference(id) => lopdf.get_dictionary(id).ok().cloned(),
            _ => None,
        })
        .unwrap_or_default();
    let order = base
        .get(b"Order")
        .ok()
        .cloned()
        .and_then(|obj| match obj {
            lopdf::Object::Array(items) => Some(items),
            lopdf::Object::Reference(id) => lopdf
                .get_object(id)
                .ok()
                .and_then(|o| o.as_array().ok().cloned()),
            _ => None,
        })
        .unwrap_or_default();
    let refs_of = |key: &[u8]| -> Vec<lopdf::ObjectId> {
        base.get(key)
            .ok()
            .and_then(|o| o.as_array().ok().cloned())
            .unwrap_or_default()
            .into_iter()
            .filter_map(|o| o.as_reference().ok())
            .collect()
    };
    let ctx = LayerUiCtx {
        on: refs_of(b"ON"),
        off: refs_of(b"OFF"),
        locked: refs_of(b"Locked"),
        base_on: !matches!(
            base.get(b"BaseState"),
            Ok(lopdf::Object::Name(name)) if name.as_slice() != b"ON"
        ),
        rb_groups: base
            .get(b"RBGroups")
            .ok()
            .and_then(|o| o.as_array().ok().cloned())
            .unwrap_or_default()
            .into_iter()
            .filter_map(|group| {
                let items = match group {
                    lopdf::Object::Array(items) => items,
                    lopdf::Object::Reference(id) => lopdf
                        .get_object(id)
                        .ok()
                        .and_then(|o| o.as_array().ok().cloned())?,
                    _ => return None,
                };
                let mut ids: Vec<lopdf::ObjectId> = items
                    .into_iter()
                    .filter_map(|o| o.as_reference().ok())
                    .collect();
                ids.sort_unstable();
                Some(ids)
            })
            .collect(),
    };
    let mut out = Vec::new();
    walk_layer_order(lopdf, &order, 0, false, &ctx, &mut out);
    out
}

/// Shared state for the `/Order` walk: visibility sets and radio groups.
struct LayerUiCtx {
    on: Vec<lopdf::ObjectId>,
    off: Vec<lopdf::ObjectId>,
    locked: Vec<lopdf::ObjectId>,
    base_on: bool,
    rb_groups: Vec<Vec<lopdf::ObjectId>>,
}

impl LayerUiCtx {
    /// Whether a group shows: the same rule `set_layer_ui_config` toggles.
    fn shown(&self, id: lopdf::ObjectId) -> bool {
        !self.off.contains(&id) && (self.on.contains(&id) || self.base_on)
    }
}

/// One `/Order` level into interface rows.
fn walk_layer_order(
    lopdf: &lopdf::Document,
    items: &[lopdf::Object],
    depth: i32,
    radio: bool,
    ctx: &LayerUiCtx,
    out: &mut Vec<Value>,
) {
    for item in items {
        let resolved = match item {
            lopdf::Object::Reference(id) => lopdf.get_object(*id).ok().cloned(),
            _ => Some(item.clone()),
        };
        match (item, resolved) {
            (lopdf::Object::Reference(id), Some(lopdf::Object::Dictionary(dict))) => {
                let text = match dict.get(b"Name") {
                    Ok(lopdf::Object::String(bytes, _)) => {
                        String::from_utf8_lossy(bytes).into_owned()
                    }
                    Ok(lopdf::Object::Name(name)) => String::from_utf8_lossy(name).into_owned(),
                    _ => String::new(),
                };
                push_layer_row(
                    out,
                    depth,
                    i32::from(ctx.locked.contains(id)),
                    i32::from(ctx.shown(*id)),
                    text,
                    if radio { "radiobox" } else { "checkbox" },
                );
            }
            (_, Some(lopdf::Object::Array(nested))) => {
                let mut ids: Vec<lopdf::ObjectId> = nested
                    .iter()
                    .filter_map(|o| o.as_reference().ok())
                    .collect();
                ids.sort_unstable();
                let nested_radio = ctx.rb_groups.contains(&ids);
                walk_layer_order(lopdf, &nested, depth + 1, nested_radio, ctx, out);
            }
            (_, Some(lopdf::Object::String(bytes, _))) => {
                push_layer_row(
                    out,
                    depth,
                    0,
                    1,
                    String::from_utf8_lossy(&bytes).into_owned(),
                    "label",
                );
            }
            _ => {}
        }
    }
}

/// Append one interface row; the number is the row's own position.
fn push_layer_row(
    out: &mut Vec<Value>,
    depth: i32,
    locked: i32,
    on: i32,
    text: String,
    kind: &str,
) {
    let number = out.len() as i32;
    let mut row = std::collections::HashMap::new();
    row.insert("depth".to_string(), Value::Int(i64::from(depth)));
    row.insert("locked".to_string(), Value::Int(i64::from(locked)));
    row.insert("number".to_string(), Value::Int(i64::from(number)));
    row.insert("on".to_string(), Value::Int(i64::from(on)));
    row.insert("text".to_string(), Value::Str(text));
    row.insert("type".to_string(), Value::Str(kind.to_string()));
    out.push(Value::Dict(row));
}

/// Permission bits: `/Encrypt /P` for encrypted documents, all-granted for
/// plain ones.
fn permission_bits(doc: &PdfDocument) -> i32 {
    let lopdf = doc.lopdf();
    let encrypt = lopdf.trailer.get(b"Encrypt").ok().cloned();
    let dict = encrypt.and_then(|obj| match obj {
        lopdf::Object::Dictionary(dict) => Some(dict.clone()),
        lopdf::Object::Reference(id) => lopdf.get_dictionary(id).ok().cloned(),
        _ => None,
    });
    dict.as_ref()
        .and_then(|d| d.get(b"P").ok()?.as_i64().ok())
        .map(|p| p as i32)
        .unwrap_or(-4)
}

/// Flatten the outline tree into spec entries.
fn flatten_toc(items: &[crate::structure::Outline], level: i32, out: &mut Vec<TocEntry>) {
    for item in items {
        let page = item.page.map(|p| p as i32 + 1).unwrap_or(0);
        out.push(TocEntry {
            level,
            title: item.title.clone(),
            page,
            dest: None,
        });
        flatten_toc(&item.children, level + 1, out);
    }
}

/// Flatten with follow-on destinations from the bookmark actions.
fn flatten_full_toc(
    doc: &PdfDocument,
    items: &[pdf_manip::bookmarks::OutlineEntry],
    level: i32,
    out: &mut Vec<TocEntry>,
) {
    use pdf_manip::bookmarks::{BookmarkAction, FitType};
    for item in items {
        let (kind, page_0, target, zoom) = match &item.action {
            Some(BookmarkAction::GoTo { page, fit }) => {
                let page_0 = (*page as i32).saturating_sub(1);
                let (target, zoom) = match fit {
                    FitType::Xyz { x, y, zoom } => {
                        let height = page_frames(doc, page_0.max(0) as usize)
                            .map(|f| f.crop[3].max(f.crop[1]) - f.crop[1].min(f.crop[3]))
                            .unwrap_or(0.0);
                        let tx = x.unwrap_or(0.0) as f64;
                        let ty = height - y.unwrap_or(0.0) as f64;
                        (Point { x: tx, y: ty }, zoom.unwrap_or(0.0) as f64)
                    }
                    FitType::FitH(top) => (
                        Point {
                            x: 0.0,
                            y: *top as f64,
                        },
                        0.0,
                    ),
                    FitType::FitV(left) => (
                        Point {
                            x: *left as f64,
                            y: 0.0,
                        },
                        0.0,
                    ),
                    FitType::Fit => (Point { x: 0.0, y: 0.0 }, 0.0),
                };
                (constants::LINK_GOTO, page_0, target, zoom)
            }
            Some(BookmarkAction::GoToR { page, .. }) => (
                constants::LINK_GOTOR,
                (*page as i32).saturating_sub(1),
                Point { x: 0.0, y: 0.0 },
                0.0,
            ),
            Some(BookmarkAction::Uri(_)) => {
                (constants::LINK_URI, -1, Point { x: 0.0, y: 0.0 }, 0.0)
            }
            Some(BookmarkAction::Named(_)) => {
                (constants::LINK_NAMED, -1, Point { x: 0.0, y: 0.0 }, 0.0)
            }
            None => (constants::LINK_NONE, -1, Point { x: 0.0, y: 0.0 }, 0.0),
        };
        // The full row mirrors the simple row's page: -1 without a target.
        let sheet = if item.action.is_none() {
            -1
        } else if page_0 >= 0 {
            page_0 + 1
        } else {
            0
        };
        out.push(TocEntry {
            level,
            title: item.title.clone(),
            page: sheet,
            dest: Some(TocDest {
                kind,
                page: page_0,
                target,
                zoom,
            }),
        });
        flatten_full_toc(doc, &item.children, level + 1, out);
    }
}

/// Object numbers of the outline items, in tree order.
fn outline_ids(lopdf: &lopdf::Document, mut dict_id: lopdf::ObjectId, out: &mut Vec<i32>) {
    for _ in 0..4096 {
        let Ok(dict) = lopdf.get_dictionary(dict_id) else {
            return;
        };
        out.push(dict_id.0 as i32);
        if let Ok(first) = dict.get(b"First").and_then(|o| o.as_reference()) {
            let mut kids = Vec::new();
            outline_ids(lopdf, first, &mut kids);
            out.append(&mut kids);
        }
        let Ok(next) = dict.get(b"Next").and_then(|o| o.as_reference()) else {
            return;
        };
        dict_id = next;
    }
}

/// Build the write bytes for `save_full` / `tobytes` / `write`: encryption is
/// applied to a scratch copy so `&self` methods never mutate the handle.
fn rendered_bytes(doc: &PdfDocument, options: &SaveSettings) -> Result<Vec<u8>> {
    if options.linear {
        return Err(crate::Error::Unsupported(
            "compat save with linear=true: linearization is not implemented; \
             save without it, or run qpdf --linearize afterwards."
                .to_string(),
        ));
    }
    if options.encryption == constants::PDF_ENCRYPT_NONE
        || options.encryption == constants::PDF_ENCRYPT_KEEP
    {
        if options.incremental {
            return doc.to_incremental_bytes();
        }
        return doc.to_bytes();
    }
    let algorithm = match options.encryption {
        v if v == constants::PDF_ENCRYPT_AES_256 => pdf_manip::encrypt::EncryptionAlgorithm::Aes256,
        v if v == constants::PDF_ENCRYPT_AES_128 => pdf_manip::encrypt::EncryptionAlgorithm::Aes128,
        v if v == constants::PDF_ENCRYPT_RC4_128 => {
            pdf_manip::encrypt::EncryptionAlgorithm::Rc4_128
        }
        v if v == constants::PDF_ENCRYPT_RC4_40 => pdf_manip::encrypt::EncryptionAlgorithm::Rc4_40,
        other => {
            return Err(ErrorKind::BadValue.to_error(format!("bad encryption {other}")));
        }
    };
    let user_password = options
        .user_password
        .clone()
        .unwrap_or_default()
        .into_bytes();
    let owner_password = options
        .owner_password
        .clone()
        .map(String::into_bytes)
        .unwrap_or_else(|| user_password.clone());
    let config = pdf_manip::encrypt::EncryptConfig {
        user_password,
        owner_password,
        algorithm,
        permissions: pdf_manip::encrypt::Permissions::from_p_value(options.permissions),
    };
    let mut scratch = doc.lopdf().clone();
    let mut buf = Vec::new();
    pdf_manip::encrypt::encrypt_and_save(&mut scratch, &config, &mut buf)?;
    Ok(buf)
}

/// External-object numbers named by a page's `/Resources /XObject`.
fn page_xobject_ids(doc: &PdfDocument, index: usize) -> Result<Vec<i32>> {
    let ids = page_ids(doc);
    let id = *ids.get(index).ok_or_else(|| {
        ErrorKind::IndexOutOfRange.to_error(format!("page index {index} out of range"))
    })?;
    let lopdf = doc.lopdf();
    let resources = inherited(lopdf, id, b"Resources");
    let dict = resources.as_ref().and_then(|obj| match obj {
        lopdf::Object::Dictionary(dict) => Some(dict.clone()),
        lopdf::Object::Reference(ref_id) => lopdf.get_dictionary(*ref_id).ok().cloned(),
        lopdf::Object::Stream(stream) => Some(stream.dict.clone()),
        _ => None,
    });
    let xobjects = dict.as_ref().and_then(|d| {
        d.get(b"XObject").ok().cloned().and_then(|obj| match obj {
            lopdf::Object::Dictionary(dict) => Some(dict.clone()),
            lopdf::Object::Reference(ref_id) => lopdf.get_dictionary(ref_id).ok().cloned(),
            _ => None,
        })
    });
    let mut out = Vec::new();
    if let Some(dict) = xobjects {
        for (_, value) in dict.iter() {
            if let Ok(ref_id) = value.as_reference() {
                out.push(xref_of(ref_id));
            }
        }
    }
    out.sort_unstable();
    Ok(out)
}

impl PdfDocument {
    /// Whether a chapter position exists.
    pub fn contains(&self, location: &Location) -> Result<bool> {
        if location.chapter != 0 || location.page < 0 {
            return Ok(false);
        }
        Ok((location.page as usize) < self.page_count())
    }

    /// Delete the page at an index.
    pub fn delete_at(&mut self, index: i32) -> Result<()> {
        let count = self.page_count();
        let target = resolve_index(index, count)?;
        let mut scratch = self.lopdf().clone();
        let pages = scratch.get_pages();
        let mut numbers: Vec<u32> = pages.keys().copied().collect();
        numbers.sort_unstable();
        let Some(page_number) = numbers.get(target).copied() else {
            return Err(
                ErrorKind::IndexOutOfRange.to_error(format!("page index {index} out of range"))
            );
        };
        let page_id = pages[&page_number];
        // Unlink the page from its parent's /Kids and fix the /Count chain.
        let parent = scratch
            .get_dictionary(page_id)
            .ok()
            .and_then(|d| d.get(b"Parent").ok()?.as_reference().ok());
        scratch.objects.remove(&page_id);
        if let Some(parent_id) = parent {
            if let Ok(dict) = scratch.get_dictionary_mut(parent_id) {
                if let Ok(kids) = dict.get_mut(b"Kids").and_then(|k| k.as_array_mut()) {
                    kids.retain(|kid| kid.as_reference().ok() != Some(page_id));
                }
                let count = dict
                    .get(b"Count")
                    .ok()
                    .and_then(|c| c.as_i64().ok())
                    .unwrap_or(0);
                dict.set(b"Count", lopdf::Object::Integer((count - 1).max(0)));
            }
        }
        *self = Self::from_lopdf(scratch)?;
        Ok(())
    }

    /// Borrow for a managed block.
    pub fn enter(&self) -> &Self {
        self
    }

    /// Leave a managed block.
    pub fn exit(&mut self) -> Result<()> {
        Ok(())
    }

    /// Page at an index.
    pub fn at(&self, index: i32) -> Result<Page<'_>> {
        let target = resolve_index(index, self.page_count())?;
        Ok(Page::new(self, target))
    }

    /// Open a file, bytes, or a fresh shell.
    pub fn open_compat(options: &DocumentOpenOptions) -> Result<Self> {
        if let Some(data) = &options.data {
            if data.is_empty() {
                return Err(ErrorKind::EmptyFile.to_error("empty stream"));
            }
            let pdf = super::open::input_to_pdf_bytes(
                data,
                options.filetype.as_deref(),
                options.filename.as_deref(),
            )?;
            return Self::from_bytes(&pdf);
        }
        if let Some(filename) = &options.filename {
            let bytes = std::fs::read(filename).map_err(|source| {
                if source.kind() == std::io::ErrorKind::NotFound {
                    ErrorKind::MissingFile.to_error(filename.clone())
                } else {
                    crate::Error::Io {
                        source,
                        path: Some(filename.into()),
                    }
                }
            })?;
            if bytes.is_empty() {
                return Err(ErrorKind::EmptyFile.to_error(format!("{filename} is empty")));
            }
            let pdf = super::open::input_to_pdf_bytes(
                &bytes,
                options.filetype.as_deref(),
                Some(filename),
            )?;
            return Self::from_bytes(&pdf);
        }
        // A bare rect or size still opens an empty shell: pages arrive later.
        Ok(Self::create())
    }

    /// Length without emptiness: the spec has no empty check here.
    #[allow(clippy::len_without_is_empty)]
    /// Page count.
    pub fn len(&self) -> usize {
        self.page_count()
    }

    /// Unlock with a password.
    pub fn authenticate(&self, _password: &str) -> Result<i32> {
        let lopdf = self.lopdf();
        // No early return for unencrypted bytes: a file whose empty user
        // password opened it on load still verifies against the retained
        // state, and a never-encrypted file fails both checks below.
        let user = if lopdf.authenticate_user_password(_password).is_ok() {
            2
        } else {
            0
        };
        let owner = if lopdf.authenticate_owner_password(_password).is_ok() {
            4
        } else {
            0
        };
        Ok(user | owner)
    }

    /// Whether incremental saving applies.
    pub fn can_save_incrementally(&self) -> Result<bool> {
        Ok(!self.lopdf().is_encrypted())
    }

    /// Chapter count.
    pub fn chapter_count(&self) -> Result<usize> {
        Ok(1)
    }

    /// Pages in a chapter.
    pub fn chapter_page_count(&self, chapter: i32) -> Result<usize> {
        if chapter != 0 {
            return Err(ErrorKind::BadValue.to_error(format!("no chapter {chapter}")));
        }
        Ok(self.page_count())
    }

    /// Close the document.
    pub fn close(self) {}

    /// Convert a page span to PDF content.
    ///
    /// Returns the bytes of a new PDF holding pages `from_page..=to_page`
    /// (negative indices count back from the last page, as the recorded
    /// `to_page=-1` default does), each rotated by `rotate` degrees. The
    /// source handle is unchanged.
    pub fn convert_to_pdf(&self, from_page: i32, to_page: i32, rotate: i32) -> Result<Vec<u8>> {
        if rotate % 90 != 0 {
            return Err(ErrorKind::BadValue
                .to_error(format!("rotate must be a multiple of 90, got {rotate}")));
        }
        let count = self.page_count();
        let from = resolve_index(from_page, count)?;
        let to = resolve_index(to_page, count)?;
        if to < from {
            return Err(ErrorKind::BadValue
                .to_error(format!("to_page {to_page} is before from_page {from_page}")));
        }
        let pages: Vec<u32> = (from..=to).map(|page| page as u32 + 1).collect();
        let mut out = pdf_manip::pages::extract_pages(self.lopdf(), &pages).map_err(|e| {
            ErrorKind::UnreadableFile.to_error(format!("cannot extract pages: {e}"))
        })?;
        for index in 1..=pages.len() as u32 {
            pdf_manip::pages::rotate_page(&mut out, index, i64::from(rotate)).map_err(|e| {
                ErrorKind::UnreadableFile.to_error(format!("cannot rotate page: {e}"))
            })?;
        }
        let mut bytes = Vec::new();
        out.save_to(&mut bytes)
            .map_err(|source| crate::Error::Io { source, path: None })?;
        Ok(bytes)
    }

    /// Embedded-file count.
    pub fn embfile_count(&self) -> Result<usize> {
        Ok(self.attachments()?.len())
    }

    /// Embedded-file bytes.
    pub fn embfile_get(&self, item: &EmbItem) -> Result<Vec<u8>> {
        let name = self.embfile_name(item)?;
        self.attachment_bytes(&name)?
            .ok_or_else(|| ErrorKind::UnknownKey.to_error(format!("no embedded file {name:?}")))
    }

    /// Embedded-file facts.
    pub fn embfile_info(&self, item: &EmbItem) -> Result<EmbeddedFile> {
        let name = self.embfile_name(item)?;
        let size = self
            .attachments()?
            .into_iter()
            .find(|a| a.name == name)
            .map(|a| a.size)
            .unwrap_or(0);
        Ok(EmbeddedFile {
            name: name.clone(),
            filename: name,
            description: String::new(),
            size,
        })
    }

    /// Embedded-file names.
    pub fn embfile_names(&self) -> Result<Vec<String>> {
        Ok(self.attachments()?.into_iter().map(|a| a.name).collect())
    }

    /// Resolve an embedded-file handle to its name.
    fn embfile_name(&self, item: &EmbItem) -> Result<String> {
        let names = self.embfile_names()?;
        match item {
            EmbItem::Name(name) => {
                if names.iter().any(|n| n == name) {
                    Ok(name.clone())
                } else {
                    Err(ErrorKind::UnknownKey.to_error(format!("no embedded file {name:?}")))
                }
            }
            EmbItem::Index(index) => {
                let target = resolve_index(*index, names.len())?;
                Ok(names[target].clone())
            }
        }
    }

    /// Save with cleanup defaults.
    pub fn ez_save(&self, path: &str, options: &SaveSettings) -> Result<()> {
        self.save_full(path, options)
    }

    /// Position behind a bookmark.
    pub fn find_bookmark(&self, mark: i64) -> Result<Location> {
        if mark < 0 || mark as usize >= self.page_count() {
            return Err(ErrorKind::BadValue.to_error(format!("bad bookmark {mark}")));
        }
        Ok(Location {
            chapter: 0,
            page: mark as i32,
        })
    }

    /// Optional-content group by number.
    pub fn get_layer(&self, config: i32) -> Result<Ocg> {
        let layers = self.get_layers()?;
        let target = resolve_index(config, layers.len())?;
        Ok(layers[target].clone())
    }

    /// Optional-content groups.
    pub fn get_layers(&self) -> Result<Vec<Ocg>> {
        self.get_ocgs()
    }

    /// Reserve a fresh object number.
    pub fn get_new_xref(&mut self) -> Result<i32> {
        let mut scratch = self.lopdf().clone();
        let fresh = scratch.max_id + 1;
        scratch.objects.insert((fresh, 0), lopdf::Object::Null);
        scratch.max_id = fresh;
        *self = Self::from_lopdf(scratch)?;
        Ok(fresh as i32)
    }

    /// Optional-content state of an object.
    pub fn get_oc(&self, xref: i32) -> Result<i32> {
        let obj = object_by_xref(self, xref)?.clone();
        let dict = match &obj {
            lopdf::Object::Dictionary(dict) => Some(dict.clone()),
            lopdf::Object::Stream(stream) => Some(stream.dict.clone()),
            _ => None,
        };
        let Some(dict) = dict else {
            return Ok(0);
        };
        Ok(i32::from(dict.get(b"OC").is_ok()))
    }

    /// Optional-content groups.
    pub fn get_ocgs(&self) -> Result<Vec<Ocg>> {
        let lopdf = self.lopdf();
        let Some(root) = catalog_id(self) else {
            return Ok(Vec::new());
        };
        let props = lopdf
            .get_dictionary(root)
            .ok()
            .and_then(|catalog| catalog.get(b"OCProperties").ok()?.as_reference().ok())
            .and_then(|id| lopdf.get_dictionary(id).ok().cloned());
        let Some(props) = props else {
            return Ok(Vec::new());
        };
        let groups = props
            .get(b"OCGs")
            .ok()
            .and_then(|o| o.as_array().ok())
            .cloned()
            .unwrap_or_default();
        let off: Vec<lopdf::ObjectId> = props
            .get(b"D")
            .ok()
            .and_then(|d| match d {
                lopdf::Object::Dictionary(dict) => dict.get(b"OFF").ok().cloned(),
                lopdf::Object::Reference(id) => {
                    lopdf.get_dictionary(*id).ok()?.get(b"OFF").ok().cloned()
                }
                _ => None,
            })
            .and_then(|o| o.as_array().ok().cloned())
            .unwrap_or_default()
            .into_iter()
            .filter_map(|o| o.as_reference().ok())
            .collect();
        let mut out = Vec::new();
        for group in &groups {
            let Ok(id) = group.as_reference() else {
                continue;
            };
            let name = lopdf
                .get_dictionary(id)
                .ok()
                .and_then(|d| d.get(b"Name").ok().cloned())
                .and_then(|o| match o {
                    lopdf::Object::String(bytes, _) => {
                        Some(String::from_utf8_lossy(&bytes).into_owned())
                    }
                    _ => None,
                })
                .unwrap_or_default();
            out.push(Ocg {
                name,
                on: !off.contains(&id),
            });
        }
        Ok(out)
    }

    /// Membership dict of an object.
    pub fn get_ocmd(&self, xref: i32) -> Result<OcmdInfo> {
        let obj = object_by_xref(self, xref)?.clone();
        let dict = match &obj {
            lopdf::Object::Dictionary(dict) => Some(dict.clone()),
            lopdf::Object::Stream(stream) => Some(stream.dict.clone()),
            _ => None,
        };
        let Some(dict) = dict else {
            return Err(ErrorKind::BadValue.to_error(format!("xref {xref} holds no dict")));
        };
        let policy = match dict.get(b"P").ok() {
            Some(lopdf::Object::Name(name)) => String::from_utf8_lossy(name).into_owned(),
            _ => "AnyOn".to_string(),
        };
        let mut members = Vec::new();
        if let Ok(lopdf::Object::Array(items)) = dict.get(b"OCGs") {
            for item in items {
                if let Ok(id) = item.as_reference() {
                    members.push(xref_of(id));
                }
            }
        }
        Ok(OcmdInfo { policy, members })
    }

    /// Object numbers of the outline.
    pub fn get_outline_xrefs(&self) -> Result<Vec<i32>> {
        let lopdf = self.lopdf();
        let Some(root) = catalog_id(self) else {
            return Ok(Vec::new());
        };
        let first = lopdf
            .get_dictionary(root)
            .ok()
            .and_then(|catalog| catalog.get(b"Outlines").ok()?.as_reference().ok())
            .and_then(|id| {
                lopdf
                    .get_dictionary(id)
                    .ok()?
                    .get(b"First")
                    .ok()?
                    .as_reference()
                    .ok()
            });
        let Some(first) = first else {
            return Ok(Vec::new());
        };
        let mut out = Vec::new();
        outline_ids(lopdf, first, &mut out);
        Ok(out)
    }

    /// Page label runs.
    pub fn get_page_labels(&self) -> Result<Vec<PageLabel>> {
        let lopdf = self.lopdf();
        let Some(root) = catalog_id(self) else {
            return Ok(Vec::new());
        };
        let nums = lopdf
            .get_dictionary(root)
            .ok()
            .and_then(|catalog| catalog.get(b"PageLabels").ok().cloned())
            .and_then(|obj| match obj {
                lopdf::Object::Dictionary(dict) => Some(dict.clone()),
                lopdf::Object::Reference(id) => lopdf.get_dictionary(id).ok().cloned(),
                _ => None,
            })
            .and_then(|dict| dict.get(b"Nums").ok().cloned());
        let Some(lopdf::Object::Array(nums)) = nums else {
            return Ok(Vec::new());
        };
        let mut out = Vec::new();
        for pair in nums.chunks(2) {
            let [start, entry] = pair else { continue };
            let Ok(start_page) = start.as_i64() else {
                continue;
            };
            let dict = match entry {
                lopdf::Object::Dictionary(dict) => Some(dict.clone()),
                lopdf::Object::Reference(id) => lopdf.get_dictionary(*id).ok().cloned(),
                _ => None,
            };
            let Some(dict) = dict else { continue };
            let style = match dict.get(b"S").ok() {
                Some(lopdf::Object::Name(name)) => String::from_utf8_lossy(name).into_owned(),
                _ => "D".to_string(),
            };
            let prefix = match dict.get(b"P").ok() {
                Some(lopdf::Object::String(bytes, _)) => {
                    String::from_utf8_lossy(bytes).into_owned()
                }
                _ => String::new(),
            };
            let first_number = dict
                .get(b"St")
                .ok()
                .and_then(|o| o.as_i64().ok())
                .unwrap_or(1) as i32;
            out.push(PageLabel {
                start_page: start_page as i32,
                prefix,
                first_number,
                style,
            });
        }
        Ok(out)
    }

    /// Pages carrying a label.
    pub fn get_page_numbers(&self, label: &str, only_one: bool) -> Result<Vec<i32>> {
        let mut out = Vec::new();
        for (index, shown) in self.page_labels().into_iter().enumerate() {
            if shown == label {
                out.push(index as i32);
                if only_one {
                    break;
                }
            }
        }
        Ok(out)
    }

    /// External objects on a page.
    pub fn get_page_xobjects(&self, page: i32) -> Result<Vec<i32>> {
        let target = resolve_index(page, self.page_count())?;
        page_xobject_ids(self, target)
    }

    /// Signature flags.
    pub fn get_sigflags(&self) -> Result<i32> {
        Ok(acroform_dict(self)
            .as_ref()
            .and_then(|form| form.get(b"SigFlags").ok()?.as_i64().ok())
            .unwrap_or(0) as i32)
    }

    /// Outline entries.
    pub fn get_toc(&self, simple: bool) -> Result<Vec<TocEntry>> {
        if simple {
            let mut out = Vec::new();
            flatten_toc(&self.outlines()?, 1, &mut out);
            return Ok(out);
        }
        let entries = pdf_manip::bookmarks::read_outline(self.lopdf()).unwrap_or_default();
        let mut out = Vec::new();
        flatten_full_toc(self, &entries, 1, &mut out);
        Ok(out)
    }

    /// XML metadata packet.
    pub fn get_xml_metadata(&self) -> Result<String> {
        let lopdf = self.lopdf();
        let Some(root) = catalog_id(self) else {
            return Ok(String::new());
        };
        let stream_id = lopdf
            .get_dictionary(root)
            .ok()
            .and_then(|catalog| catalog.get(b"Metadata").ok()?.as_reference().ok());
        let Some(stream_id) = stream_id else {
            return Ok(String::new());
        };
        let bytes = lopdf
            .get_object(stream_id)
            .ok()
            .and_then(|o| o.as_stream().ok())
            .and_then(|s| s.decompressed_content().ok())
            .unwrap_or_default();
        Ok(String::from_utf8_lossy(&bytes).into_owned())
    }

    /// Finish opening a fresh document.
    pub fn init_doc(&mut self) -> Result<()> {
        Ok(())
    }

    /// Whether unsaved changes exist.
    pub fn is_dirty(&self) -> Result<bool> {
        // The facade tracks no dirty flag; the bindings keep their own.
        Ok(false)
    }

    /// Whether linearised for fast web view.
    pub fn is_fast_webaccess(&self) -> Result<bool> {
        let linearised = self
            .lopdf()
            .objects
            .get(&(1, 0))
            .and_then(|o| o.as_dict().ok())
            .is_some_and(|dict| dict.get(b"Linearized").is_ok());
        Ok(linearised)
    }

    /// Whether the document holds a form.
    pub fn is_form_pdf(&self) -> Result<bool> {
        let has_fields = acroform_dict(self)
            .as_ref()
            .and_then(|form| form.get(b"Fields").ok())
            .and_then(|o| o.as_array().ok())
            .is_some_and(|fields| !fields.is_empty());
        Ok(has_fields)
    }

    /// Whether the document is a PDF.
    pub fn is_pdf(&self) -> Result<bool> {
        Ok(true)
    }

    /// Whether the document reflows.
    pub fn is_reflowable(&self) -> Result<bool> {
        Ok(false)
    }

    /// Whether the document needed repair.
    pub fn is_repaired(&self) -> Result<bool> {
        Ok(false)
    }

    /// Whether an object is a stream.
    pub fn is_stream(&self, xref: i32) -> Result<bool> {
        Ok(object_by_xref(self, xref)
            .ok()
            .is_some_and(|o| matches!(o, lopdf::Object::Stream(_))))
    }

    /// Document language.
    pub fn language(&self) -> Result<String> {
        Ok(catalog_text(self, b"Lang"))
    }

    /// Last chapter position.
    pub fn last_location(&self) -> Result<Location> {
        Ok(Location {
            chapter: 0,
            page: self.page_count().saturating_sub(1) as i32,
        })
    }

    /// Layer interface configurations.
    ///
    /// One entry per `/Order` node of the default configuration: optional
    /// groups read as checkboxes (radioboxes inside an `/RBGroups` set),
    /// bare strings as labels. Groups added without an order entry (see
    /// `add_layer`) have no interface row, as the recording pins.
    pub fn layer_ui_configs(&self) -> Result<Vec<Value>> {
        Ok(layer_ui_entries(self))
    }

    /// Reflow the document to a box.
    pub fn layout(
        &mut self,
        _rect: Option<Rect>,
        _width: f64,
        _height: f64,
        _fontsize: f64,
    ) -> Result<()> {
        Err(super::not_supported("Document", "layout"))
    }

    /// Page by number.
    pub fn load_page(&self, number: i32) -> Result<Page<'_>> {
        self.at(number)
    }

    /// Chapter position of a page.
    pub fn location_from_page_number(&self, page: i32) -> Result<Location> {
        let target = resolve_index(page, self.page_count())?;
        Ok(Location {
            chapter: 0,
            page: target as i32,
        })
    }

    /// Bookmark for a chapter position.
    pub fn make_bookmark(&self, location: Location) -> Result<i64> {
        if location.chapter != 0 || location.page < 0 || location.page as usize >= self.page_count()
        {
            return Err(ErrorKind::BadValue.to_error(format!(
                "bad location {}/{}",
                location.chapter, location.page
            )));
        }
        Ok(location.page as i64)
    }

    /// Marked-content info.
    pub fn markinfo(&self) -> Result<Value> {
        let lopdf = self.lopdf();
        let Some(root) = catalog_id(self) else {
            return Ok(Value::Dict(Default::default()));
        };
        let dict = lopdf
            .get_dictionary(root)
            .ok()
            .and_then(|catalog| catalog.get(b"MarkInfo").ok().cloned())
            .and_then(|obj| match obj {
                lopdf::Object::Dictionary(dict) => Some(dict.clone()),
                lopdf::Object::Reference(id) => lopdf.get_dictionary(id).ok().cloned(),
                _ => None,
            });
        let mut map = std::collections::HashMap::new();
        if let Some(dict) = dict {
            for key in [b"Marked".as_slice(), b"UserProperties", b"Suspects"] {
                if let Ok(value) = dict.get(key) {
                    let name = String::from_utf8_lossy(key).into_owned();
                    let entry = match value {
                        lopdf::Object::Boolean(b) => Value::Bool(*b),
                        other => Value::Str(object_source(other, false)),
                    };
                    map.insert(name, entry);
                }
            }
        }
        Ok(Value::Dict(map))
    }

    /// Document name.
    pub fn name(&self) -> Result<String> {
        // The facade keeps no path; the bindings carry the name.
        Ok(String::new())
    }

    /// Whether appearances render on view.
    pub fn need_appearances(&self) -> Result<bool> {
        Ok(acroform_dict(self)
            .as_ref()
            .and_then(|form| form.get(b"NeedAppearances").ok())
            .is_some_and(|o| matches!(o, lopdf::Object::Boolean(true))))
    }

    /// Password state code.
    pub fn needs_pass(&self) -> Result<i32> {
        let lopdf = self.lopdf();
        if !lopdf.is_encrypted() {
            return Ok(0);
        }
        // An empty user password opens the file on its own, so no password
        // is needed; anything else asks for one.
        let open = lopdf.authenticate_user_password("").is_ok();
        Ok(i32::from(!open))
    }

    /// Chapter position after this one.
    pub fn next_location(&self, location: Location) -> Result<Location> {
        let count = self.page_count() as i32;
        if location.chapter != 0 || location.page < 0 || location.page + 1 >= count {
            return Err(ErrorKind::IndexOutOfRange.to_error("no next location".to_string()));
        }
        Ok(Location {
            chapter: 0,
            page: location.page + 1,
        })
    }

    /// Full outline entries.
    pub fn outline(&self) -> Result<Vec<TocEntry>> {
        self.get_toc(false)
    }

    /// Page and chaptered-page counts.
    pub fn page_count2(&self) -> Result<(usize, usize)> {
        let count = self.page_count();
        Ok((count, count))
    }

    /// Crop box of a page.
    pub fn page_cropbox(&self, page: i32) -> Result<Rect> {
        let target = resolve_index(page, self.page_count())?;
        page_rect(self, target)
    }

    /// Page number of a chapter position.
    pub fn page_number_from_location(&self, location: Location) -> Result<i32> {
        if location.chapter != 0 || location.page < 0 || location.page as usize >= self.page_count()
        {
            return Err(ErrorKind::BadValue.to_error(format!(
                "bad location {}/{}",
                location.chapter, location.page
            )));
        }
        Ok(location.page)
    }

    /// Object number of a page.
    pub fn page_xref(&self, page: i32) -> Result<i32> {
        let target = resolve_index(page, self.page_count())?;
        Ok(xref_of(page_ids(self)[target]))
    }

    /// Page layout name.
    pub fn pagelayout(&self) -> Result<String> {
        Ok(catalog_text(self, b"PageLayout"))
    }

    /// Page mode name.
    pub fn pagemode(&self) -> Result<String> {
        Ok(catalog_text(self, b"PageMode"))
    }

    /// Catalog source.
    pub fn pdf_catalog(&self) -> Result<String> {
        let Some(id) = catalog_id(self) else {
            return Ok(String::new());
        };
        let dict = self
            .lopdf()
            .get_dictionary(id)
            .map_err(|_| ErrorKind::BadValue.to_error("no catalog".to_string()))?;
        Ok(object_source(
            &lopdf::Object::Dictionary(dict.clone()),
            false,
        ))
    }

    /// Trailer source.
    pub fn pdf_trailer(&self, _compressed: bool, ascii: bool) -> Result<String> {
        Ok(object_source(
            &lopdf::Object::Dictionary(self.lopdf().trailer.clone()),
            ascii,
        ))
    }

    /// Permission bits.
    pub fn permissions(&self) -> Result<i32> {
        Ok(permission_bits(self))
    }

    /// Chapter position before this one.
    pub fn prev_location(&self, location: Location) -> Result<Location> {
        if location.chapter != 0 || location.page <= 0 {
            return Err(ErrorKind::IndexOutOfRange.to_error("no previous location".to_string()));
        }
        Ok(Location {
            chapter: 0,
            page: location.page - 1,
        })
    }

    /// Recolor grey content.
    pub fn recolor(&mut self, _components: i32) -> Result<()> {
        Err(ErrorKind::EngineFault.to_error("recolor is not supported".to_string()))
    }

    /// Fresh view of a page.
    pub fn reload_page(&self, page: Page<'_>) -> Result<Page<'_>> {
        self.at(page.index() as i32)
    }

    /// Resolve named destinations.
    pub fn resolve_names(&mut self) -> Result<()> {
        // Destinations resolve on read; there is nothing to precompute.
        Ok(())
    }

    /// Save to a path with full options.
    pub fn save_full(&self, path: &str, options: &SaveSettings) -> Result<()> {
        let bytes = rendered_bytes(self, options)?;
        std::fs::write(path, bytes).map_err(|source| crate::Error::Io {
            source,
            path: Some(path.into()),
        })?;
        Ok(())
    }

    /// Append unsaved changes.
    pub fn save_incr(&self) -> Result<()> {
        Err(ErrorKind::BadValue.to_error("no backing file to append to".to_string()))
    }

    /// Save a compact snapshot.
    pub fn save_snapshot(&self, path: &str) -> Result<()> {
        self.save_full(path, &SaveSettings::default())
    }

    /// Switch the layer configuration.
    pub fn switch_layer(&mut self, config: i32, _as_default: bool) -> Result<()> {
        let lopdf = self.lopdf_mut();
        let root = lopdf
            .trailer
            .get(b"Root")
            .ok()
            .and_then(|o| o.as_reference().ok())
            .ok_or_else(|| ErrorKind::BadValue.to_error("no catalog".to_string()))?;
        let props_id = lopdf
            .get_dictionary(root)
            .ok()
            .and_then(|catalog| catalog.get(b"OCProperties").ok()?.as_reference().ok())
            .ok_or_else(|| ErrorKind::BadValue.to_error("no optional content".to_string()))?;
        let configs = lopdf
            .get_dictionary(props_id)
            .ok()
            .and_then(|props| props.get(b"Configs").ok()?.as_array().ok().cloned())
            .unwrap_or_default();
        let target = resolve_index(config, configs.len())?;
        let chosen = configs[target].clone();
        let props = lopdf
            .get_dictionary_mut(props_id)
            .map_err(|_| ErrorKind::BadValue.to_error("no optional content".to_string()))?;
        props.set(b"D", chosen);
        Ok(())
    }

    /// Opaque identity token.
    pub fn this(&self) -> usize {
        self as *const Self as usize
    }

    /// Whether the handle is a PDF.
    pub fn this_is_pdf(&self) -> Result<bool> {
        Ok(true)
    }

    /// Save to bytes.
    pub fn tobytes(&self, options: &SaveSettings) -> Result<Vec<u8>> {
        rendered_bytes(self, options)
    }

    /// Incremental version count.
    pub fn version_count(&self) -> Result<usize> {
        Ok(1)
    }

    /// Write to bytes.
    pub fn write(&self, options: &SaveSettings) -> Result<Vec<u8>> {
        rendered_bytes(self, options)
    }

    /// Highest object number.
    pub fn xref(&self) -> Result<i32> {
        Ok(self.lopdf().max_id as i32)
    }

    /// Type and value of an object key.
    pub fn xref_get_key(&self, xref: i32, key: &str) -> Result<(String, String)> {
        let obj = object_by_xref(self, xref)?.clone();
        let dict = match &obj {
            lopdf::Object::Dictionary(dict) => Some(dict.clone()),
            lopdf::Object::Stream(stream) => Some(stream.dict.clone()),
            _ => None,
        };
        let Some(dict) = dict else {
            return Err(ErrorKind::BadValue.to_error(format!("xref {xref} holds no dict")));
        };
        let value = dict
            .get(key.as_bytes())
            .ok()
            .cloned()
            .ok_or_else(|| ErrorKind::UnknownKey.to_error(format!("no key {key:?}")))?;
        Ok((
            object_type_name(&value).to_string(),
            object_source(&value, false),
        ))
    }

    /// Keys of an object.
    pub fn xref_get_keys(&self, xref: i32) -> Result<Vec<String>> {
        let obj = object_by_xref(self, xref)?.clone();
        let dict = match &obj {
            lopdf::Object::Dictionary(dict) => Some(dict.clone()),
            lopdf::Object::Stream(stream) => Some(stream.dict.clone()),
            _ => None,
        };
        let Some(dict) = dict else {
            return Err(ErrorKind::BadValue.to_error(format!("xref {xref} holds no dict")));
        };
        Ok(dict
            .iter()
            .map(|(key, _)| String::from_utf8_lossy(key).into_owned())
            .collect())
    }

    /// Whether an object is a stream.
    pub fn xref_is_stream(&self, xref: i32) -> Result<bool> {
        self.is_stream(xref)
    }

    /// Whether an object is an external object.
    pub fn xref_is_xobject(&self, xref: i32) -> Result<bool> {
        let obj = object_by_xref(self, xref)?.clone();
        let lopdf::Object::Stream(stream) = &obj else {
            return Ok(false);
        };
        Ok(matches!(
            stream.dict.get(b"Subtype").ok(),
            Some(lopdf::Object::Name(name))
                if name.as_slice() == b"Image"
                    || name.as_slice() == b"Form"
                    || name.as_slice() == b"PS"
        ))
    }

    /// Object count.
    pub fn xref_length(&self) -> Result<usize> {
        Ok(self.lopdf().objects.len())
    }

    /// Source of an object.
    pub fn xref_object(&self, xref: i32, _compressed: bool, ascii: bool) -> Result<String> {
        Ok(object_source(object_by_xref(self, xref)?, ascii))
    }

    /// Decoded stream bytes.
    pub fn xref_stream(&self, xref: i32) -> Result<Vec<u8>> {
        let obj = object_by_xref(self, xref)?.clone();
        let lopdf::Object::Stream(stream) = &obj else {
            return Err(ErrorKind::BadValue.to_error(format!("xref {xref} is no stream")));
        };
        stream
            .decompressed_content()
            .map_err(|_| ErrorKind::EngineFault.to_error(format!("cannot decode xref {xref}")))
    }

    /// Raw stream bytes.
    pub fn xref_stream_raw(&self, xref: i32) -> Result<Vec<u8>> {
        let obj = object_by_xref(self, xref)?.clone();
        let lopdf::Object::Stream(stream) = &obj else {
            return Err(ErrorKind::BadValue.to_error(format!("xref {xref} is no stream")));
        };
        Ok(stream.content.clone())
    }

    /// Object number of the XML metadata.
    pub fn xref_xml_metadata(&self) -> Result<i32> {
        let lopdf = self.lopdf();
        let Some(root) = catalog_id(self) else {
            return Ok(0);
        };
        Ok(lopdf
            .get_dictionary(root)
            .ok()
            .and_then(|catalog| catalog.get(b"Metadata").ok()?.as_reference().ok())
            .map(xref_of)
            .unwrap_or(0))
    }

    /// Render appearances on view.
    pub fn set_need_appearances(&mut self, value: bool) -> Result<()> {
        let lopdf = self.lopdf_mut();
        let root = lopdf
            .trailer
            .get(b"Root")
            .ok()
            .and_then(|o| o.as_reference().ok())
            .ok_or_else(|| ErrorKind::BadValue.to_error("no catalog".to_string()))?;
        let form_id = lopdf
            .get_dictionary(root)
            .ok()
            .and_then(|catalog| catalog.get(b"AcroForm").ok()?.as_reference().ok());
        if let Some(form_id) = form_id {
            let form = lopdf
                .get_dictionary_mut(form_id)
                .map_err(|_| ErrorKind::BadValue.to_error("no form".to_string()))?;
            form.set(b"NeedAppearances", lopdf::Object::Boolean(value));
        } else {
            let fresh = lopdf.max_id + 1;
            let mut form = lopdf::Dictionary::new();
            form.set(b"NeedAppearances", lopdf::Object::Boolean(value));
            lopdf
                .objects
                .insert((fresh, 0), lopdf::Object::Dictionary(form));
            lopdf.max_id = fresh;
            let catalog = lopdf
                .get_dictionary_mut(root)
                .map_err(|_| ErrorKind::BadValue.to_error("no catalog".to_string()))?;
            catalog.set(b"AcroForm", lopdf::Object::Reference((fresh, 0)));
        }
        Ok(())
    }

    /// Compare against another value; ordering is unsupported.
    pub fn compare(&self, _other: &Self, _op: CompareOp) -> Result<bool> {
        Err(ErrorKind::BadValue.to_error("documents do not order".to_string()))
    }
}

/// Content stream object ids of a page, in `/Contents` order.
fn content_ids(doc: &PdfDocument, index: usize) -> Result<Vec<lopdf::ObjectId>> {
    let ids = page_ids(doc);
    let id = *ids.get(index).ok_or_else(|| {
        ErrorKind::IndexOutOfRange.to_error(format!("page index {index} out of range"))
    })?;
    let contents = inherited(doc.lopdf(), id, b"Contents");
    let mut out = Vec::new();
    match contents {
        Some(lopdf::Object::Reference(ref_id)) => out.push(ref_id),
        Some(lopdf::Object::Array(items)) => {
            for item in &items {
                if let Ok(ref_id) = item.as_reference() {
                    out.push(ref_id);
                }
            }
        }
        _ => {}
    }
    Ok(out)
}

/// Clockwise rotation matrix in the y-down frame: right turns down.
fn clockwise_matrix(degrees: i32) -> Matrix {
    match degrees.rem_euclid(360) {
        90 => Matrix {
            a: 0.0,
            b: 1.0,
            c: -1.0,
            d: 0.0,
            e: 0.0,
            f: 0.0,
        },
        180 => Matrix {
            a: -1.0,
            b: 0.0,
            c: 0.0,
            d: -1.0,
            e: 0.0,
            f: 0.0,
        },
        270 => Matrix {
            a: 0.0,
            b: -1.0,
            c: 1.0,
            d: 0.0,
            e: 0.0,
            f: 0.0,
        },
        _ => Matrix {
            a: 1.0,
            b: 0.0,
            c: 0.0,
            d: 1.0,
            e: 0.0,
            f: 0.0,
        },
    }
}

impl<'a> Page<'a> {
    /// Art box.
    pub fn artbox(&self) -> Result<Rect> {
        named_page_box(self.document(), self.index(), b"ArtBox")
    }

    /// Bleed box.
    pub fn bleedbox(&self) -> Result<Rect> {
        named_page_box(self.document(), self.index(), b"BleedBox")
    }

    /// Content bounding box.
    pub fn bound(&self) -> Result<Rect> {
        // The lane has no content walker; the crop box is the outer bound.
        self.cropbox()
    }

    /// Crop box.
    pub fn cropbox(&self) -> Result<Rect> {
        named_page_box(self.document(), self.index(), b"CropBox")
    }

    /// Crop box offset.
    pub fn cropbox_position(&self) -> Result<Point> {
        let frames = page_frames(self.document(), self.index())?;
        Ok(Point {
            x: frames.crop[0].min(frames.crop[2]) - frames.media[0].min(frames.media[2]),
            y: frames.media[3].max(frames.media[1]) - frames.crop[3].max(frames.crop[1]),
        })
    }

    /// Matrix undoing the rotation.
    pub fn derotation_matrix(&self) -> Result<Matrix> {
        let rotation = page_frames(self.document(), self.index())?.rotation;
        Ok(clockwise_matrix(-rotation))
    }

    /// Box change log: line-metric boxes in content-stream order.
    ///
    /// A request for layers (any `layers` but none, false or an empty list)
    /// adds the layer name to every entry; the recorded default and empty
    /// list stay two-element.
    pub fn get_bboxlog(&self, layers: Option<&Value>) -> Result<Value> {
        let with_layer = match layers {
            None | Some(Value::Null) => false,
            Some(Value::Bool(found)) => *found,
            Some(Value::List(items)) => !items.is_empty(),
            Some(_) => true,
        };
        super::edit::bboxlog_entries(self.document(), self.index(), with_layer)
    }

    /// Content object numbers.
    pub fn get_contents(&self) -> Result<Vec<i32>> {
        Ok(content_ids(self.document(), self.index())?
            .into_iter()
            .map(xref_of)
            .collect())
    }

    /// Retained rendering of the page.
    ///
    /// The recording pins the page box with and without marks, so the
    /// flag is accepted and the answer is the box either way.
    pub fn get_displaylist(&self, _annots: bool) -> Result<DisplayList> {
        let _ = _annots;
        Ok(DisplayList {
            rect: page_rect(self.document(), self.index())?,
        })
    }

    /// Page label.
    pub fn get_label(&self) -> Result<String> {
        self.document()
            .page_labels()
            .get(self.index())
            .cloned()
            .ok_or_else(|| {
                ErrorKind::IndexOutOfRange
                    .to_error(format!("page index {} out of range", self.index()))
            })
    }

    /// Reflowed layout, if any.
    pub fn get_layout(&self, _options: &LayoutOptions) -> Result<Option<Value>> {
        Ok(None)
    }

    /// Optional-content items.
    pub fn get_oc_items(&self) -> Result<Vec<Value>> {
        Ok(Vec::new())
    }

    /// External object numbers.
    pub fn get_xobjects(&self) -> Result<Vec<i32>> {
        page_xobject_ids(self.document(), self.index())
    }

    /// Whether contents are wrapped.
    pub fn is_wrapped(&self) -> Result<bool> {
        Ok(false)
    }

    /// Page language.
    pub fn language(&self) -> Result<String> {
        let doc = self.document();
        let ids = page_ids(doc);
        let id = *ids.get(self.index()).ok_or_else(|| {
            ErrorKind::IndexOutOfRange.to_error(format!("page index {} out of range", self.index()))
        })?;
        let lang = inherited(doc.lopdf(), id, b"Lang").and_then(|obj| match obj {
            lopdf::Object::String(bytes, _) => Some(String::from_utf8_lossy(&bytes).into_owned()),
            _ => None,
        });
        Ok(lang.unwrap_or_default())
    }

    /// Media box.
    pub fn mediabox(&self) -> Result<Rect> {
        named_page_box(self.document(), self.index(), b"MediaBox")
    }

    /// Media box size.
    pub fn mediabox_size(&self) -> Result<(f64, f64)> {
        let media = self.mediabox()?;
        Ok(((media.x1 - media.x0).abs(), (media.y1 - media.y0).abs()))
    }

    /// Content stream bytes.
    pub fn read_contents(&self) -> Result<Vec<u8>> {
        let doc = self.document();
        let mut out = Vec::new();
        for id in content_ids(doc, self.index())? {
            let bytes = doc
                .lopdf()
                .get_object(id)
                .ok()
                .and_then(|o| o.as_stream().ok())
                .and_then(|s| s.decompressed_content().ok())
                .unwrap_or_default();
            if !out.is_empty() {
                out.push(b'\n');
            }
            out.extend_from_slice(&bytes);
        }
        Ok(out)
    }

    /// Page rectangle.
    pub fn rect(&self) -> Result<Rect> {
        page_rect(self.document(), self.index())
    }

    /// Rotation in degrees.
    pub fn rotation(&self) -> Result<i32> {
        Ok(page_frames(self.document(), self.index())?.rotation)
    }

    /// Replay a retained rendering.
    pub fn run(&self, _display_list: &DisplayList, _matrix: Matrix) -> Result<()> {
        Err(ErrorKind::EngineFault.to_error("page replay is not supported".to_string()))
    }

    /// Matrix applying the rotation.
    pub fn rotation_matrix(&self) -> Result<Matrix> {
        let rotation = page_frames(self.document(), self.index())?.rotation;
        Ok(clockwise_matrix(rotation))
    }

    /// Page transformation matrix.
    pub fn transformation_matrix(&self) -> Result<Matrix> {
        // Map y-up PDF points onto the y-down lane frame, then rotate.
        let frames = page_frames(self.document(), self.index())?;
        let top = frames.crop[3].max(frames.crop[1]);
        let rot = clockwise_matrix(frames.rotation);
        // Product `rot * flip`: flip first, then rotate.
        Ok(Matrix {
            a: rot.a,
            b: rot.b,
            c: -rot.c,
            d: -rot.d,
            e: rot.c.mul_add(top, rot.e),
            f: rot.d.mul_add(top, rot.f),
        })
    }

    /// Trim box.
    pub fn trimbox(&self) -> Result<Rect> {
        named_page_box(self.document(), self.index(), b"TrimBox")
    }

    /// Page object number.
    pub fn xref(&self) -> Result<i32> {
        let ids = page_ids(self.document());
        ids.get(self.index()).copied().map(xref_of).ok_or_else(|| {
            ErrorKind::IndexOutOfRange.to_error(format!("page index {} out of range", self.index()))
        })
    }

    /// Compare against another value; ordering is unsupported.
    pub fn compare(&self, _other: &Self, _op: CompareOp) -> Result<bool> {
        Err(ErrorKind::BadValue.to_error("pages do not order".to_string()))
    }
}

impl<'a> PageMut<'a> {
    /// Clip contents to a rectangle.
    pub fn clip_to_rect(&mut self, rect: Rect) -> Result<()> {
        // Prepend a rectangular clip path; coordinates arrive y-down and go
        // back to y-up for the content stream.
        let frames = page_frames(self.document(), self.index())?;
        let top = frames.crop[3].max(frames.crop[1]);
        let x = rect.x0.min(rect.x1);
        let w = (rect.x1 - rect.x0).abs();
        let y_up_bottom = top - rect.y1.max(rect.y0);
        let h = (rect.y1 - rect.y0).abs();
        let prefix = format!("q {x} {y_up_bottom} {w} {h} re W n\n");
        self.splice_contents(prefix.as_bytes(), b"Q\n")
    }

    /// Recolor grey content.
    pub fn recolor(&mut self, _components: i32) -> Result<()> {
        Err(ErrorKind::EngineFault.to_error("recolor is not supported".to_string()))
    }

    /// Reload the page view.
    pub fn refresh(&mut self) -> Result<()> {
        // Reads always come straight from the document; nothing is cached.
        Ok(())
    }

    /// Wrap contents for isolated drawing.
    pub fn wrap_contents(&mut self) -> Result<()> {
        self.splice_contents(b"q\n", b"Q\n")
    }

    /// Surround every content stream of the page with two byte runs.
    fn splice_contents(&mut self, prefix: &[u8], suffix: &[u8]) -> Result<()> {
        let index = self.index();
        let doc = self.document_mut();
        let ids = content_ids(doc, index)?;
        for id in ids {
            let stream = doc
                .lopdf_mut()
                .get_object_mut(id)
                .ok()
                .and_then(|o| o.as_stream_mut().ok())
                .ok_or_else(|| ErrorKind::BadValue.to_error("page content is no stream"))?;
            let mut content = stream
                .decompressed_content()
                .unwrap_or_else(|_| stream.content.clone());
            let mut joined = Vec::with_capacity(prefix.len() + content.len() + suffix.len());
            joined.extend_from_slice(prefix);
            joined.append(&mut content);
            joined.extend_from_slice(suffix);
            stream.set_content(joined);
        }
        Ok(())
    }
}

impl Link {
    /// Blank link to fill and insert.
    pub fn new() -> Self {
        Self {
            rect: Rect {
                x0: 0.0,
                y0: 0.0,
                x1: 0.0,
                y1: 0.0,
            },
            kind: constants::LINK_NONE,
            uri: String::new(),
            xref: 0,
            id: String::new(),
            border: Border {
                width: 0.0,
                width_is_int: false,
                style: None,
                dashes: Vec::new(),
                clouds: 0,
            },
            colors: AnnotColors {
                stroke: Vec::new(),
                fill: Vec::new(),
            },
            flags: 0,
            dest: None,
            next: None,
        }
    }

    /// Border styling.
    pub fn border(&self) -> Border {
        self.border.clone()
    }

    /// Stroke and fill colours.
    pub fn colors(&self) -> AnnotColors {
        self.colors.clone()
    }

    /// Follow-on destination for local links.
    pub fn dest(&self) -> Option<LinkDest> {
        self.dest.clone()
    }

    /// Link flags bitfield.
    pub fn flags(&self) -> i32 {
        self.flags
    }

    /// Whether the link leaves the document.
    pub fn is_external(&self) -> bool {
        !matches!(self.kind, v if v == constants::LINK_NONE || v == constants::LINK_GOTO)
    }

    /// Next link on the page.
    pub fn next(&self) -> Option<Link> {
        self.next.as_deref().cloned()
    }

    /// Link rectangle.
    pub fn rect(&self) -> Rect {
        self.rect
    }

    /// Set the border styling.
    pub fn set_border(&mut self, options: &BorderOptions) {
        self.border = Border {
            width: options.width,
            width_is_int: false,
            style: options.style.clone(),
            dashes: options.dashes.clone(),
            clouds: options.clouds,
        };
    }

    /// Set stroke and fill colours.
    pub fn set_colors(&mut self, colors: &AnnotColors) {
        self.colors = colors.clone();
    }

    /// Set the flags bitfield.
    pub fn set_flags(&mut self, flags: i32) {
        self.flags = flags;
    }

    /// Target URI for remote links.
    pub fn uri(&self) -> String {
        self.uri.clone()
    }

    /// Compare against another value; ordering is unsupported.
    pub fn compare(&self, _other: &Self, _op: CompareOp) -> Result<bool> {
        Err(ErrorKind::BadValue.to_error("links do not order".to_string()))
    }
}

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

impl PartialEq for PdfDocument {
    fn eq(&self, other: &Self) -> bool {
        std::ptr::eq(self, other)
    }
}

impl<'a> PartialEq for Page<'a> {
    fn eq(&self, other: &Self) -> bool {
        self.index() == other.index() && std::ptr::eq(self.document(), other.document())
    }
}