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
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
|
# Copyright (c) 2006, Mathieu Fenniak
# Copyright (c) 2007, Ashish Kulkarni <kulkarni.ashish@gmail.com>
#
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
# * Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
# * The name of the author may not be used to endorse or promote products
# derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
import collections
import decimal
import enum
import hashlib
import re
import uuid
from io import BytesIO, FileIO, IOBase
from pathlib import Path
from types import TracebackType
from typing import (
IO,
Any,
Callable,
Deque,
Dict,
Iterable,
List,
Optional,
Pattern,
Tuple,
Type,
Union,
cast,
)
from ._cmap import _default_fonts_space_width, build_char_map_from_dict
from ._doc_common import PdfDocCommon
from ._encryption import EncryptAlgorithm, Encryption
from ._page import PageObject
from ._page_labels import nums_clear_range, nums_insert, nums_next
from ._reader import PdfReader
from ._utils import (
StrByteType,
StreamType,
_get_max_pdf_version_header,
b_,
deprecate_with_replacement,
logger_warning,
)
from .constants import AnnotationDictionaryAttributes as AA
from .constants import CatalogAttributes as CA
from .constants import (
CatalogDictionary,
FileSpecificationDictionaryEntries,
GoToActionArguments,
ImageType,
InteractiveFormDictEntries,
PageLabelStyle,
TypFitArguments,
UserAccessPermissions,
)
from .constants import Core as CO
from .constants import FieldDictionaryAttributes as FA
from .constants import PageAttributes as PG
from .constants import PagesAttributes as PA
from .constants import TrailerKeys as TK
from .errors import PyPdfError
from .generic import (
PAGE_FIT,
ArrayObject,
BooleanObject,
ByteStringObject,
ContentStream,
DecodedStreamObject,
Destination,
DictionaryObject,
Fit,
FloatObject,
IndirectObject,
NameObject,
NullObject,
NumberObject,
PdfObject,
RectangleObject,
StreamObject,
TextStringObject,
TreeObject,
ViewerPreferences,
create_string_object,
hex_to_rgb,
)
from .pagerange import PageRange, PageRangeSpec
from .types import (
AnnotationSubtype,
BorderArrayType,
LayoutType,
OutlineItemType,
OutlineType,
PagemodeType,
)
from .xmp import XmpInformation
ALL_DOCUMENT_PERMISSIONS = UserAccessPermissions.all()
DEFAULT_FONT_HEIGHT_IN_MULTILINE = 12
class ObjectDeletionFlag(enum.IntFlag):
NONE = 0
TEXT = enum.auto()
LINKS = enum.auto()
ATTACHMENTS = enum.auto()
OBJECTS_3D = enum.auto()
ALL_ANNOTATIONS = enum.auto()
XOBJECT_IMAGES = enum.auto()
INLINE_IMAGES = enum.auto()
DRAWING_IMAGES = enum.auto()
IMAGES = XOBJECT_IMAGES | INLINE_IMAGES | DRAWING_IMAGES
def _rolling_checksum(stream: BytesIO, blocksize: int = 65536) -> str:
hash = hashlib.md5()
for block in iter(lambda: stream.read(blocksize), b""):
hash.update(block)
return hash.hexdigest()
class PdfWriter(PdfDocCommon):
"""
Write a PDF file out, given pages produced by another class or through
cloning a PDF file during initialization.
Typically data is added from a :class:`PdfReader<pypdf.PdfReader>`.
"""
def __init__(
self,
fileobj: Union[None, PdfReader, StrByteType, Path] = "",
clone_from: Union[None, PdfReader, StrByteType, Path] = None,
) -> None:
self._header = b"%PDF-1.3"
self._objects: List[PdfObject] = []
"""The indirect objects in the PDF."""
self._idnum_hash: Dict[bytes, IndirectObject] = {}
"""Maps hash values of indirect objects to their IndirectObject instances."""
self._id_translated: Dict[int, Dict[int, int]] = {}
# The root of our page tree node.
pages = DictionaryObject()
pages.update(
{
NameObject(PA.TYPE): NameObject("/Pages"),
NameObject(PA.COUNT): NumberObject(0),
NameObject(PA.KIDS): ArrayObject(),
}
)
self._pages = self._add_object(pages)
self.flattened_pages = []
# info object
info = DictionaryObject()
info.update({NameObject("/Producer"): create_string_object("pypdf")})
self._info_obj: PdfObject = self._add_object(info)
# root object
self._root_object = DictionaryObject()
self._root_object.update(
{
NameObject(PA.TYPE): NameObject(CO.CATALOG),
NameObject(CO.PAGES): self._pages,
}
)
self._root = self._add_object(self._root_object)
def _get_clone_from(
fileobj: Union[None, PdfReader, str, Path, IO[Any], BytesIO],
clone_from: Union[None, PdfReader, str, Path, IO[Any], BytesIO],
) -> Union[None, PdfReader, str, Path, IO[Any], BytesIO]:
if not isinstance(fileobj, (str, Path, IO, BytesIO)) or (
fileobj != "" and clone_from is None
):
cloning = True
if not (
not isinstance(fileobj, (str, Path))
or (
Path(str(fileobj)).exists()
and Path(str(fileobj)).stat().st_size > 0
)
):
cloning = False
if isinstance(fileobj, (IO, BytesIO)):
t = fileobj.tell()
fileobj.seek(-1, 2)
if fileobj.tell() == 0:
cloning = False
fileobj.seek(t, 0)
if cloning:
clone_from = fileobj
return clone_from
clone_from = _get_clone_from(fileobj, clone_from)
# to prevent overwriting
self.temp_fileobj = fileobj
self.fileobj = ""
self.with_as_usage = False
if clone_from is not None:
if not isinstance(clone_from, PdfReader):
clone_from = PdfReader(clone_from)
self.clone_document_from_reader(clone_from)
self._encryption: Optional[Encryption] = None
self._encrypt_entry: Optional[DictionaryObject] = None
self._ID: Union[ArrayObject, None] = None
# for commonality
@property
def is_encrypted(self) -> bool:
"""
Read-only boolean property showing whether this PDF file is encrypted.
Note that this property, if true, will remain true even after the
:meth:`decrypt()<pypdf.PdfReader.decrypt>` method is called.
"""
return False
@property
def root_object(self) -> DictionaryObject:
"""
Provide direct access to PDF Structure.
Note:
Recommended only for read access.
"""
return self._root_object
@property
def _info(self) -> Optional[DictionaryObject]:
"""
Provide access to "/Info". Standardized with PdfReader.
Returns:
/Info Dictionary; None if the entry does not exist
"""
return cast(DictionaryObject, self._info_obj.get_object())
@_info.setter
def _info(self, value: Union[IndirectObject, DictionaryObject]) -> None:
obj = cast(DictionaryObject, self._info_obj.get_object())
obj.clear()
obj.update(cast(DictionaryObject, value.get_object()))
@property
def xmp_metadata(self) -> Optional[XmpInformation]:
"""XMP (Extensible Metadata Platform) data."""
return cast(XmpInformation, self.root_object.xmp_metadata)
@xmp_metadata.setter
def xmp_metadata(self, value: Optional[XmpInformation]) -> None:
"""XMP (Extensible Metadata Platform) data."""
if value is None:
if "/Metadata" in self.root_object:
del self.root_object["/Metadata"]
else:
self.root_object[NameObject("/Metadata")] = value
return self.root_object.xmp_metadata # type: ignore
def __enter__(self) -> "PdfWriter":
"""Store that writer is initialized by 'with'."""
t = self.temp_fileobj
self.__init__() # type: ignore
self.with_as_usage = True
self.fileobj = t # type: ignore
return self
def __exit__(
self,
exc_type: Optional[Type[BaseException]],
exc: Optional[BaseException],
traceback: Optional[TracebackType],
) -> None:
"""Write data to the fileobj."""
if self.fileobj:
self.write(self.fileobj)
def _repr_mimebundle_(
self,
include: Union[None, Iterable[str]] = None,
exclude: Union[None, Iterable[str]] = None,
) -> Dict[str, Any]:
"""
Integration into Jupyter Notebooks.
This method returns a dictionary that maps a mime-type to its
representation.
See https://ipython.readthedocs.io/en/stable/config/integrating.html
"""
pdf_data = BytesIO()
self.write(pdf_data)
data = {
"application/pdf": pdf_data,
}
if include is not None:
# Filter representations based on include list
data = {k: v for k, v in data.items() if k in include}
if exclude is not None:
# Remove representations based on exclude list
data = {k: v for k, v in data.items() if k not in exclude}
return data
@property
def pdf_header(self) -> str:
"""
Read/Write property of the PDF header that is written.
This should be something like ``'%PDF-1.5'``. It is recommended to set
the lowest version that supports all features which are used within the
PDF file.
Note: `pdf_header` returns a string but accepts bytes or str for writing
"""
return self._header.decode()
@pdf_header.setter
def pdf_header(self, new_header: Union[str, bytes]) -> None:
if isinstance(new_header, str):
new_header = new_header.encode()
self._header = new_header
def _add_object(self, obj: PdfObject) -> IndirectObject:
if (
getattr(obj, "indirect_reference", None) is not None
and obj.indirect_reference.pdf == self # type: ignore
):
return obj.indirect_reference # type: ignore
# check for /Contents in Pages (/Contents in annotation are strings)
if isinstance(obj, DictionaryObject) and isinstance(
obj.get(PG.CONTENTS, None), (ArrayObject, DictionaryObject)
):
obj[NameObject(PG.CONTENTS)] = self._add_object(obj[PG.CONTENTS])
self._objects.append(obj)
obj.indirect_reference = IndirectObject(len(self._objects), 0, self)
return obj.indirect_reference
def get_object(
self,
indirect_reference: Union[int, IndirectObject],
) -> PdfObject:
if isinstance(indirect_reference, int):
return self._objects[indirect_reference - 1]
if indirect_reference.pdf != self:
raise ValueError("pdf must be self")
return self._objects[indirect_reference.idnum - 1]
def _replace_object(
self,
indirect_reference: Union[int, IndirectObject],
obj: PdfObject,
) -> PdfObject:
if isinstance(indirect_reference, IndirectObject):
if indirect_reference.pdf != self:
raise ValueError("pdf must be self")
indirect_reference = indirect_reference.idnum
gen = self._objects[indirect_reference - 1].indirect_reference.generation # type: ignore
if (
getattr(obj, "indirect_reference", None) is not None
and obj.indirect_reference.pdf != self # type: ignore
):
obj = obj.clone(self)
self._objects[indirect_reference - 1] = obj
obj.indirect_reference = IndirectObject(indirect_reference, gen, self)
return self._objects[indirect_reference - 1]
def _add_page(
self,
page: PageObject,
action: Callable[[Any, Union[PageObject, IndirectObject]], None],
excluded_keys: Iterable[str] = (),
) -> PageObject:
assert cast(str, page[PA.TYPE]) == CO.PAGE
page_org = page
excluded_keys = list(excluded_keys)
excluded_keys += [PA.PARENT, "/StructParents"]
# acrobat does not accept to have two indirect ref pointing on the same
# page; therefore in order to add easily multiple copies of the same
# page, we need to create a new dictionary for the page, however the
# objects below (including content) are not duplicated:
try: # delete an already existing page
del self._id_translated[id(page_org.indirect_reference.pdf)][ # type: ignore
page_org.indirect_reference.idnum # type: ignore
]
except Exception:
pass
page = cast("PageObject", page_org.clone(self, False, excluded_keys))
if page_org.pdf is not None:
other = page_org.pdf.pdf_header
self.pdf_header = _get_max_pdf_version_header(self.pdf_header, other)
page[NameObject(PA.PARENT)] = self._pages
pages = cast(DictionaryObject, self.get_object(self._pages))
assert page.indirect_reference is not None
action(pages[PA.KIDS], page.indirect_reference)
action(self.flattened_pages, page)
page_count = cast(int, pages[PA.COUNT])
pages[NameObject(PA.COUNT)] = NumberObject(page_count + 1)
return page
def set_need_appearances_writer(self, state: bool = True) -> None:
"""
Sets the "NeedAppearances" flag in the PDF writer.
The "NeedAppearances" flag indicates whether the appearance dictionary
for form fields should be automatically generated by the PDF viewer or
if the embedded appearance should be used.
Args:
state: The actual value of the NeedAppearances flag.
Returns:
None
"""
# See 12.7.2 and 7.7.2 for more information:
# https://opensource.adobe.com/dc-acrobat-sdk-docs/pdfstandards/PDF32000_2008.pdf
try:
# get the AcroForm tree
if CatalogDictionary.ACRO_FORM not in self._root_object:
self._root_object[
NameObject(CatalogDictionary.ACRO_FORM)
] = self._add_object(DictionaryObject())
need_appearances = NameObject(InteractiveFormDictEntries.NeedAppearances)
cast(DictionaryObject, self._root_object[CatalogDictionary.ACRO_FORM])[
need_appearances
] = BooleanObject(state)
except Exception as exc: # pragma: no cover
logger_warning(
f"set_need_appearances_writer({state}) catch : {exc}", __name__
)
def create_viewer_preferences(self) -> ViewerPreferences:
o = ViewerPreferences()
self._root_object[
NameObject(CatalogDictionary.VIEWER_PREFERENCES)
] = self._add_object(o)
return o
def add_page(
self,
page: PageObject,
excluded_keys: Iterable[str] = (),
) -> PageObject:
"""
Add a page to this PDF file.
Recommended for advanced usage including the adequate excluded_keys.
The page is usually acquired from a :class:`PdfReader<pypdf.PdfReader>`
instance.
Args:
page: The page to add to the document. Should be
an instance of :class:`PageObject<pypdf._page.PageObject>`
excluded_keys:
Returns:
The added PageObject.
"""
return self._add_page(page, list.append, excluded_keys)
def insert_page(
self,
page: PageObject,
index: int = 0,
excluded_keys: Iterable[str] = (),
) -> PageObject:
"""
Insert a page in this PDF file. The page is usually acquired from a
:class:`PdfReader<pypdf.PdfReader>` instance.
Args:
page: The page to add to the document.
index: Position at which the page will be inserted.
excluded_keys:
Returns:
The added PageObject.
"""
return self._add_page(page, lambda kids, p: kids.insert(index, p))
def _get_page_number_by_indirect(
self, indirect_reference: Union[None, int, NullObject, IndirectObject]
) -> Optional[int]:
"""
Generate _page_id2num.
Args:
indirect_reference:
Returns:
The page number or None
"""
# to provide same function as in PdfReader
if indirect_reference is None or isinstance(indirect_reference, NullObject):
return None
if isinstance(indirect_reference, int):
indirect_reference = IndirectObject(indirect_reference, 0, self)
obj = indirect_reference.get_object()
if isinstance(obj, PageObject):
return obj.page_number
return None
def add_blank_page(
self, width: Optional[float] = None, height: Optional[float] = None
) -> PageObject:
"""
Append a blank page to this PDF file and return it.
If no page size is specified, use the size of the last page.
Args:
width: The width of the new page expressed in default user
space units.
height: The height of the new page expressed in default
user space units.
Returns:
The newly appended page.
Raises:
PageSizeNotDefinedError: if width and height are not defined
and previous page does not exist.
"""
page = PageObject.create_blank_page(self, width, height)
return self.add_page(page)
def insert_blank_page(
self,
width: Optional[Union[float, decimal.Decimal]] = None,
height: Optional[Union[float, decimal.Decimal]] = None,
index: int = 0,
) -> PageObject:
"""
Insert a blank page to this PDF file and return it.
If no page size is specified, use the size of the last page.
Args:
width: The width of the new page expressed in default user
space units.
height: The height of the new page expressed in default
user space units.
index: Position to add the page.
Returns:
The newly inserted page.
Raises:
PageSizeNotDefinedError: if width and height are not defined
and previous page does not exist.
"""
if width is None or height is None and (self.get_num_pages() - 1) >= index:
oldpage = self.pages[index]
width = oldpage.mediabox.width
height = oldpage.mediabox.height
page = PageObject.create_blank_page(self, width, height)
self.insert_page(page, index)
return page
@property
def open_destination(
self,
) -> Union[None, Destination, TextStringObject, ByteStringObject]:
return super().open_destination
@open_destination.setter
def open_destination(self, dest: Union[None, str, Destination, PageObject]) -> None:
if dest is None:
try:
del self._root_object["/OpenAction"]
except KeyError:
pass
elif isinstance(dest, str):
self._root_object[NameObject("/OpenAction")] = TextStringObject(dest)
elif isinstance(dest, Destination):
self._root_object[NameObject("/OpenAction")] = dest.dest_array
elif isinstance(dest, PageObject):
self._root_object[NameObject("/OpenAction")] = Destination(
"Opening",
dest.indirect_reference
if dest.indirect_reference is not None
else NullObject(),
PAGE_FIT,
).dest_array
def add_js(self, javascript: str) -> None:
"""
Add JavaScript which will launch upon opening this PDF.
Args:
javascript: Your Javascript.
>>> output.add_js("this.print({bUI:true,bSilent:false,bShrinkToFit:true});")
# Example: This will launch the print window when the PDF is opened.
"""
# Names / JavaScript preferred to be able to add multiple scripts
if "/Names" not in self._root_object:
self._root_object[NameObject(CA.NAMES)] = DictionaryObject()
names = cast(DictionaryObject, self._root_object[CA.NAMES])
if "/JavaScript" not in names:
names[NameObject("/JavaScript")] = DictionaryObject(
{NameObject("/Names"): ArrayObject()}
)
js_list = cast(
ArrayObject, cast(DictionaryObject, names["/JavaScript"])["/Names"]
)
js = DictionaryObject()
js.update(
{
NameObject(PA.TYPE): NameObject("/Action"),
NameObject("/S"): NameObject("/JavaScript"),
NameObject("/JS"): TextStringObject(f"{javascript}"),
}
)
# We need a name for parameterized javascript in the pdf file,
# but it can be anything.
js_list.append(create_string_object(str(uuid.uuid4())))
js_list.append(self._add_object(js))
def add_attachment(self, filename: str, data: Union[str, bytes]) -> None:
"""
Embed a file inside the PDF.
Reference:
https://opensource.adobe.com/dc-acrobat-sdk-docs/pdfstandards/PDF32000_2008.pdf
Section 7.11.3
Args:
filename: The filename to display.
data: The data in the file.
"""
# We need three entries:
# * The file's data
# * The /Filespec entry
# * The file's name, which goes in the Catalog
# The entry for the file
# Sample:
# 8 0 obj
# <<
# /Length 12
# /Type /EmbeddedFile
# >>
# stream
# Hello world!
# endstream
# endobj
file_entry = DecodedStreamObject()
file_entry.set_data(b_(data))
file_entry.update({NameObject(PA.TYPE): NameObject("/EmbeddedFile")})
# The Filespec entry
# Sample:
# 7 0 obj
# <<
# /Type /Filespec
# /F (hello.txt)
# /EF << /F 8 0 R >>
# >>
# endobj
ef_entry = DictionaryObject()
ef_entry.update({NameObject("/F"): self._add_object(file_entry)})
filespec = DictionaryObject()
filespec.update(
{
NameObject(PA.TYPE): NameObject("/Filespec"),
NameObject(FileSpecificationDictionaryEntries.F): create_string_object(
filename
), # Perhaps also try TextStringObject
NameObject(FileSpecificationDictionaryEntries.EF): ef_entry,
}
)
# Then create the entry for the root, as it needs
# a reference to the Filespec
# Sample:
# 1 0 obj
# <<
# /Type /Catalog
# /Outlines 2 0 R
# /Pages 3 0 R
# /Names << /EmbeddedFiles << /Names [(hello.txt) 7 0 R] >> >>
# >>
# endobj
if CA.NAMES not in self._root_object:
self._root_object[NameObject(CA.NAMES)] = self._add_object(
DictionaryObject()
)
if "/EmbeddedFiles" not in cast(DictionaryObject, self._root_object[CA.NAMES]):
embedded_files_names_dictionary = DictionaryObject(
{NameObject(CA.NAMES): ArrayObject()}
)
cast(DictionaryObject, self._root_object[CA.NAMES])[
NameObject("/EmbeddedFiles")
] = self._add_object(embedded_files_names_dictionary)
else:
embedded_files_names_dictionary = cast(
DictionaryObject,
cast(DictionaryObject, self._root_object[CA.NAMES])["/EmbeddedFiles"],
)
cast(ArrayObject, embedded_files_names_dictionary[CA.NAMES]).extend(
[create_string_object(filename), filespec]
)
def append_pages_from_reader(
self,
reader: PdfReader,
after_page_append: Optional[Callable[[PageObject], None]] = None,
) -> None:
"""
Copy pages from reader to writer. Includes an optional callback
parameter which is invoked after pages are appended to the writer.
``append`` should be preferred.
Args:
reader: a PdfReader object from which to copy page
annotations to this writer object. The writer's annots
will then be updated.
after_page_append:
Callback function that is invoked after each page is appended to
the writer. Signature includes a reference to the appended page
(delegates to append_pages_from_reader). The single parameter of
the callback is a reference to the page just appended to the
document.
"""
# Get page count from writer and reader
reader_num_pages = len(reader.pages)
# Copy pages from reader to writer
for reader_page_number in range(reader_num_pages):
reader_page = reader.pages[reader_page_number]
writer_page = self.add_page(reader_page)
# Trigger callback, pass writer page as parameter
if callable(after_page_append):
after_page_append(writer_page)
def _update_field_annotation(
self,
field: DictionaryObject,
anno: DictionaryObject,
font_name: str = "",
font_size: float = -1,
) -> None:
# Calculate rectangle dimensions
_rct = cast(RectangleObject, anno[AA.Rect])
rct = RectangleObject((0, 0, abs(_rct[2] - _rct[0]), abs(_rct[3] - _rct[1])))
# Extract font information
da = anno.get_inherited(
AA.DA,
cast(DictionaryObject, self.root_object[CatalogDictionary.ACRO_FORM]).get(
AA.DA, None
),
)
if da is None:
da = TextStringObject("/Helv 0 Tf 0 g")
else:
da = da.get_object()
font_properties = da.replace("\n", " ").replace("\r", " ").split(" ")
font_properties = [x for x in font_properties if x != ""]
if font_name:
font_properties[font_properties.index("Tf") - 2] = font_name
else:
font_name = font_properties[font_properties.index("Tf") - 2]
font_height = (
font_size
if font_size >= 0
else float(font_properties[font_properties.index("Tf") - 1])
)
if font_height == 0:
if field.get(FA.Ff, 0) & FA.FfBits.Multiline:
font_height = DEFAULT_FONT_HEIGHT_IN_MULTILINE
else:
font_height = rct.height - 2
font_properties[font_properties.index("Tf") - 1] = str(font_height)
da = " ".join(font_properties)
y_offset = rct.height - 1 - font_height
# Retrieve font information from local DR ...
dr: Any = cast(
DictionaryObject,
cast(
DictionaryObject,
anno.get_inherited(
"/DR",
cast(
DictionaryObject, self.root_object[CatalogDictionary.ACRO_FORM]
).get("/DR", DictionaryObject()),
),
).get_object(),
)
dr = dr.get("/Font", DictionaryObject()).get_object()
# _default_fonts_space_width keys is the list of Standard fonts
if font_name not in dr and font_name not in _default_fonts_space_width:
# ...or AcroForm dictionary
dr = cast(
Dict[Any, Any],
cast(
DictionaryObject, self.root_object[CatalogDictionary.ACRO_FORM]
).get("/DR", {}),
)
dr = dr.get_object().get("/Font", DictionaryObject()).get_object()
font_res = dr.get(font_name, None)
if font_res is not None:
font_res = cast(DictionaryObject, font_res.get_object())
font_subtype, _, font_encoding, font_map = build_char_map_from_dict(
200, font_res
)
try: # get rid of width stored in -1 key
del font_map[-1]
except KeyError:
pass
font_full_rev: Dict[str, bytes]
if isinstance(font_encoding, str):
font_full_rev = {
v: k.encode(font_encoding) for k, v in font_map.items()
}
else:
font_full_rev = {v: bytes((k,)) for k, v in font_encoding.items()}
font_encoding_rev = {v: bytes((k,)) for k, v in font_encoding.items()}
for kk, v in font_map.items():
font_full_rev[v] = font_encoding_rev.get(kk, kk)
else:
logger_warning(f"Font dictionary for {font_name} not found.", __name__)
font_full_rev = {}
# Retrieve field text and selected values
field_flags = field.get(FA.Ff, 0)
if field.get(FA.FT, "/Tx") == "/Ch" and field_flags & FA.FfBits.Combo == 0:
txt = "\n".join(anno.get_inherited(FA.Opt, []))
sel = field.get("/V", [])
if not isinstance(sel, list):
sel = [sel]
else: # /Tx
txt = field.get("/V", "")
sel = []
# Escape parentheses (pdf 1.7 reference, table 3.2 Literal Strings)
txt = txt.replace("\\", "\\\\").replace("(", r"\(").replace(")", r"\)")
# Generate appearance stream
ap_stream = f"q\n/Tx BMC \nq\n1 1 {rct.width - 1} {rct.height - 1} re\nW\nBT\n{da}\n".encode()
for line_number, line in enumerate(txt.replace("\n", "\r").split("\r")):
if line in sel:
# may be improved but cannot find how to get fill working => replaced with lined box
ap_stream += (
f"1 {y_offset - (line_number * font_height * 1.4) - 1} {rct.width - 2} {font_height + 2} re\n"
f"0.5 0.5 0.5 rg s\n{da}\n"
).encode()
if line_number == 0:
ap_stream += f"2 {y_offset} Td\n".encode()
else:
# Td is a relative translation
ap_stream += f"0 {- font_height * 1.4} Td\n".encode()
enc_line: List[bytes] = [
font_full_rev.get(c, c.encode("utf-16-be")) for c in line
]
if any(len(c) >= 2 for c in enc_line):
ap_stream += b"<" + (b"".join(enc_line)).hex().encode() + b"> Tj\n"
else:
ap_stream += b"(" + b"".join(enc_line) + b") Tj\n"
ap_stream += b"ET\nQ\nEMC\nQ\n"
# Create appearance dictionary
dct = DecodedStreamObject.initialize_from_dictionary(
{
NameObject("/Type"): NameObject("/XObject"),
NameObject("/Subtype"): NameObject("/Form"),
NameObject("/BBox"): rct,
"__streamdata__": ByteStringObject(ap_stream),
"/Length": 0,
}
)
if AA.AP in anno:
for k, v in cast(DictionaryObject, anno[AA.AP]).get("/N", {}).items():
if k not in {"/BBox", "/Length", "/Subtype", "/Type", "/Filter"}:
dct[k] = v
# Update Resources with font information if necessary
if font_res is not None:
dct[NameObject("/Resources")] = DictionaryObject(
{
NameObject("/Font"): DictionaryObject(
{
NameObject(font_name): getattr(
font_res, "indirect_reference", font_res
)
}
)
}
)
if AA.AP not in anno:
anno[NameObject(AA.AP)] = DictionaryObject(
{NameObject("/N"): self._add_object(dct)}
)
elif "/N" not in cast(DictionaryObject, anno[AA.AP]):
cast(DictionaryObject, anno[NameObject(AA.AP)])[
NameObject("/N")
] = self._add_object(dct)
else: # [/AP][/N] exists
n = anno[AA.AP]["/N"].indirect_reference.idnum # type: ignore
self._objects[n - 1] = dct
dct.indirect_reference = IndirectObject(n, 0, self)
FFBITS_NUL = FA.FfBits(0)
def update_page_form_field_values(
self,
page: Union[PageObject, List[PageObject], None],
fields: Dict[str, Any],
flags: FA.FfBits = FFBITS_NUL,
auto_regenerate: Optional[bool] = True,
) -> None:
"""
Update the form field values for a given page from a fields dictionary.
Copy field texts and values from fields to page.
If the field links to a parent object, add the information to the parent.
Args:
page: `PageObject` - references **PDF writer's page** where the
annotations and field data will be updated.
`List[Pageobject]` - provides list of pages to be processed.
`None` - all pages.
fields: a Python dictionary of:
* field names (/T) as keys and text values (/V) as value
* field names (/T) as keys and list of text values (/V) for multiple choice list
* field names (/T) as keys and tuple of:
* text values (/V)
* font id (e.g. /F1, the font id must exist)
* font size (0 for autosize)
flags: A set of flags from :class:`~pypdf.constants.FieldDictionaryAttributes.FfBits`.
auto_regenerate: Set/unset the need_appearances flag;
the flag is unchanged if auto_regenerate is None.
"""
if CatalogDictionary.ACRO_FORM not in self._root_object:
raise PyPdfError("No /AcroForm dictionary in PdfWriter Object")
af = cast(DictionaryObject, self._root_object[CatalogDictionary.ACRO_FORM])
if InteractiveFormDictEntries.Fields not in af:
raise PyPdfError("No /Fields dictionary in Pdf in PdfWriter Object")
if isinstance(auto_regenerate, bool):
self.set_need_appearances_writer(auto_regenerate)
# Iterate through pages, update field values
if page is None:
page = list(self.pages)
if isinstance(page, list):
for p in page:
if PG.ANNOTS in p: # just to prevent warnings
self.update_page_form_field_values(p, fields, flags, None)
return None
if PG.ANNOTS not in page:
logger_warning("No fields to update on this page", __name__)
return
for writer_annot in page[PG.ANNOTS]: # type: ignore
writer_annot = cast(DictionaryObject, writer_annot.get_object())
if writer_annot.get("/Subtype", "") != "/Widget":
continue
if "/FT" in writer_annot and "/T" in writer_annot:
writer_parent_annot = writer_annot
else:
writer_parent_annot = writer_annot.get(
PG.PARENT, DictionaryObject()
).get_object()
for field, value in fields.items():
if not (
self._get_qualified_field_name(writer_parent_annot) == field
or writer_parent_annot.get("/T", None) == field
):
continue
if (
writer_parent_annot.get("/FT", None) == "/Ch"
and "/I" in writer_parent_annot
):
del writer_parent_annot["/I"]
if flags:
writer_annot[NameObject(FA.Ff)] = NumberObject(flags)
if isinstance(value, list):
lst = ArrayObject(TextStringObject(v) for v in value)
writer_parent_annot[NameObject(FA.V)] = lst
elif isinstance(value, tuple):
writer_annot[NameObject(FA.V)] = TextStringObject(
value[0],
)
else:
writer_parent_annot[NameObject(FA.V)] = TextStringObject(value)
if writer_parent_annot.get(FA.FT) in ("/Btn"):
# case of Checkbox button (no /FT found in Radio widgets
v = NameObject(value)
if v not in writer_annot[NameObject(AA.AP)][NameObject("/N")]:
v = NameObject("/Off")
# other cases will be updated through the for loop
writer_annot[NameObject(AA.AS)] = v
elif (
writer_parent_annot.get(FA.FT) == "/Tx"
or writer_parent_annot.get(FA.FT) == "/Ch"
):
# textbox
if isinstance(value, tuple):
self._update_field_annotation(
writer_parent_annot, writer_annot, value[1], value[2]
)
else:
self._update_field_annotation(writer_parent_annot, writer_annot)
elif (
writer_annot.get(FA.FT) == "/Sig"
): # deprecated # not implemented yet
# signature
logger_warning("Signature forms not implemented yet", __name__)
def reattach_fields(
self, page: Optional[PageObject] = None
) -> List[DictionaryObject]:
"""
Parse annotations within the page looking for orphan fields and
reattach then into the Fields Structure.
Args:
page: page to analyze.
If none is provided, all pages will be analyzed.
Returns:
list of reattached fields.
"""
lst = []
if page is None:
for p in self.pages:
lst += self.reattach_fields(p)
return lst
try:
af = cast(DictionaryObject, self._root_object[CatalogDictionary.ACRO_FORM])
except KeyError:
af = DictionaryObject()
self._root_object[NameObject(CatalogDictionary.ACRO_FORM)] = af
try:
fields = cast(ArrayObject, af[InteractiveFormDictEntries.Fields])
except KeyError:
fields = ArrayObject()
af[NameObject(InteractiveFormDictEntries.Fields)] = fields
if "/Annots" not in page:
return lst
annots = cast(ArrayObject, page["/Annots"])
for idx in range(len(annots)):
ano = annots[idx]
indirect = isinstance(ano, IndirectObject)
ano = cast(DictionaryObject, ano.get_object())
if ano.get("/Subtype", "") == "/Widget" and "/FT" in ano:
if (
"indirect_reference" in ano.__dict__
and ano.indirect_reference in fields
):
continue
if not indirect:
annots[idx] = self._add_object(ano)
fields.append(ano.indirect_reference)
lst.append(ano)
return lst
def clone_reader_document_root(self, reader: PdfReader) -> None:
"""
Copy the reader document root to the writer and all sub-elements,
including pages, threads, outlines,... For partial insertion, ``append``
should be considered.
Args:
reader: PdfReader from which the document root should be copied.
"""
self._objects.clear()
self._root_object = reader.root_object.clone(self)
self._root = self._root_object.indirect_reference # type: ignore[assignment]
self._pages = self._root_object.raw_get("/Pages")
self._flatten()
assert self.flattened_pages is not None
for p in self.flattened_pages:
p[NameObject("/Parent")] = self._pages
self._objects[cast(IndirectObject, p.indirect_reference).idnum - 1] = p
cast(DictionaryObject, self._pages.get_object())[
NameObject("/Kids")
] = ArrayObject([p.indirect_reference for p in self.flattened_pages])
def clone_document_from_reader(
self,
reader: PdfReader,
after_page_append: Optional[Callable[[PageObject], None]] = None,
) -> None:
"""
Create a copy (clone) of a document from a PDF file reader cloning
section '/Root' and '/Info' and '/ID' of the pdf.
Args:
reader: PDF file reader instance from which the clone
should be created.
after_page_append:
Callback function that is invoked after each page is appended to
the writer. Signature includes a reference to the appended page
(delegates to append_pages_from_reader). The single parameter of
the callback is a reference to the page just appended to the
document.
"""
self.clone_reader_document_root(reader)
self._info_obj = self._add_object(DictionaryObject())
if TK.INFO in reader.trailer:
self._info = reader._info # actually copy fields
try:
self._ID = cast(ArrayObject, reader._ID).clone(self)
except AttributeError:
pass
if callable(after_page_append):
for page in cast(
ArrayObject, cast(DictionaryObject, self._pages.get_object())["/Kids"]
):
after_page_append(page.get_object())
def _compute_document_identifier(self) -> ByteStringObject:
stream = BytesIO()
self._write_pdf_structure(stream)
stream.seek(0)
return ByteStringObject(_rolling_checksum(stream).encode("utf8"))
def generate_file_identifiers(self) -> None:
"""
Generate an identifier for the PDF that will be written.
The only point of this is ensuring uniqueness. Reproducibility is not
required.
When a file is first written, both identifiers shall be set to the same value.
If both identifiers match when a file reference is resolved, it is very
likely that the correct and unchanged file has been found. If only the first
identifier matches, a different version of the correct file has been found.
see 14.4 "File Identifiers".
"""
if self._ID:
id1 = self._ID[0]
id2 = self._compute_document_identifier()
else:
id1 = self._compute_document_identifier()
id2 = id1
self._ID = ArrayObject((id1, id2))
def encrypt(
self,
user_password: str,
owner_password: Optional[str] = None,
use_128bit: bool = True,
permissions_flag: UserAccessPermissions = ALL_DOCUMENT_PERMISSIONS,
*,
algorithm: Optional[str] = None,
) -> None:
"""
Encrypt this PDF file with the PDF Standard encryption handler.
Args:
user_password: The password which allows for opening
and reading the PDF file with the restrictions provided.
owner_password: The password which allows for
opening the PDF files without any restrictions. By default,
the owner password is the same as the user password.
use_128bit: flag as to whether to use 128bit
encryption. When false, 40bit encryption will be used.
By default, this flag is on.
permissions_flag: permissions as described in
Table 3.20 of the PDF 1.7 specification. A bit value of 1 means
the permission is granted.
Hence an integer value of -1 will set all flags.
Bit position 3 is for printing, 4 is for modifying content,
5 and 6 control annotations, 9 for form fields,
10 for extraction of text and graphics.
algorithm: encrypt algorithm. Values may be one of "RC4-40", "RC4-128",
"AES-128", "AES-256-R5", "AES-256". If it is valid,
`use_128bit` will be ignored.
"""
if owner_password is None:
owner_password = user_password
if algorithm is not None:
try:
alg = getattr(EncryptAlgorithm, algorithm.replace("-", "_"))
except AttributeError:
raise ValueError(f"algorithm '{algorithm}' NOT supported")
else:
alg = EncryptAlgorithm.RC4_128
if not use_128bit:
alg = EncryptAlgorithm.RC4_40
self.generate_file_identifiers()
assert self._ID
self._encryption = Encryption.make(alg, permissions_flag, self._ID[0])
# in case call `encrypt` again
entry = self._encryption.write_entry(user_password, owner_password)
if self._encrypt_entry:
# replace old encrypt_entry
assert self._encrypt_entry.indirect_reference is not None
entry.indirect_reference = self._encrypt_entry.indirect_reference
self._objects[entry.indirect_reference.idnum - 1] = entry
else:
self._add_object(entry)
self._encrypt_entry = entry
def write_stream(self, stream: StreamType) -> None:
if hasattr(stream, "mode") and "b" not in stream.mode:
logger_warning(
f"File <{stream.name}> to write to is not in binary mode. "
"It may not be written to correctly.",
__name__,
)
if not self._root:
self._root = self._add_object(self._root_object)
self._sweep_indirect_references(self._root)
object_positions = self._write_pdf_structure(stream)
xref_location = self._write_xref_table(stream, object_positions)
self._write_trailer(stream, xref_location)
def write(self, stream: Union[Path, StrByteType]) -> Tuple[bool, IO[Any]]:
"""
Write the collection of pages added to this object out as a PDF file.
Args:
stream: An object to write the file to. The object can support
the write method and the tell method, similar to a file object, or
be a file path, just like the fileobj, just named it stream to keep
existing workflow.
Returns:
A tuple (bool, IO).
"""
my_file = False
if stream == "":
raise ValueError(f"Output(stream={stream}) is empty.")
if isinstance(stream, (str, Path)):
stream = FileIO(stream, "wb")
self.with_as_usage = True #
my_file = True
self.write_stream(stream)
if self.with_as_usage:
stream.close()
return my_file, stream
def _write_pdf_structure(self, stream: StreamType) -> List[int]:
object_positions = []
stream.write(self.pdf_header.encode() + b"\n")
stream.write(b"%\xE2\xE3\xCF\xD3\n")
for i, obj in enumerate(self._objects):
if obj is not None:
idnum = i + 1
object_positions.append(stream.tell())
stream.write(f"{idnum} 0 obj\n".encode())
if self._encryption and obj != self._encrypt_entry:
obj = self._encryption.encrypt_object(obj, idnum, 0)
obj.write_to_stream(stream)
stream.write(b"\nendobj\n")
return object_positions
def _write_xref_table(self, stream: StreamType, object_positions: List[int]) -> int:
xref_location = stream.tell()
stream.write(b"xref\n")
stream.write(f"0 {len(self._objects) + 1}\n".encode())
stream.write(f"{0:0>10} {65535:0>5} f \n".encode())
for offset in object_positions:
stream.write(f"{offset:0>10} {0:0>5} n \n".encode())
return xref_location
def _write_trailer(self, stream: StreamType, xref_location: int) -> None:
"""
Write the PDF trailer to the stream.
To quote the PDF specification:
[The] trailer [gives] the location of the cross-reference table and
of certain special objects within the body of the file.
"""
stream.write(b"trailer\n")
trailer = DictionaryObject()
trailer.update(
{
NameObject(TK.SIZE): NumberObject(len(self._objects) + 1),
NameObject(TK.ROOT): self._root,
NameObject(TK.INFO): self._info_obj,
}
)
if self._ID:
trailer[NameObject(TK.ID)] = self._ID
if self._encrypt_entry:
trailer[NameObject(TK.ENCRYPT)] = self._encrypt_entry.indirect_reference
trailer.write_to_stream(stream)
stream.write(f"\nstartxref\n{xref_location}\n%%EOF\n".encode()) # eof
def add_metadata(self, infos: Dict[str, Any]) -> None:
"""
Add custom metadata to the output.
Args:
infos: a Python dictionary where each key is a field
and each value is your new metadata.
"""
args = {}
if isinstance(infos, PdfObject):
infos = cast(DictionaryObject, infos.get_object())
for key, value in list(infos.items()):
if isinstance(value, PdfObject):
value = value.get_object()
args[NameObject(key)] = create_string_object(str(value))
assert isinstance(self._info, DictionaryObject)
self._info.update(args)
def _sweep_indirect_references(
self,
root: Union[
ArrayObject,
BooleanObject,
DictionaryObject,
FloatObject,
IndirectObject,
NameObject,
PdfObject,
NumberObject,
TextStringObject,
NullObject,
],
) -> None:
"""
Resolving any circular references to Page objects.
Circular references to Page objects can arise when objects such as
annotations refer to their associated page. If these references are not
properly handled, the PDF file will contain multiple copies of the same
Page object. To address this problem, Page objects store their original
object reference number. This method adds the reference number of any
circularly referenced Page objects to an external reference map. This
ensures that self-referencing trees reference the correct new object
location, rather than copying in a new copy of the Page object.
Args:
root: The root of the PDF object tree to sweep.
"""
stack: Deque[
Tuple[
Any,
Optional[Any],
Any,
List[PdfObject],
]
] = collections.deque()
discovered = []
parent = None
grant_parents: List[PdfObject] = []
key_or_id = None
# Start from root
stack.append((root, parent, key_or_id, grant_parents))
while len(stack):
data, parent, key_or_id, grant_parents = stack.pop()
# Build stack for a processing depth-first
if isinstance(data, (ArrayObject, DictionaryObject)):
for key, value in data.items():
stack.append(
(
value,
data,
key,
grant_parents + [parent] if parent is not None else [],
)
)
elif isinstance(data, IndirectObject) and data.pdf != self:
data = self._resolve_indirect_object(data)
if str(data) not in discovered:
discovered.append(str(data))
stack.append((data.get_object(), None, None, []))
# Check if data has a parent and if it is a dict or
# an array update the value
if isinstance(parent, (DictionaryObject, ArrayObject)):
if isinstance(data, StreamObject):
# a dictionary value is a stream; streams must be indirect
# objects, so we need to change this value.
data = self._resolve_indirect_object(self._add_object(data))
update_hashes = []
# Data changed and thus the hash value changed
if parent[key_or_id] != data:
update_hashes = [parent.hash_value()] + [
grant_parent.hash_value() for grant_parent in grant_parents
]
parent[key_or_id] = data
# Update old hash value to new hash value
for old_hash in update_hashes:
indirect_reference = self._idnum_hash.pop(old_hash, None)
if indirect_reference is not None:
indirect_reference_obj = indirect_reference.get_object()
if indirect_reference_obj is not None:
self._idnum_hash[
indirect_reference_obj.hash_value()
] = indirect_reference
def _resolve_indirect_object(self, data: IndirectObject) -> IndirectObject:
"""
Resolves an indirect object to an indirect object in this PDF file.
If the input indirect object already belongs to this PDF file, it is
returned directly. Otherwise, the object is retrieved from the input
object's PDF file using the object's ID number and generation number. If
the object cannot be found, a warning is logged and a `NullObject` is
returned.
If the object is not already in this PDF file, it is added to the file's
list of objects and assigned a new ID number and generation number of 0.
The hash value of the object is then added to the `_idnum_hash`
dictionary, with the corresponding `IndirectObject` reference as the
value.
Args:
data: The `IndirectObject` to resolve.
Returns:
The resolved `IndirectObject` in this PDF file.
Raises:
ValueError: If the input stream is closed.
"""
if hasattr(data.pdf, "stream") and data.pdf.stream.closed:
raise ValueError(f"I/O operation on closed file: {data.pdf.stream.name}")
if data.pdf == self:
return data
# Get real object indirect object
real_obj = data.pdf.get_object(data)
if real_obj is None:
logger_warning(
f"Unable to resolve [{data.__class__.__name__}: {data}], "
"returning NullObject instead",
__name__,
)
real_obj = NullObject()
hash_value = real_obj.hash_value()
# Check if object is handled
if hash_value in self._idnum_hash:
return self._idnum_hash[hash_value]
if data.pdf == self:
self._idnum_hash[hash_value] = IndirectObject(data.idnum, 0, self)
# This is new object in this pdf
else:
self._idnum_hash[hash_value] = self._add_object(real_obj)
return self._idnum_hash[hash_value]
def get_reference(self, obj: PdfObject) -> IndirectObject:
idnum = self._objects.index(obj) + 1
ref = IndirectObject(idnum, 0, self)
assert ref.get_object() == obj
return ref
def get_outline_root(self) -> TreeObject:
if CO.OUTLINES in self._root_object:
# Table 3.25 Entries in the catalog dictionary
outline = cast(TreeObject, self._root_object[CO.OUTLINES])
if not isinstance(outline, TreeObject):
t = TreeObject(outline)
self._replace_object(outline.indirect_reference.idnum, t)
outline = t
idnum = self._objects.index(outline) + 1
outline_ref = IndirectObject(idnum, 0, self)
assert outline_ref.get_object() == outline
else:
outline = TreeObject()
outline.update({})
outline_ref = self._add_object(outline)
self._root_object[NameObject(CO.OUTLINES)] = outline_ref
return outline
def get_threads_root(self) -> ArrayObject:
"""
The list of threads.
See §12.4.3 of the PDF 1.7 or PDF 2.0 specification.
Returns:
An array (possibly empty) of Dictionaries with ``/F`` and
``/I`` properties.
"""
if CO.THREADS in self._root_object:
# Table 3.25 Entries in the catalog dictionary
threads = cast(ArrayObject, self._root_object[CO.THREADS])
else:
threads = ArrayObject()
self._root_object[NameObject(CO.THREADS)] = threads
return threads
@property
def threads(self) -> ArrayObject:
"""
Read-only property for the list of threads.
See §8.3.2 from PDF 1.7 spec.
Each element is a dictionaries with ``/F`` and ``/I`` keys.
"""
return self.get_threads_root()
def add_outline_item_destination(
self,
page_destination: Union[IndirectObject, PageObject, TreeObject],
parent: Union[None, TreeObject, IndirectObject] = None,
before: Union[None, TreeObject, IndirectObject] = None,
is_open: bool = True,
) -> IndirectObject:
page_destination = cast(PageObject, page_destination.get_object())
if isinstance(page_destination, PageObject):
return self.add_outline_item_destination(
Destination(
f"page #{page_destination.page_number}",
cast(IndirectObject, page_destination.indirect_reference),
Fit.fit(),
)
)
if parent is None:
parent = self.get_outline_root()
page_destination[NameObject("/%is_open%")] = BooleanObject(is_open)
parent = cast(TreeObject, parent.get_object())
page_destination_ref = self._add_object(page_destination)
if before is not None:
before = before.indirect_reference
parent.insert_child(
page_destination_ref,
before,
self,
page_destination.inc_parent_counter_outline
if is_open
else (lambda x, y: 0),
)
if "/Count" not in page_destination:
page_destination[NameObject("/Count")] = NumberObject(0)
return page_destination_ref
def add_outline_item_dict(
self,
outline_item: OutlineItemType,
parent: Union[None, TreeObject, IndirectObject] = None,
before: Union[None, TreeObject, IndirectObject] = None,
is_open: bool = True,
) -> IndirectObject:
outline_item_object = TreeObject()
outline_item_object.update(outline_item)
if "/A" in outline_item:
action = DictionaryObject()
a_dict = cast(DictionaryObject, outline_item["/A"])
for k, v in list(a_dict.items()):
action[NameObject(str(k))] = v
action_ref = self._add_object(action)
outline_item_object[NameObject("/A")] = action_ref
return self.add_outline_item_destination(
outline_item_object, parent, before, is_open
)
def add_outline_item(
self,
title: str,
page_number: Union[None, PageObject, IndirectObject, int],
parent: Union[None, TreeObject, IndirectObject] = None,
before: Union[None, TreeObject, IndirectObject] = None,
color: Optional[Union[Tuple[float, float, float], str]] = None,
bold: bool = False,
italic: bool = False,
fit: Fit = PAGE_FIT,
is_open: bool = True,
) -> IndirectObject:
"""
Add an outline item (commonly referred to as a "Bookmark") to the PDF file.
Args:
title: Title to use for this outline item.
page_number: Page number this outline item will point to.
parent: A reference to a parent outline item to create nested
outline items.
before:
color: Color of the outline item's font as a red, green, blue tuple
from 0.0 to 1.0 or as a Hex String (#RRGGBB)
bold: Outline item font is bold
italic: Outline item font is italic
fit: The fit of the destination page.
Returns:
The added outline item as an indirect object.
"""
page_ref: Union[None, NullObject, IndirectObject, NumberObject]
if isinstance(italic, Fit): # it means that we are on the old params
if fit is not None and page_number is None:
page_number = fit # type: ignore
return self.add_outline_item(
title, page_number, parent, None, before, color, bold, italic, is_open=is_open # type: ignore
)
if page_number is None:
action_ref = None
else:
if isinstance(page_number, IndirectObject):
page_ref = page_number
elif isinstance(page_number, PageObject):
page_ref = page_number.indirect_reference
elif isinstance(page_number, int):
try:
page_ref = self.pages[page_number].indirect_reference
except IndexError:
page_ref = NumberObject(page_number)
if page_ref is None:
logger_warning(
f"can not find reference of page {page_number}",
__name__,
)
page_ref = NullObject()
dest = Destination(
NameObject("/" + title + " outline item"),
page_ref,
fit,
)
action_ref = self._add_object(
DictionaryObject(
{
NameObject(GoToActionArguments.D): dest.dest_array,
NameObject(GoToActionArguments.S): NameObject("/GoTo"),
}
)
)
outline_item = self._add_object(
_create_outline_item(action_ref, title, color, italic, bold)
)
if parent is None:
parent = self.get_outline_root()
return self.add_outline_item_destination(outline_item, parent, before, is_open)
def add_outline(self) -> None:
raise NotImplementedError(
"This method is not yet implemented. Use :meth:`add_outline_item` instead."
)
def add_named_destination_array(
self, title: TextStringObject, destination: Union[IndirectObject, ArrayObject]
) -> None:
named_dest = self.get_named_dest_root()
i = 0
while i < len(named_dest):
if title < named_dest[i]:
named_dest.insert(i, destination)
named_dest.insert(i, TextStringObject(title))
return
else:
i += 2
named_dest.extend([TextStringObject(title), destination])
return
def add_named_destination_object(
self,
page_destination: PdfObject,
) -> IndirectObject:
page_destination_ref = self._add_object(page_destination.dest_array) # type: ignore
self.add_named_destination_array(
cast("TextStringObject", page_destination["/Title"]), page_destination_ref # type: ignore
)
return page_destination_ref
def add_named_destination(
self,
title: str,
page_number: int,
) -> IndirectObject:
page_ref = self.get_object(self._pages)[PA.KIDS][page_number] # type: ignore
dest = DictionaryObject()
dest.update(
{
NameObject(GoToActionArguments.D): ArrayObject(
[page_ref, NameObject(TypFitArguments.FIT_H), NumberObject(826)]
),
NameObject(GoToActionArguments.S): NameObject("/GoTo"),
}
)
dest_ref = self._add_object(dest)
if not isinstance(title, TextStringObject):
title = TextStringObject(str(title))
self.add_named_destination_array(title, dest_ref)
return dest_ref
def remove_links(self) -> None:
"""Remove links and annotations from this output."""
for page in self.pages:
self.remove_objects_from_page(page, ObjectDeletionFlag.ALL_ANNOTATIONS)
def remove_annotations(
self, subtypes: Optional[Union[AnnotationSubtype, Iterable[AnnotationSubtype]]]
) -> None:
"""
Remove annotations by annotation subtype.
Args:
subtypes: subtype or list of subtypes to be removed.
Examples are: "/Link", "/FileAttachment", "/Sound",
"/Movie", "/Screen", ...
If you want to remove all annotations, use subtypes=None.
"""
for page in self.pages:
self._remove_annots_from_page(page, subtypes)
def _remove_annots_from_page(
self,
page: Union[IndirectObject, PageObject, DictionaryObject],
subtypes: Optional[Iterable[str]],
) -> None:
page = cast(DictionaryObject, page.get_object())
if PG.ANNOTS in page:
i = 0
while i < len(cast(ArrayObject, page[PG.ANNOTS])):
an = cast(ArrayObject, page[PG.ANNOTS])[i]
obj = cast(DictionaryObject, an.get_object())
if subtypes is None or cast(str, obj["/Subtype"]) in subtypes:
if isinstance(an, IndirectObject):
self._objects[an.idnum - 1] = NullObject() # to reduce PDF size
del page[PG.ANNOTS][i] # type:ignore
else:
i += 1
def remove_objects_from_page(
self,
page: Union[PageObject, DictionaryObject],
to_delete: Union[ObjectDeletionFlag, Iterable[ObjectDeletionFlag]],
) -> None:
"""
Remove objects specified by ``to_delete`` from the given page.
Args:
page: Page object to clean up.
to_delete: Objects to be deleted; can be a ``ObjectDeletionFlag``
or a list of ObjectDeletionFlag
"""
if isinstance(to_delete, (list, tuple)):
for to_d in to_delete:
self.remove_objects_from_page(page, to_d)
return
assert isinstance(to_delete, ObjectDeletionFlag)
if to_delete & ObjectDeletionFlag.LINKS:
return self._remove_annots_from_page(page, ("/Link",))
if to_delete & ObjectDeletionFlag.ATTACHMENTS:
return self._remove_annots_from_page(
page, ("/FileAttachment", "/Sound", "/Movie", "/Screen")
)
if to_delete & ObjectDeletionFlag.OBJECTS_3D:
return self._remove_annots_from_page(page, ("/3D",))
if to_delete & ObjectDeletionFlag.ALL_ANNOTATIONS:
return self._remove_annots_from_page(page, None)
jump_operators = []
if to_delete & ObjectDeletionFlag.DRAWING_IMAGES:
jump_operators = (
[b"w", b"J", b"j", b"M", b"d", b"i"]
+ [b"W", b"W*"]
+ [b"b", b"b*", b"B", b"B*", b"S", b"s", b"f", b"f*", b"F", b"n"]
+ [b"m", b"l", b"c", b"v", b"y", b"h", b"re"]
+ [b"sh"]
)
if to_delete & ObjectDeletionFlag.TEXT:
jump_operators = [b"Tj", b"TJ", b"'", b'"']
def clean(content: ContentStream, images: List[str], forms: List[str]) -> None:
nonlocal jump_operators, to_delete
i = 0
while i < len(content.operations):
operands, operator = content.operations[i]
if (
(
operator == b"INLINE IMAGE"
and (to_delete & ObjectDeletionFlag.INLINE_IMAGES)
)
or (operator in jump_operators)
or (
operator == b"Do"
and (to_delete & ObjectDeletionFlag.XOBJECT_IMAGES)
and (operands[0] in images)
)
):
del content.operations[i]
else:
i += 1
content.get_data() # this ensures ._data is rebuilt from the .operations
def clean_forms(
elt: DictionaryObject, stack: List[DictionaryObject]
) -> Tuple[List[str], List[str]]:
nonlocal to_delete
# elt in recursive call is a new ContentStream object, so we have to check the indirect_reference
if (elt in stack) or (
hasattr(elt, "indirect_reference")
and any(
elt.indirect_reference == getattr(x, "indirect_reference", -1)
for x in stack
)
):
# to prevent infinite looping
return [], [] # pragma: no cover
try:
d = cast(
Dict[Any, Any],
cast(DictionaryObject, elt["/Resources"])["/XObject"],
)
except KeyError:
d = {}
images = []
forms = []
for k, v in d.items():
o = v.get_object()
try:
content: Any = None
if (
to_delete & ObjectDeletionFlag.XOBJECT_IMAGES
and o["/Subtype"] == "/Image"
):
content = NullObject() # to delete the image keeping the entry
images.append(k)
if o["/Subtype"] == "/Form":
forms.append(k)
if isinstance(o, ContentStream):
content = o
else:
content = ContentStream(o, self)
content.update(
{
k1: v1
for k1, v1 in o.items()
if k1 not in ["/Length", "/Filter", "/DecodeParms"]
}
)
try:
content.indirect_reference = o.indirect_reference
except AttributeError: # pragma: no cover
pass
stack.append(elt)
clean_forms(content, stack) # clean subforms
if content is not None:
if isinstance(v, IndirectObject):
self._objects[v.idnum - 1] = content
else:
# should only occur with pdf not respecting pdf spec
# where streams must be indirected.
d[k] = self._add_object(content) # pragma: no cover
except (TypeError, KeyError):
pass
for im in images:
del d[im] # for clean-up
if isinstance(elt, StreamObject): # for /Form
if not isinstance(elt, ContentStream): # pragma: no cover
e = ContentStream(elt, self)
e.update(elt.items())
elt = e
clean(elt, images, forms) # clean the content
return images, forms
if not isinstance(page, PageObject):
page = PageObject(self, page.indirect_reference) # pragma: no cover
if "/Contents" in page:
content = cast(ContentStream, page.get_contents())
images, forms = clean_forms(page, [])
clean(content, images, forms)
page.replace_contents(content)
def remove_images(
self,
to_delete: ImageType = ImageType.ALL,
) -> None:
"""
Remove images from this output.
Args:
to_delete : The type of images to be deleted
(default = all images types)
"""
if isinstance(to_delete, bool):
to_delete = ImageType.ALL
i = (
(
ObjectDeletionFlag.XOBJECT_IMAGES
if to_delete & ImageType.XOBJECT_IMAGES
else ObjectDeletionFlag.NONE
)
| (
ObjectDeletionFlag.INLINE_IMAGES
if to_delete & ImageType.INLINE_IMAGES
else ObjectDeletionFlag.NONE
)
| (
ObjectDeletionFlag.DRAWING_IMAGES
if to_delete & ImageType.DRAWING_IMAGES
else ObjectDeletionFlag.NONE
)
)
for page in self.pages:
self.remove_objects_from_page(page, i)
def remove_text(self) -> None:
"""Remove text from this output."""
for page in self.pages:
self.remove_objects_from_page(page, ObjectDeletionFlag.TEXT)
def add_uri(
self,
page_number: int,
uri: str,
rect: RectangleObject,
border: Optional[ArrayObject] = None,
) -> None:
"""
Add an URI from a rectangular area to the specified page.
Args:
page_number: index of the page on which to place the URI action.
uri: URI of resource to link to.
rect: :class:`RectangleObject<pypdf.generic.RectangleObject>` or
array of four integers specifying the clickable rectangular area
``[xLL, yLL, xUR, yUR]``, or string in the form
``"[ xLL yLL xUR yUR ]"``.
border: if provided, an array describing border-drawing
properties. See the PDF spec for details. No border will be
drawn if this argument is omitted.
"""
page_link = self.get_object(self._pages)[PA.KIDS][page_number] # type: ignore
page_ref = cast(Dict[str, Any], self.get_object(page_link))
border_arr: BorderArrayType
if border is not None:
border_arr = [NumberObject(n) for n in border[:3]]
if len(border) == 4:
dash_pattern = ArrayObject([NumberObject(n) for n in border[3]])
border_arr.append(dash_pattern)
else:
border_arr = [NumberObject(2), NumberObject(2), NumberObject(2)]
if isinstance(rect, str):
rect = NumberObject(rect)
elif isinstance(rect, RectangleObject):
pass
else:
rect = RectangleObject(rect)
lnk2 = DictionaryObject()
lnk2.update(
{
NameObject("/S"): NameObject("/URI"),
NameObject("/URI"): TextStringObject(uri),
}
)
lnk = DictionaryObject()
lnk.update(
{
NameObject(AA.Type): NameObject("/Annot"),
NameObject(AA.Subtype): NameObject("/Link"),
NameObject(AA.P): page_link,
NameObject(AA.Rect): rect,
NameObject("/H"): NameObject("/I"),
NameObject(AA.Border): ArrayObject(border_arr),
NameObject("/A"): lnk2,
}
)
lnk_ref = self._add_object(lnk)
if PG.ANNOTS in page_ref:
page_ref[PG.ANNOTS].append(lnk_ref)
else:
page_ref[NameObject(PG.ANNOTS)] = ArrayObject([lnk_ref])
_valid_layouts = (
"/NoLayout",
"/SinglePage",
"/OneColumn",
"/TwoColumnLeft",
"/TwoColumnRight",
"/TwoPageLeft",
"/TwoPageRight",
)
def _get_page_layout(self) -> Optional[LayoutType]:
try:
return cast(LayoutType, self._root_object["/PageLayout"])
except KeyError:
return None
def _set_page_layout(self, layout: Union[NameObject, LayoutType]) -> None:
"""
Set the page layout.
Args:
layout: The page layout to be used.
.. list-table:: Valid ``layout`` arguments
:widths: 50 200
* - /NoLayout
- Layout explicitly not specified
* - /SinglePage
- Show one page at a time
* - /OneColumn
- Show one column at a time
* - /TwoColumnLeft
- Show pages in two columns, odd-numbered pages on the left
* - /TwoColumnRight
- Show pages in two columns, odd-numbered pages on the right
* - /TwoPageLeft
- Show two pages at a time, odd-numbered pages on the left
* - /TwoPageRight
- Show two pages at a time, odd-numbered pages on the right
"""
if not isinstance(layout, NameObject):
if layout not in self._valid_layouts:
logger_warning(
f"Layout should be one of: {'', ''.join(self._valid_layouts)}",
__name__,
)
layout = NameObject(layout)
self._root_object.update({NameObject("/PageLayout"): layout})
def set_page_layout(self, layout: LayoutType) -> None:
"""
Set the page layout.
Args:
layout: The page layout to be used
.. list-table:: Valid ``layout`` arguments
:widths: 50 200
* - /NoLayout
- Layout explicitly not specified
* - /SinglePage
- Show one page at a time
* - /OneColumn
- Show one column at a time
* - /TwoColumnLeft
- Show pages in two columns, odd-numbered pages on the left
* - /TwoColumnRight
- Show pages in two columns, odd-numbered pages on the right
* - /TwoPageLeft
- Show two pages at a time, odd-numbered pages on the left
* - /TwoPageRight
- Show two pages at a time, odd-numbered pages on the right
"""
self._set_page_layout(layout)
@property
def page_layout(self) -> Optional[LayoutType]:
"""
Page layout property.
.. list-table:: Valid ``layout`` values
:widths: 50 200
* - /NoLayout
- Layout explicitly not specified
* - /SinglePage
- Show one page at a time
* - /OneColumn
- Show one column at a time
* - /TwoColumnLeft
- Show pages in two columns, odd-numbered pages on the left
* - /TwoColumnRight
- Show pages in two columns, odd-numbered pages on the right
* - /TwoPageLeft
- Show two pages at a time, odd-numbered pages on the left
* - /TwoPageRight
- Show two pages at a time, odd-numbered pages on the right
"""
return self._get_page_layout()
@page_layout.setter
def page_layout(self, layout: LayoutType) -> None:
self._set_page_layout(layout)
_valid_modes = (
"/UseNone",
"/UseOutlines",
"/UseThumbs",
"/FullScreen",
"/UseOC",
"/UseAttachments",
)
def _get_page_mode(self) -> Optional[PagemodeType]:
try:
return cast(PagemodeType, self._root_object["/PageMode"])
except KeyError:
return None
@property
def page_mode(self) -> Optional[PagemodeType]:
"""
Page mode property.
.. list-table:: Valid ``mode`` values
:widths: 50 200
* - /UseNone
- Do not show outline or thumbnails panels
* - /UseOutlines
- Show outline (aka bookmarks) panel
* - /UseThumbs
- Show page thumbnails panel
* - /FullScreen
- Fullscreen view
* - /UseOC
- Show Optional Content Group (OCG) panel
* - /UseAttachments
- Show attachments panel
"""
return self._get_page_mode()
@page_mode.setter
def page_mode(self, mode: PagemodeType) -> None:
if isinstance(mode, NameObject):
mode_name: NameObject = mode
else:
if mode not in self._valid_modes:
logger_warning(
f"Mode should be one of: {', '.join(self._valid_modes)}", __name__
)
mode_name = NameObject(mode)
self._root_object.update({NameObject("/PageMode"): mode_name})
def add_annotation(
self,
page_number: Union[int, PageObject],
annotation: Dict[str, Any],
) -> DictionaryObject:
"""
Add a single annotation to the page.
The added annotation must be a new annotation.
It cannot be recycled.
Args:
page_number: PageObject or page index.
annotation: Annotation to be added (created with annotation).
Returns:
The inserted object.
This can be used for popup creation, for example.
"""
page = page_number
if isinstance(page, int):
page = self.pages[page]
elif not isinstance(page, PageObject):
raise TypeError("page: invalid type")
to_add = cast(DictionaryObject, _pdf_objectify(annotation))
to_add[NameObject("/P")] = page.indirect_reference
if page.annotations is None:
page[NameObject("/Annots")] = ArrayObject()
assert page.annotations is not None
# Internal link annotations need the correct object type for the
# destination
if to_add.get("/Subtype") == "/Link" and "/Dest" in to_add:
tmp = cast(Dict[Any, Any], to_add[NameObject("/Dest")])
dest = Destination(
NameObject("/LinkName"),
tmp["target_page_index"],
Fit(
fit_type=tmp["fit"], fit_args=dict(tmp)["fit_args"]
), # I have no clue why this dict-hack is necessary
)
to_add[NameObject("/Dest")] = dest.dest_array
page.annotations.append(self._add_object(to_add))
if to_add.get("/Subtype") == "/Popup" and NameObject("/Parent") in to_add:
cast(DictionaryObject, to_add["/Parent"].get_object())[
NameObject("/Popup")
] = to_add.indirect_reference
return to_add
def clean_page(self, page: Union[PageObject, IndirectObject]) -> PageObject:
"""
Perform some clean up in the page.
Currently: convert NameObject named destination to TextStringObject
(required for names/dests list)
Args:
page:
Returns:
The cleaned PageObject
"""
page = cast("PageObject", page.get_object())
for a in page.get("/Annots", []):
a_obj = a.get_object()
d = a_obj.get("/Dest", None)
act = a_obj.get("/A", None)
if isinstance(d, NameObject):
a_obj[NameObject("/Dest")] = TextStringObject(d)
elif act is not None:
act = act.get_object()
d = act.get("/D", None)
if isinstance(d, NameObject):
act[NameObject("/D")] = TextStringObject(d)
return page
def _create_stream(
self, fileobj: Union[Path, StrByteType, PdfReader]
) -> Tuple[IOBase, Optional[Encryption]]:
# If the fileobj parameter is a string, assume it is a path
# and create a file object at that location. If it is a file,
# copy the file's contents into a BytesIO stream object; if
# it is a PdfReader, copy that reader's stream into a
# BytesIO stream.
# If fileobj is none of the above types, it is not modified
encryption_obj = None
stream: IOBase
if isinstance(fileobj, (str, Path)):
with FileIO(fileobj, "rb") as f:
stream = BytesIO(f.read())
elif isinstance(fileobj, PdfReader):
if fileobj._encryption:
encryption_obj = fileobj._encryption
orig_tell = fileobj.stream.tell()
fileobj.stream.seek(0)
stream = BytesIO(fileobj.stream.read())
# reset the stream to its original location
fileobj.stream.seek(orig_tell)
elif hasattr(fileobj, "seek") and hasattr(fileobj, "read"):
fileobj.seek(0)
filecontent = fileobj.read()
stream = BytesIO(filecontent)
else:
raise NotImplementedError(
"PdfMerger.merge requires an object that PdfReader can parse. "
"Typically, that is a Path or a string representing a Path, "
"a file object, or an object implementing .seek and .read. "
"Passing a PdfReader directly works as well."
)
return stream, encryption_obj
def append(
self,
fileobj: Union[StrByteType, PdfReader, Path],
outline_item: Union[
str, None, PageRange, Tuple[int, int], Tuple[int, int, int], List[int]
] = None,
pages: Union[
None,
PageRange,
Tuple[int, int],
Tuple[int, int, int],
List[int],
List[PageObject],
] = None,
import_outline: bool = True,
excluded_fields: Optional[Union[List[str], Tuple[str, ...]]] = None,
) -> None:
"""
Identical to the :meth:`merge()<merge>` method, but assumes you want to
concatenate all pages onto the end of the file instead of specifying a
position.
Args:
fileobj: A File Object or an object that supports the standard
read and seek methods similar to a File Object. Could also be a
string representing a path to a PDF file.
outline_item: Optionally, you may specify a string to build an
outline (aka 'bookmark') to identify the beginning of the
included file.
pages: Can be a :class:`PageRange<pypdf.pagerange.PageRange>`
or a ``(start, stop[, step])`` tuple
or a list of pages to be processed
to merge only the specified range of pages from the source
document into the output document.
import_outline: You may prevent the source document's
outline (collection of outline items, previously referred to as
'bookmarks') from being imported by specifying this as ``False``.
excluded_fields: Provide the list of fields/keys to be ignored
if ``/Annots`` is part of the list, the annotation will be ignored
if ``/B`` is part of the list, the articles will be ignored
"""
if excluded_fields is None:
excluded_fields = ()
if isinstance(outline_item, (tuple, list, PageRange)):
if isinstance(pages, bool):
if not isinstance(import_outline, bool):
excluded_fields = import_outline
import_outline = pages
pages = outline_item
self.merge(
None,
fileobj,
None,
pages,
import_outline,
excluded_fields,
)
else: # if isinstance(outline_item,str):
self.merge(
None,
fileobj,
outline_item,
pages,
import_outline,
excluded_fields,
)
def merge(
self,
position: Optional[int],
fileobj: Union[Path, StrByteType, PdfReader],
outline_item: Optional[str] = None,
pages: Optional[Union[PageRangeSpec, List[PageObject]]] = None,
import_outline: bool = True,
excluded_fields: Optional[Union[List[str], Tuple[str, ...]]] = (),
) -> None:
"""
Merge the pages from the given file into the output file at the
specified page number.
Args:
position: The *page number* to insert this file. File will
be inserted after the given number.
fileobj: A File Object or an object that supports the standard
read and seek methods similar to a File Object. Could also be a
string representing a path to a PDF file.
outline_item: Optionally, you may specify a string to build an outline
(aka 'bookmark') to identify the
beginning of the included file.
pages: can be a :class:`PageRange<pypdf.pagerange.PageRange>`
or a ``(start, stop[, step])`` tuple
or a list of pages to be processed
to merge only the specified range of pages from the source
document into the output document.
import_outline: You may prevent the source document's
outline (collection of outline items, previously referred to as
'bookmarks') from being imported by specifying this as ``False``.
excluded_fields: provide the list of fields/keys to be ignored
if ``/Annots`` is part of the list, the annotation will be ignored
if ``/B`` is part of the list, the articles will be ignored
Raises:
TypeError: The pages attribute is not configured properly
"""
if isinstance(fileobj, PdfDocCommon):
reader = fileobj
else:
stream, encryption_obj = self._create_stream(fileobj)
# Create a new PdfReader instance using the stream
# (either file or BytesIO or StringIO) created above
reader = PdfReader(stream, strict=False) # type: ignore[arg-type]
if excluded_fields is None:
excluded_fields = ()
# Find the range of pages to merge.
if pages is None:
pages = list(range(len(reader.pages)))
elif isinstance(pages, PageRange):
pages = list(range(*pages.indices(len(reader.pages))))
elif isinstance(pages, list):
pass # keep unchanged
elif isinstance(pages, tuple) and len(pages) <= 3:
pages = list(range(*pages))
elif not isinstance(pages, tuple):
raise TypeError(
'"pages" must be a tuple of (start, stop[, step]) or a list'
)
srcpages = {}
for page in pages:
if isinstance(page, PageObject):
pg = page
else:
pg = reader.pages[page]
assert pg.indirect_reference is not None
if position is None:
# numbers in the exclude list identifies that the exclusion is
# only applicable to 1st level of cloning
srcpages[pg.indirect_reference.idnum] = self.add_page(
pg, list(excluded_fields) + [1, "/B", 1, "/Annots"] # type: ignore
)
else:
srcpages[pg.indirect_reference.idnum] = self.insert_page(
pg, position, list(excluded_fields) + [1, "/B", 1, "/Annots"] # type: ignore
)
position += 1
srcpages[pg.indirect_reference.idnum].original_page = pg
reader._namedDests = (
reader.named_destinations
) # need for the outline processing below
for dest in reader._namedDests.values():
arr = dest.dest_array
if "/Names" in self._root_object and dest["/Title"] in cast(
List[Any],
cast(
DictionaryObject,
cast(DictionaryObject, self._root_object["/Names"])["/Dests"],
)["/Names"],
):
# already exists : should not duplicate it
pass
elif isinstance(dest["/Page"], NullObject):
pass
elif isinstance(dest["/Page"], int):
# the page reference is a page number normally not a PDF Reference
# page numbers as int are normally accepted only in external goto
p = reader.pages[dest["/Page"]]
assert p.indirect_reference is not None
try:
arr[NumberObject(0)] = NumberObject(
srcpages[p.indirect_reference.idnum].page_number
)
self.add_named_destination_array(dest["/Title"], arr)
except KeyError:
pass
elif dest["/Page"].indirect_reference.idnum in srcpages:
arr[NumberObject(0)] = srcpages[
dest["/Page"].indirect_reference.idnum
].indirect_reference
self.add_named_destination_array(dest["/Title"], arr)
outline_item_typ: TreeObject
if outline_item is not None:
outline_item_typ = cast(
"TreeObject",
self.add_outline_item(
TextStringObject(outline_item),
next(iter(srcpages.values())).indirect_reference,
fit=PAGE_FIT,
).get_object(),
)
else:
outline_item_typ = self.get_outline_root()
_ro = reader.root_object
if import_outline and CO.OUTLINES in _ro:
outline = self._get_filtered_outline(
_ro.get(CO.OUTLINES, None), srcpages, reader
)
self._insert_filtered_outline(
outline, outline_item_typ, None
) # TODO : use before parameter
if "/Annots" not in excluded_fields:
for pag in srcpages.values():
lst = self._insert_filtered_annotations(
pag.original_page.get("/Annots", ()), pag, srcpages, reader
)
if len(lst) > 0:
pag[NameObject("/Annots")] = lst
self.clean_page(pag)
if "/AcroForm" in _ro and _ro["/AcroForm"] is not None:
if "/AcroForm" not in self._root_object:
self._root_object[NameObject("/AcroForm")] = self._add_object(
cast(
DictionaryObject,
reader.root_object["/AcroForm"],
).clone(self, False, ("/Fields",))
)
arr = ArrayObject()
else:
arr = cast(
ArrayObject,
cast(DictionaryObject, self._root_object["/AcroForm"])["/Fields"],
)
trslat = self._id_translated[id(reader)]
try:
for f in reader.root_object["/AcroForm"]["/Fields"]: # type: ignore
try:
ind = IndirectObject(trslat[f.idnum], 0, self)
if ind not in arr:
arr.append(ind)
except KeyError:
# for trslat[] which mean the field has not be copied
# through the page
pass
except KeyError: # for /Acroform or /Fields are not existing
arr = self._add_object(ArrayObject())
cast(DictionaryObject, self._root_object["/AcroForm"])[
NameObject("/Fields")
] = arr
if "/B" not in excluded_fields:
self.add_filtered_articles("", srcpages, reader)
def _add_articles_thread(
self,
thread: DictionaryObject, # thread entry from the reader's array of threads
pages: Dict[int, PageObject],
reader: PdfReader,
) -> IndirectObject:
"""
Clone the thread with only the applicable articles.
Args:
thread:
pages:
reader:
Returns:
The added thread as an indirect reference
"""
nthread = thread.clone(
self, force_duplicate=True, ignore_fields=("/F",)
) # use of clone to keep link between reader and writer
self.threads.append(nthread.indirect_reference)
first_article = cast("DictionaryObject", thread["/F"])
current_article: Optional[DictionaryObject] = first_article
new_article: Optional[DictionaryObject] = None
while current_article is not None:
pag = self._get_cloned_page(
cast("PageObject", current_article["/P"]), pages, reader
)
if pag is not None:
if new_article is None:
new_article = cast(
"DictionaryObject",
self._add_object(DictionaryObject()).get_object(),
)
new_first = new_article
nthread[NameObject("/F")] = new_article.indirect_reference
else:
new_article2 = cast(
"DictionaryObject",
self._add_object(
DictionaryObject(
{NameObject("/V"): new_article.indirect_reference}
)
).get_object(),
)
new_article[NameObject("/N")] = new_article2.indirect_reference
new_article = new_article2
new_article[NameObject("/P")] = pag
new_article[NameObject("/T")] = nthread.indirect_reference
new_article[NameObject("/R")] = current_article["/R"]
pag_obj = cast("PageObject", pag.get_object())
if "/B" not in pag_obj:
pag_obj[NameObject("/B")] = ArrayObject()
cast("ArrayObject", pag_obj["/B"]).append(
new_article.indirect_reference
)
current_article = cast("DictionaryObject", current_article["/N"])
if current_article == first_article:
new_article[NameObject("/N")] = new_first.indirect_reference # type: ignore
new_first[NameObject("/V")] = new_article.indirect_reference # type: ignore
current_article = None
assert nthread.indirect_reference is not None
return nthread.indirect_reference
def add_filtered_articles(
self,
fltr: Union[
Pattern[Any], str
], # thread entry from the reader's array of threads
pages: Dict[int, PageObject],
reader: PdfReader,
) -> None:
"""
Add articles matching the defined criteria.
Args:
fltr:
pages:
reader:
"""
if isinstance(fltr, str):
fltr = re.compile(fltr)
elif not isinstance(fltr, Pattern):
fltr = re.compile("")
for p in pages.values():
pp = p.original_page
for a in pp.get("/B", ()):
thr = a.get_object().get("/T")
if thr is None:
continue
else:
thr = thr.get_object()
if thr.indirect_reference.idnum not in self._id_translated[
id(reader)
] and fltr.search((thr["/I"] if "/I" in thr else {}).get("/Title", "")):
self._add_articles_thread(thr, pages, reader)
def _get_cloned_page(
self,
page: Union[None, int, IndirectObject, PageObject, NullObject],
pages: Dict[int, PageObject],
reader: PdfReader,
) -> Optional[IndirectObject]:
if isinstance(page, NullObject):
return None
if isinstance(page, int):
_i = reader.pages[page].indirect_reference
elif isinstance(page, DictionaryObject) and page.get("/Type", "") == "/Page":
_i = page.indirect_reference
elif isinstance(page, IndirectObject):
_i = page
try:
return pages[_i.idnum].indirect_reference # type: ignore
except Exception:
return None
def _insert_filtered_annotations(
self,
annots: Union[IndirectObject, List[DictionaryObject]],
page: PageObject,
pages: Dict[int, PageObject],
reader: PdfReader,
) -> List[Destination]:
outlist = ArrayObject()
if isinstance(annots, IndirectObject):
annots = cast("List[Any]", annots.get_object())
for an in annots:
ano = cast("DictionaryObject", an.get_object())
if (
ano["/Subtype"] != "/Link"
or "/A" not in ano
or cast("DictionaryObject", ano["/A"])["/S"] != "/GoTo"
or "/Dest" in ano
):
if "/Dest" not in ano:
outlist.append(self._add_object(ano.clone(self)))
else:
d = ano["/Dest"]
if isinstance(d, str):
# it is a named dest
if str(d) in self.get_named_dest_root():
outlist.append(ano.clone(self).indirect_reference)
else:
d = cast("ArrayObject", d)
p = self._get_cloned_page(d[0], pages, reader)
if p is not None:
anc = ano.clone(self, ignore_fields=("/Dest",))
anc[NameObject("/Dest")] = ArrayObject([p] + d[1:])
outlist.append(self._add_object(anc))
else:
d = cast("DictionaryObject", ano["/A"])["/D"]
if isinstance(d, str):
# it is a named dest
if str(d) in self.get_named_dest_root():
outlist.append(ano.clone(self).indirect_reference)
else:
d = cast("ArrayObject", d)
p = self._get_cloned_page(d[0], pages, reader)
if p is not None:
anc = ano.clone(self, ignore_fields=("/D",))
cast("DictionaryObject", anc["/A"])[
NameObject("/D")
] = ArrayObject([p] + d[1:])
outlist.append(self._add_object(anc))
return outlist
def _get_filtered_outline(
self,
node: Any,
pages: Dict[int, PageObject],
reader: PdfReader,
) -> List[Destination]:
"""
Extract outline item entries that are part of the specified page set.
Args:
node:
pages:
reader:
Returns:
A list of destination objects.
"""
new_outline = []
if node is None:
node = NullObject()
node = node.get_object()
if node is None or isinstance(node, NullObject):
node = DictionaryObject()
if node.get("/Type", "") == "/Outlines" or "/Title" not in node:
node = node.get("/First", None)
if node is not None:
node = node.get_object()
new_outline += self._get_filtered_outline(node, pages, reader)
else:
v: Union[None, IndirectObject, NullObject]
while node is not None:
node = node.get_object()
o = cast("Destination", reader._build_outline_item(node))
v = self._get_cloned_page(cast("PageObject", o["/Page"]), pages, reader)
if v is None:
v = NullObject()
o[NameObject("/Page")] = v
if "/First" in node:
o._filtered_children = self._get_filtered_outline(
node["/First"], pages, reader
)
else:
o._filtered_children = []
if (
not isinstance(o["/Page"], NullObject)
or len(o._filtered_children) > 0
):
new_outline.append(o)
node = node.get("/Next", None)
return new_outline
def _clone_outline(self, dest: Destination) -> TreeObject:
n_ol = TreeObject()
self._add_object(n_ol)
n_ol[NameObject("/Title")] = TextStringObject(dest["/Title"])
if not isinstance(dest["/Page"], NullObject):
if dest.node is not None and "/A" in dest.node:
n_ol[NameObject("/A")] = dest.node["/A"].clone(self)
else:
n_ol[NameObject("/Dest")] = dest.dest_array
# TODO: /SE
if dest.node is not None:
n_ol[NameObject("/F")] = NumberObject(dest.node.get("/F", 0))
n_ol[NameObject("/C")] = ArrayObject(
dest.node.get(
"/C", [FloatObject(0.0), FloatObject(0.0), FloatObject(0.0)]
)
)
return n_ol
def _insert_filtered_outline(
self,
outlines: List[Destination],
parent: Union[TreeObject, IndirectObject],
before: Union[None, TreeObject, IndirectObject] = None,
) -> None:
for dest in outlines:
# TODO : can be improved to keep A and SE entries (ignored for the moment)
# with np=self.add_outline_item_destination(dest,parent,before)
if dest.get("/Type", "") == "/Outlines" or "/Title" not in dest:
np = parent
else:
np = self._clone_outline(dest)
cast(TreeObject, parent.get_object()).insert_child(np, before, self)
self._insert_filtered_outline(dest._filtered_children, np, None)
def close(self) -> None:
"""Implemented for API harmonization."""
return
def find_outline_item(
self,
outline_item: Dict[str, Any],
root: Optional[OutlineType] = None,
) -> Optional[List[int]]:
if root is None:
o = self.get_outline_root()
else:
o = cast("TreeObject", root)
i = 0
while o is not None:
if (
o.indirect_reference == outline_item
or o.get("/Title", None) == outline_item
):
return [i]
elif "/First" in o:
res = self.find_outline_item(
outline_item, cast(OutlineType, o["/First"])
)
if res:
return ([i] if "/Title" in o else []) + res
if "/Next" in o:
i += 1
o = cast(TreeObject, o["/Next"])
else:
return None
def find_bookmark(
self,
outline_item: Dict[str, Any],
root: Optional[OutlineType] = None,
) -> Optional[List[int]]: # deprecated
"""
.. deprecated:: 2.9.0
Use :meth:`find_outline_item` instead.
"""
deprecate_with_replacement("find_bookmark", "find_outline_item", "5.0.0")
return self.find_outline_item(outline_item, root)
def reset_translation(
self, reader: Union[None, PdfReader, IndirectObject] = None
) -> None:
"""
Reset the translation table between reader and the writer object.
Late cloning will create new independent objects.
Args:
reader: PdfReader or IndirectObject referencing a PdfReader object.
if set to None or omitted, all tables will be reset.
"""
if reader is None:
self._id_translated = {}
elif isinstance(reader, PdfReader):
try:
del self._id_translated[id(reader)]
except Exception:
pass
elif isinstance(reader, IndirectObject):
try:
del self._id_translated[id(reader.pdf)]
except Exception:
pass
else:
raise Exception("invalid parameter {reader}")
def set_page_label(
self,
page_index_from: int,
page_index_to: int,
style: Optional[PageLabelStyle] = None,
prefix: Optional[str] = None,
start: Optional[int] = 0,
) -> None:
"""
Set a page label to a range of pages.
Page indexes must be given starting from 0.
Labels must have a style, a prefix or both.
If to a range is not assigned any page label a decimal label starting from 1 is applied.
Args:
page_index_from: page index of the beginning of the range starting from 0
page_index_to: page index of the beginning of the range starting from 0
style: The numbering style to be used for the numeric portion of each page label:
* ``/D`` Decimal arabic numerals
* ``/R`` Uppercase roman numerals
* ``/r`` Lowercase roman numerals
* ``/A`` Uppercase letters (A to Z for the first 26 pages,
AA to ZZ for the next 26, and so on)
* ``/a`` Lowercase letters (a to z for the first 26 pages,
aa to zz for the next 26, and so on)
prefix: The label prefix for page labels in this range.
start: The value of the numeric portion for the first page label
in the range.
Subsequent pages are numbered sequentially from this value,
which must be greater than or equal to 1.
Default value: 1.
"""
if style is None and prefix is None:
raise ValueError("at least one between style and prefix must be given")
if page_index_from < 0:
raise ValueError("page_index_from must be equal or greater then 0")
if page_index_to < page_index_from:
raise ValueError(
"page_index_to must be equal or greater then page_index_from"
)
if page_index_to >= len(self.pages):
raise ValueError("page_index_to exceeds number of pages")
if start is not None and start != 0 and start < 1:
raise ValueError("if given, start must be equal or greater than one")
self._set_page_label(page_index_from, page_index_to, style, prefix, start)
def _set_page_label(
self,
page_index_from: int,
page_index_to: int,
style: Optional[PageLabelStyle] = None,
prefix: Optional[str] = None,
start: Optional[int] = 0,
) -> None:
"""
Set a page label to a range of pages.
Page indexes must be given
starting from 0. Labels must have a style, a prefix or both. If to a
range is not assigned any page label a decimal label starting from 1 is
applied.
Args:
page_index_from: page index of the beginning of the range starting from 0
page_index_to: page index of the beginning of the range starting from 0
style: The numbering style to be used for the numeric portion of each page label:
/D Decimal arabic numerals
/R Uppercase roman numerals
/r Lowercase roman numerals
/A Uppercase letters (A to Z for the first 26 pages,
AA to ZZ for the next 26, and so on)
/a Lowercase letters (a to z for the first 26 pages,
aa to zz for the next 26, and so on)
prefix: The label prefix for page labels in this range.
start: The value of the numeric portion for the first page label
in the range.
Subsequent pages are numbered sequentially from this value,
which must be greater than or equal to 1. Default value: 1.
"""
default_page_label = DictionaryObject()
default_page_label[NameObject("/S")] = NameObject("/D")
new_page_label = DictionaryObject()
if style is not None:
new_page_label[NameObject("/S")] = NameObject(style)
if prefix is not None:
new_page_label[NameObject("/P")] = TextStringObject(prefix)
if start != 0:
new_page_label[NameObject("/St")] = NumberObject(start)
if NameObject(CatalogDictionary.PAGE_LABELS) not in self._root_object:
nums = ArrayObject()
nums_insert(NumberObject(0), default_page_label, nums)
page_labels = TreeObject()
page_labels[NameObject("/Nums")] = nums
self._root_object[NameObject(CatalogDictionary.PAGE_LABELS)] = page_labels
page_labels = cast(
TreeObject, self._root_object[NameObject(CatalogDictionary.PAGE_LABELS)]
)
nums = cast(ArrayObject, page_labels[NameObject("/Nums")])
nums_insert(NumberObject(page_index_from), new_page_label, nums)
nums_clear_range(NumberObject(page_index_from), page_index_to, nums)
next_label_pos, *_ = nums_next(NumberObject(page_index_from), nums)
if next_label_pos != page_index_to + 1 and page_index_to + 1 < len(self.pages):
nums_insert(NumberObject(page_index_to + 1), default_page_label, nums)
page_labels[NameObject("/Nums")] = nums
self._root_object[NameObject(CatalogDictionary.PAGE_LABELS)] = page_labels
def _pdf_objectify(obj: Union[Dict[str, Any], str, int, List[Any]]) -> PdfObject:
if isinstance(obj, PdfObject):
return obj
if isinstance(obj, dict):
to_add = DictionaryObject()
for key, value in obj.items():
name_key = NameObject(key)
casted_value = _pdf_objectify(value)
to_add[name_key] = casted_value
return to_add
elif isinstance(obj, list):
return ArrayObject(_pdf_objectify(el) for el in obj)
elif isinstance(obj, str):
if obj.startswith("/"):
return NameObject(obj)
else:
return TextStringObject(obj)
elif isinstance(obj, (int, float)):
return FloatObject(obj)
else:
raise NotImplementedError(
f"type(obj)={type(obj)} could not be casted to PdfObject"
)
def _create_outline_item(
action_ref: Union[None, IndirectObject],
title: str,
color: Union[Tuple[float, float, float], str, None],
italic: bool,
bold: bool,
) -> TreeObject:
outline_item = TreeObject()
if action_ref is not None:
outline_item[NameObject("/A")] = action_ref
outline_item.update(
{
NameObject("/Title"): create_string_object(title),
}
)
if color:
if isinstance(color, str):
color = hex_to_rgb(color)
outline_item.update(
{NameObject("/C"): ArrayObject([FloatObject(c) for c in color])}
)
if italic or bold:
format_flag = 0
if italic:
format_flag += 1
if bold:
format_flag += 2
outline_item.update({NameObject("/F"): NumberObject(format_flag)})
return outline_item
|