-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathcpdfmanual.tex
6835 lines (5176 loc) · 268 KB
/
cpdfmanual.tex
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
%\DocumentMetadata{lang=en, pdfversion=2.0, pdfstandard=ua-2, pdfstandard=a-4f, testphase={phase-III, title, table, math, firstaid}}
\documentclass{book}
% Edit here to produce cpdfmanual.pdf, cpdflibmanual.pdf, pycpdfmanual.pdf,
% dotnetcpdflibmanual.pdf, jcpdflibmanual.pdf jscpdflibmanual.pdf etc.
\usepackage{comment}
\excludecomment{cpdflib}
\excludecomment{pycpdflib}
\excludecomment{dotnetcpdflib}
\excludecomment{jcpdflib}
\excludecomment{jscpdflib}
%Our packages
\usepackage{alltt}
\usepackage{palatino}
\usepackage{listings}
\usepackage{microtype}
\usepackage{graphicx}
\usepackage{upquote}
\PassOptionsToPackage{hyphens}{url}\usepackage[plainpages=false,pdfpagelabels,pdfborder=0 0 0,draft=false,hidelinks,bookmarksnumbered]{hyperref}
\usepackage{framed}
\newcommand{\smallgap}{\bigskip}
\addtolength{\textwidth}{20mm}
\usepackage{makeidx}\makeindex
\usepackage[left=3cm, right=1.5cm, top=2cm, bottom=1.8cm, paperwidth=7.5in, paperheight=9.25in]{geometry}
\usepackage{fancyhdr}
\fancyhf{}
\pagestyle{fancy}
\fancyhead[lo]{\slshape\nouppercase{\leftmark}\hfill\thepage}
\fancyhead[re]{\thepage\hfill\slshape\nouppercase{\leftmark}}
\fancyfoot{}
\renewcommand{\headrulewidth}{0pt}
\renewcommand{\footrulewidth}{0pt}
\usepackage{pmboxdraw}
\usepackage{longtable}
%usepackage{multicol}
\begin{document}
\frontmatter
\pagestyle{empty}
\begin{flushright}
\ifdefined\HCode
{\sffamily \bfseries \Huge Coherent PDF Command Line Tools}
\else
{\sffamily \bfseries \Huge Coherent PDF
\vspace{2mm}
\begin{cpdflib}
C API and
\end{cpdflib}
\begin{pycpdflib}
Python API and
\end{pycpdflib}
\begin{dotnetcpdflib}
.NET API and
\end{dotnetcpdflib}
\begin{jcpdflib}
Java API and
\end{jcpdflib}
\begin{jscpdflib}
JavaScript API and
\end{jscpdflib}
Command Line Tools}
\fi
\vspace{12mm}
{\Huge User Manual}\\
Version 2.8 (December 2024)
\vspace{25mm}
\vfill
\ifdefined\HCode
\includegraphics[natwidth=50,natheight=50,alt={Coherent Graphics Ltd logo}]{logo.pdf}
\else
\includegraphics[alt={Coherent Graphics Ltd logo}]{logo.pdf}
\fi
\vspace{2mm}
{\sffamily \bfseries \LARGE Coherent Graphics Ltd}
\end{flushright}
\clearpage
\pagestyle{empty}
\noindent For bug reports, feature requests and comments, email\\ \texttt{[email protected]}
\vspace*{\fill}
\noindent\copyright\ Coherent Graphics Limited. All rights reserved. ISBN 978-0957671140
\smallgap
\noindent Adobe, Acrobat, and Adobe PDF are
registered trademarks of Adobe Systems Incorporated. Windows, Powerpoint and
Excel are registered trademarks of Microsoft Corporation. The verification descriptions for the Matterhorn Protocol come from the PDF
Association, and are licensed under the Creative Commons Attribution 4.0
International license.
\cleardoublepage
\pagestyle{plain}
\chapter*{Quickstart Examples}
These examples demonstrate just a few of the facilities provided by the Coherent PDF Command Line Tools. See each chapter for more commands and full details.
\section*{\hyperref[chap:1]{Chapter 1: Basic Usage}}
\begin{framed}\noindent\texttt{cpdf in.pdf 1-3,6 -o out.pdf}\end{framed}
\noindent Read \texttt{in.pdf}, select pages 1, 2, 3 and 6, and write those pages to \texttt{out.pdf}.
\begin{framed}\noindent\texttt{cpdf in.pdf even -o out.pdf}\end{framed}
\noindent Select the even pages (2, 4, 6...) from \texttt{in.pdf} and write those pages to \texttt{out.pdf}.
\begin{framed}
\noindent\small\verb?cpdf -merge in.pdf in2.pdf AND -add-text "Copyright 2024"?\\
\noindent\small\verb? -o out.pdf?
\end{framed}
\noindent Using \texttt{AND} to perform several operations in order, here merging two files together and adding a copyright stamp to every page.
\begin{framed}\noindent\texttt{cpdf -args args.txt}\end{framed}
\noindent Read \texttt{args.txt} and use its contents as the command line arguments for Cpdf.
\section*{\hyperref[chap:2]{Chapter 2: Merging and Splitting}}
\begin{framed}\noindent\texttt{cpdf -merge in.pdf in2.pdf -o out.pdf}\end{framed}
\noindent Merge \texttt{in.pdf} and \texttt{in2.pdf} into one document, writing to \texttt{out.pdf}.
\begin{framed}\noindent\texttt{cpdf -split in.pdf -o Chunk\%\%\%.pdf -chunk 10}\end{framed}
\noindent Split \texttt{in.pdf} into ten-page chunks, writing them to \texttt{Chunk001.pdf}, \texttt{Chunk002.pdf} etc.
\begin{framed}\noindent\texttt{cpdf -split-bookmarks 0 in.pdf -utf8 -o @B.pdf}\end{framed}
\noindent Split \texttt{in.pdf} on bookmark boundaries, writing each to a file whose name is the bookmark label.
\begin{framed}\noindent\texttt{cpdf -split-max 1Mb in.pdf -o \%\%\%.pdf}\end{framed}
\noindent Split \texttt{in.pdf} into files of 1Mb or less
\begin{framed}\noindent\texttt{cpdf -spray in.pdf -o a.pdf -o b.pdf -o c.pdf}\end{framed}
\noindent Split \texttt{in.pdf}, writing pages 1,4,7... to \texttt{a.pdf}, 2,5,8... to \texttt{b.pdf} and 3,6,9... to \texttt{c.pdf}.
\section*{\hyperref[chap:3]{Chapter 3: Pages}}
\begin{framed}\noindent\texttt{cpdf -scale-page "2 2" in.pdf -o out.pdf}\end{framed}
\noindent Scale both the dimensions and contents of \texttt{in.pdf} by a factor of two in x and y directions.
\begin{framed}\noindent\texttt{cpdf -scale-to-fit usletterportrait in.pdf -o out.pdf}\end{framed}
\noindent Scale the pages in \texttt{in.pdf} to fit the US Letter page size, writing to \texttt{out.pdf}
\begin{framed}\noindent\texttt{cpdf -shift "26pt 18mm" in.pdf -o out.pdf}\end{framed}
\noindent Shift the contents of the page by 26 pts in the x direction, and 18 millimetres in the y direction, writing to \texttt{out.pdf}
\begin{framed}\noindent\texttt{cpdf -rotate-contents 90 in.pdf -o out.pdf}\end{framed}
\noindent Rotate the contents of the pages in \texttt{in.pdf} by ninety degrees and write to \texttt{out.pdf}.
\begin{framed}\noindent\texttt{cpdf -cropbox "0 0 600pt 400pt" in.pdf -o out.pdf}\end{framed}
\noindent Crop the pages in \texttt{in.pdf} to a 600 pts by 400 pts rectangle.
\section*{\hyperref[chap:4]{Chapter 4: Encryption and Decryption}}
\begin{framed}\noindent\texttt{cpdf -encrypt 128bit fred joe in.pdf -o out.pdf}\end{framed}
\noindent Encrypt \texttt{in.pdf} using 128bit PDF encryption using the owner password \texttt{fred} and the user password \texttt{joe} and writing the encrypted file to \texttt{out.pdf}
\begin{framed}\noindent\texttt{cpdf -decrypt in.pdf owner=fred -o out.pdf}\end{framed}
\noindent Decrypt \texttt{in.pdf} using the owner password, writing to \texttt{out.pdf}.
\section*{\hyperref[chap:5]{Chapter 5: Compression}}
\begin{framed}\noindent\texttt{cpdf -compress in.pdf -o out.pdf}\end{framed}
\noindent Compress the data streams in \texttt{in.pdf}, writing the result to \texttt{out.pdf}.
\begin{framed}\noindent\texttt{cpdf -decompress in.pdf -o out.pdf}\end{framed}
\noindent Decompress the data streams in \texttt{in.pdf}, writing to \texttt{out.pdf}.
\begin{framed}\noindent\texttt{cpdf -squeeze in.pdf -o out.pdf}\end{framed}
\noindent Squeeze \texttt{in.pdf}, writing to \texttt{out.pdf}. Squeezing rearranges the structure of the PDF file to save space.
\section*{\hyperref[chap:6]{Chapter 6: Bookmarks}}
\begin{framed}\noindent\texttt{cpdf -list-bookmarks -utf8 in.pdf}\end{framed}
\noindent List the bookmarks in \texttt{in.pdf}.
\begin{framed}\noindent\texttt{cpdf -add-bookmarks bookmarks.txt in.pdf -o out.pdf}\end{framed}
\noindent Add bookmarks in the same form from a prepared file \texttt{bookmarks.txt} to \texttt{in.pdf}, writing to \texttt{out.pdf}. JSON alternatives are also available.
\begin{framed}\noindent\texttt{cpdf -table-of-contents in.pdf -o out.pdf}\end{framed}
\noindent Typeset a table of contents from existing bookmarks and prepend to the document.
\section*{\hyperref[chap:7]{Chapter 7: Presentations}}
\begin{framed}
\noindent\small\verb?cpdf -presentation in.pdf 2-end -trans Split -duration 10?\\
\noindent\small\verb? -o out.pdf?
\end{framed}
\noindent Use the Split style to build a presentation from the PDF \texttt{in.pdf}, each slide staying 10 seconds on screen unless manually advanced. The first page, being a title does not move on automatically, and has no transition effect.
\section*{\hyperref[chap:8]{Chapter 8: Logos, Watermarks and Stamps}}
\begin{framed}\noindent\texttt{cpdf -stamp-on watermark.pdf in.pdf -o out.pdf}\end{framed}
\noindent Stamp the file \texttt{watermark.pdf} on to each page of \texttt{in.pdf}, writing the result to \texttt{out.pdf}.
\begin{framed}
\noindent\small\verb?cpdf -topleft 10 -font Courier?\\
\noindent\small\verb? -add-text "Page \%Page\nDate \%d-\%m-\%Y" in.pdf -o out.pdf?
\end{framed}
\noindent Add a page number and date to all the pages in \texttt{in.pdf} using the Courier font, writing to \texttt{out.pdf}.
\section*{\hyperref[chap:9]{Chapter 9: Multipage Facilities}}
\begin{framed}\noindent\texttt{cpdf -impose-xy "2 1" in.pdf -o out.pdf}\end{framed}
\noindent Two up impose the file \texttt{in.pdf}, writing to \texttt{out.pdf}.
\begin{framed}\noindent\texttt{cpdf -pad-after in.pdf 1,3,4 -o out.pdf}\end{framed}
\noindent Add extra blank pages after pages one, three and four of a document.
\begin{framed}\noindent\texttt{cpdf -chop "2 2" in.pdf -o out.pdf}\end{framed}
\noindent Chop each page into four quarters, including each in the output.
\section*{\hyperref[chap:10]{Chapter 10: Annotations}}
\begin{framed}\noindent\texttt{cpdf -list-annotations-json in.pdf > out.json}\end{framed}
\noindent List the annotations in a file \texttt{in.pdf} to standard output, redirecting to file \texttt{out.json}.
\begin{framed}\noindent\texttt{cpdf -set-annotations-json out.json in.pdf -o out.pdf}\end{framed}
\noindent Add the annotations from a JSON annotations file to \texttt{in.pdf}, writing to \texttt{out.pdf}.
\begin{framed}\noindent\texttt{cpdf -remove-annotations in.pdf -o out.pdf}\end{framed}
\noindent Remove the annotations from \texttt{in.pdf}, writing to \texttt{out.pdf}.
\section*{\hyperref[chap:11]{Chapter 11: Document Information and Metadata}}
\begin{framed}\noindent\texttt{cpdf -info -utf8 in.pdf}\end{framed}
\noindent List document metadata for \texttt{in.pdf}.
\begin{framed}\noindent\texttt{cpdf -set-title "The New Title" -also-set-xmp in.pdf -o out.pdf}\end{framed}
\noindent Set the document title of \texttt{in.pdf}, writing to \texttt{out.pdf}.
\begin{framed}\noindent\texttt{cpdf -hide-toolbar true in.pdf -o out.pdf}\end{framed}
\noindent Set the document \texttt{in.pdf} to open with the PDF Viewer's toolbar hidden, writing to \texttt{out.pdf}.
\begin{framed}\noindent\texttt{cpdf -set-metadata metadata.xml in.pdf -o out.pdf}\end{framed}
\noindent Set the metadata in a PDF \texttt{in.pdf} to the contents of the file \texttt{metadata.xml}, and write the output to \texttt{out.pdf}.
\begin{framed}\noindent\texttt{cpdf -set-page-layout TwoColumnRight in.pdf -o out.pdf}\end{framed}
\noindent Set the document \texttt{in.pdf} to open in PDF Viewer showing two columns of pages, starting on the right, putting the result in \texttt{out.pdf}.
\begin{framed}\noindent\texttt{cpdf -set-page-mode FullScreen in.pdf -o out.pdf}\end{framed}
\noindent Set the document \texttt{in.pdf} to open in PDF Viewer in full screen mode, putting the result in \texttt{out.pdf}.
\begin{framed}\noindent\texttt{cpdf -print-page-labels-json in.pdf}\end{framed}
\noindent Show, in JSON format, the page labels in \texttt{in.pdf}.
\begin{framed}\noindent\texttt{cpdf -composition in.pdf}\end{framed}
\noindent Show how much data in \texttt{in.pdf} is used for images, fonts etc.
\section*{\hyperref[chap:12]{Chapter 12: File Attachments}}
\begin{framed}\noindent\texttt{cpdf -attach-file sheet.xls in.pdf -o out.pdf}\end{framed}
\noindent Attach the file \texttt{sheet.xls} to \texttt{in.pdf}, writing to \texttt{out.pdf}.
\begin{framed}\noindent\texttt{cpdf -remove-files in.pdf -o out.pdf}\end{framed}
\noindent Remove any attachments from \texttt{in.pdf}, writing to \texttt{out.pdf}.
\begin{framed}\noindent\texttt{cpdf -dump-attachments in.pdf -o /home/fred/attachments}\end{framed}
\noindent Dump attachments to file, given the directory to put them in.
\section*{\hyperref[chap:13]{Chapter 13: Images}}
\begin{framed}\noindent\texttt{cpdf -image-resolution 600 in.pdf}\end{framed}
\noindent Identify and list any image used at less than 600dpi.
\begin{framed}\noindent\texttt{cpdf -extract-images in.pdf -im /usr/bin/magick -o output/\%\%\%}\end{framed}
\noindent Extract images from \texttt{in.pdf} to directory \texttt{output} (with the help of imagemagick).
\begin{framed}\noindent\texttt{cpdf -process-images -jpeg-to-jpeg 65 in.pdf -o out.pdf}\end{framed}
\noindent Process JPEG images in \texttt{in.pdf} to 65\% quality, writing the output to \texttt{out.pdf}.
\begin{framed}\noindent\texttt{cpdf -gs gs -rasterize in.pdf -o out.pdf}\end{framed}
\noindent Rasterize PDF page content, creating new PDF.
\begin{framed}\noindent\texttt{cpdf -gs gs -output-image in.pdf 10-end -o image\%\%\%.png}\end{framed}
\noindent Rasterize PDF pages to PNG files.
\section*{\hyperref[chap:14]{Chapter 14: Fonts}}
\begin{framed}\noindent\texttt{cpdf -list-fonts in.pdf}\end{framed}
\noindent List the fonts in use, and what pages they are used on.
\begin{framed}\noindent\texttt{cpdf -missing-fonts in.pdf}\end{framed}
\noindent List missing fonts.
\section*{\hyperref[chap:15]{Chapter 15: PDF and JSON}}
\begin{framed}
\noindent\small\verb?cpdf in.pdf -output-json -utf8 -output-json-parse-content-streams?\\
\noindent\small\verb? -o out.json?
\end{framed}
\noindent Write the PDF in JSON format to the given file, parsing its content streams into individual JSON objects too.
\begin{framed}\noindent\texttt{cpdf -j in.json -o out.pdf}\end{framed}
\noindent Load a PDF in JSON format, writing to an output PDF.
\section*{\hyperref[chap:16]{Chapter 16: Optional Content Groups}}
\begin{framed}\noindent\texttt{cpdf -ocg-list in.pdf}\end{framed}
\noindent List the optional content groups by name.
\begin{framed}\noindent\texttt{cpdf -ocg-coalesce-on-name in.pdf -o out.pdf}\end{framed}
\noindent Coalesce optional content groups after merging or stamping two files with OCGs with like names.
\section*{\hyperref[chap:17]{Chapter 17: Creating New PDFs}}
\begin{framed}
\noindent\small\verb?cpdf -create-pdf -create-pdf-pages 20?\\
\noindent\small\verb? -create-pdf-papersize usletterportrait -o out.pdf?
\end{framed}
\noindent Create a US Letter PDF of twenty pages.
\begin{framed}
\noindent\small\verb?cpdf -typeset file.txt -create-pdf-papersize a3portrait?\\
\noindent\small\verb? -font Courier -font-size 10 -o out.pdf?
\end{framed}
\noindent Typeset a text file as PDF on A3 paper with Courier 10 point font.
\begin{framed}\noindent\texttt{cpdf -jpeg pic.jpeg -png pic.png -o out.pdf}\end{framed}
\noindent Make a two-page PDF, the first from a JPEG and the second from a PNG.
\section*{\hyperref[chap:18]{Chapter 18: Drawing on PDFs}}
\begin{framed}
\noindent\small\verb?cpdf -create-pdf AND -draw -to "100 100" -line "400 400"?\\
\noindent\small\verb? -line "400 100" -close -fill?\\
\noindent\small\verb? -o out.pdf?
\end{framed}
\noindent Create a new PDF and draw a filled triangle on it.
\begin{framed}
\noindent\small\verb?cpdf -create-pdf AND -draw -mtrans "100 200" -font-size 50?\\
\noindent\small\verb? -leading 55 -bt -text "This is" -nl -text "on multiple"?\\
\noindent\small\verb? -nl -text "lines" -et -o out.pdf?
\end{framed}
\noindent Create a new PDF and draw three lines of text on it.
\begin{framed}
\noindent\small\verb!cpdf -create-pdf AND -draw -bt -text "Page 1" -et -newpage!\\
\noindent\small\verb! -bt -text "Page 2" -et -o out.pdf!
\end{framed}
\noindent Create a new PDF and draw text on one page and then the next.
\section*{\hyperref[chap:misc]{Chapter 19: Accessible PDFs with PDF/UA}}
\begin{framed}\noindent\texttt{cpdf -verify 'PDF/UA-1(matterhorn)' -json in.pdf}\end{framed}
\noindent Verify \texttt{in.pdf} for conformance to PDF/UA-1 using the Matterhorn protocol, returning results in JSON format.
\section*{\hyperref[chap:misc]{Chapter 20: Miscellaneous}}
\begin{framed}\noindent\texttt{cpdf -blacktext in.pdf -o out.pdf}\end{framed}
\noindent Blacken all the text in \texttt{in.pdf}, writing to \texttt{out.pdf}.
\begin{framed}\noindent\texttt{cpdf -thinlines 2pt in.pdf -o out.pdf}\end{framed}
\noindent Make sure all lines in \texttt{in.pdf} are at least 2pts wide, writing to \texttt{out.pdf}.
\begin{framed}\noindent\texttt{cpdf -print-dict-entry /URI in.pdf}\end{framed}
\noindent List all URLs in annotation hyperlinks in \texttt{in.pdf}.
\pagestyle{empty}\thispagestyle{plain}
\cleardoublepage
\begin{cpdflib}
\chapter*{Example Program in C}
This program loads a file \texttt{hello.pdf} from disk and writes out a document with the original included three times. Note the use of \texttt{cpdf\_startup}, \texttt{cpdf\_lastError} and \texttt{cpdf\_clearError}.
\begin{small}
\begin{verbatim}
#include <stdbool.h>
#include "cpdflibwrapper.h"
int main (int argc, char ** argv)
{
/* Initialise cpdf */
cpdf_startup(argv);
/* We will take the input hello.pdf and repeat it three times */
int mergepdf = cpdf_fromFile("hello.pdf", "");
/* Check the error state */
if (cpdf_lastError) return 1;
/* Clear the error state */
cpdf_clearError();
/* The array of PDFs to merge */
int pdfs[] = {mergepdf, mergepdf, mergepdf};
/* Merge them */
int merged = cpdf_mergeSimple(pdfs, 3);
if (cpdf_lastError) return 1;
cpdf_clearError();
/* Write output */
cpdf_toFile(merged, "merged.pdf", false, false);
if (cpdf_lastError) return 1;
return 0;
}
\end{verbatim}
\end{small}
\end{cpdflib}
\begin{pycpdflib}
\chapter*{Example Program in Python}
This program loads a file \texttt{hello.pdf} from disk and writes out a
document with the original included three times.
\begin{small}
\begin{verbatim}
#Merge example
import pycpdflib
#DLL loading depends on your own platform. These are the author's settings.
if sys.platform.startswith('darwin'):
pycpdflib.loadDLL("/Users/john/repos/python-libcpdf/libpycpdf.so")
elif sys.platform.startswith('linux'):
pycpdflib.loadDLL("../libpycpdf.so")
elif sys.platform.startswith('win32') or sys.platform.startswith('cygwin'):
os.add_dll_directory("C:\\\\OCaml64/home/JohnWhitington/python-libcpdf/")
pycpdflib.loadDLL("libpycpdf.dll")
#We will take the input hello.pdf and repeat it three times
mergepdf = pycpdf.fromFile('hello.pdf', '')
#The list of PDFs to merge
pdfs = [mergepdf, mergepdf, mergepdf]
#Merge them
merged = pycpdflib.mergeSimple(pdfs)
#Write output
pycpdflib.toFile(merged, 'merged.pdf', False, False)
\end{verbatim}
\end{small}
\end{pycpdflib}
\begin{dotnetcpdflib}
\chapter*{Example Program in C\#}
This program loads a file \texttt{hello.pdf} from disk and writes out a
document with the original included three times.
\begin{small}
\begin{verbatim}
//Merge example
using System;
using System.Collections.Generic;
using CoherentGraphics;
// Initialise cpdf
Cpdf.startup();
// We will take the input hello.pdf and repeat it three times
using (Cpdf.Pdf mergepdf = Cpdf.fromFile("hello.pdf", ""))
{
// The list of PDFs to merge
List<Cpdf.Pdf> pdfs = new List<Cpdf.Pdf> {mergepdf, mergepdf, mergepdf};
// Merge them
Cpdf.Pdf merged = Cpdf.mergeSimple(pdfs);
// Write output
Cpdf.toFile(merged, "merged.pdf", false, false);
// Dispose of merged PDF
merged.Dispose();
}
\end{verbatim}
\end{small}
\noindent Note the use of \texttt{using} and \texttt{Dispose()} to ensure the PDFs are thrown away when no longer required.
\chapter*{Example Program in VB.NET}
This program loads a file \texttt{hello.pdf} from disk and writes out a
document with the original included three times.
\begin{small}
\begin{verbatim}
' Merge example
imports System
imports System.Collections.Generic
imports CoherentGraphics
' Initialise cpdf
Cpdf.startup()
' We will take the input hello.pdf and repeat it three times
Using mergepdf As Cpdf.Pdf = Cpdf.fromFile("hello.pdf", "")
' The list of PDFs to merge
Dim pdfs As List(Of Cpdf.Pdf) =
new List(Of Cpdf.Pdf)({mergepdf, mergepdf, mergepdf})
' Merge them
Dim merged As Cpdf.Pdf = Cpdf.mergeSimple(pdfs)
' Write output
Cpdf.toFile(merged, "merged.pdf", false, false)
' Dispose of merged PDF
merged.Dispose()
End Using
\end{verbatim}
\end{small}
\noindent Note the use of \texttt{Using} and \texttt{Dispose()} to ensure the PDFs are thrown away when no longer required.
\end{dotnetcpdflib}
\begin{jcpdflib}
\chapter*{Example Program in Java}
This program loads a file \texttt{hello.pdf} from disk and writes out a
document with the original included three times.
\begin{small}
\begin{verbatim}
//Merge example
import com.coherentpdf.Jcpdf
public static void main(String[] args)
{
// Initialise cpdf
Jcpdf jcpdf = new Jcpdf();
try
{
jcpdf.startup();
}
catch (Jcpdf.CpdfError e)
{
System.out.println("Error during cpdf startup");
}
// We will take the input hello.pdf and repeat it three times
try (Jcpdf.Pdf mergepdf = jcpdf.fromFile("hello.pdf", ""))
{
// The array of PDFs to merge
Jcpdf.Pdf[] pdfs = {mergepdf, mergepdf, mergepdf};
// Merge them
Jcpdf.Pdf merged = jcpdf.mergeSimple(pdfs);
// Write output
jcpdf.toFile(merged, "merged.pdf", false, false);
// Dispose of merged PDF
merged.close();
}
catch (Jcpdf.CpdfError e)
{
System.out.println("Error during cpdf operation");
}
}
\end{verbatim}
\end{small}
\noindent Note the use of \texttt{try} and \texttt{close()} to ensure the PDFs are thrown away when no longer required.
\end{jcpdflib}
\begin{jscpdflib}
\chapter*{Example Program in JavaScript}
This program loads a file \texttt{hello.pdf} from disk and writes out a
document with the original included three times.
\begin{small}
\begin{verbatim}
//Merge example
//Load coherentpdf.js
const coherentpdf = require('./coherentpdf.js');
//Load the file hello.pdf from the current directory
var pdf = coherentpdf.fromFile('hello.pdf', '');
//Merge three copies of it
var merged = coherentpdf.mergeSimple([pdf, pdf, pdf]);
//Write to merged.pdf
coherentpdf.toFile(merged, 'merged.pdf', false, false);
//Clean up the two PDFs
coherentpdf.deletePdf(pdf);
coherentpdf.deletePdf(merged);
\end{verbatim}
\end{small}
\noindent To be run in node. A browser example is included in the distribution of coherentpdf.js.
\end{jscpdflib}
\pagestyle{plain}
\tableofcontents\clearpage\pagestyle{empty}
\cleardoublepage
\pagestyle{plain}
\chapter*{Typographical Conventions}
Command lines to be typed are shown in \texttt{typewriter\hspace{-1mm} font} in a box.
For example:
\begin{framed}
\noindent\small\verb!cpdf in.pdf -o out.pdf!
\end{framed}
\noindent When describing the general form of a command, rather than a particular
example, square brackets \verb|[]| are used to enclose optional parts, and
angled braces \verb!<>! to enclose general descriptions which may be
substituted for particular instances. For example,
\begin{framed}
\noindent\small\verb!cpdf <operation> in.pdf [<range>] -o out.pdf!
\end{framed}
\noindent describes a command line which requires an operation and, optionally,
a range. An exception is that we use \texttt{in.pdf} and \texttt{out.pdf}
instead of \texttt{<input file>} and \texttt{<output file>} to reduce
verbosity.
Under Microsoft Windows, type \texttt{cpdf.exe} instead of \texttt{cpdf}.
\clearpage\pagestyle{empty}\cleardoublepage
\mainmatter
%\chapterstyle{hangnum}
%\pagestyle{ruled}
\pagestyle{fancy}
\chapter{Basic Usage}\label{chap:1}
\label{basicusage}
\begin{framed}
\small
\noindent\begin{verbatim}
-help --help -version
-o -i -idir <directory>
-recrypt -decrypt-force -stdout
-stdin -stdin-user <password> -stdin-owner <password>
-producer <text> -creator <text> -change-id
-l -cpdflin <filename> -keep-l
-no-preserve-objstm -create-objstm -args <filename>
-args-json <filename> -utf8 -stripped
-raw -gs -gs-malformed
-gs-malformed-force -gs-quiet -error-on-malformed\end{verbatim}\end{framed}
The Coherent PDF tools provide a wide range of facilities for modifying PDF
files created by other means. There is a single command-line program
Cpdf (\texttt{cpdf.exe} under Microsoft Windows). The rest of this manual describes the options that may be given
to this program.
\section{Documentation}
The operation \texttt{-help / --help} prints each operation and option together with a short description. The operation \texttt{-version} prints the Cpdf version string.
\index{input files} \index{output files}
\section{Input and Output Files}
The typical pattern for usage is
\begin{framed}
\noindent\small\verb!cpdf [<operation>] <input file(s)> -o <output file>!
\end{framed}
\noindent and the simplest concrete example, assuming the existence of a file
\texttt{in.pdf} is:
\begin{framed}
\noindent\small\verb!cpdf in.pdf -o out.pdf!
\end{framed}
\noindent This copies \texttt{in.pdf} to \texttt{out.pdf}. Of course, we should like to do more interesting
things to the PDF file than that!
Files on the command line are distinguished from other input by their
containing a period. If an input file does not contain a period, it should be
preceded by \verb!-i!. For example:
\begin{framed}
\noindent\small\verb!cpdf -i in -o out.pdf!
\end{framed}
\noindent A whole directory of files may be added (where a command supports multiple files) by using the \verb!-idir! option:
\begin{framed}
\noindent\small\verb!cpdf -merge -idir myfiles -o out.pdf!
\end{framed}
\noindent The files in the directory \verb!myfiles! are considered in alphabetical order. They must all be PDF files. If the names of the files are numeric, leading zeroes will be required for the order to be correct (e.g \verb!001.pdf!, \verb!002.pdf! etc).
To restrict cpdf to files ending in \texttt{.pdf} (in upper or lower or mixed case) add the option \texttt{-idir-only-pdfs} \textit{before} \texttt{-idir}:
\begin{framed}
\noindent\small\verb!cpdf -merge -idir-only-pdfs -idir myfiles -o out.pdf!
\end{framed}
\section{Input Ranges}
An \index{input range} \index{range} \textit{input range} may be specified
after each input file. This is treated differently by each operation. For
instance
\begin{framed}
\noindent\small\verb!cpdf in.pdf 2-5 -o out.pdf!
\end{framed}
\noindent extracts pages two, three, four and five from \texttt{in.pdf},
writing the result to \texttt{out.pdf}, assuming that \texttt{in.pdf} contains
at least five pages.
\index{page!range}
\index{reversing}
Here are the rules for building input ranges:
\begin{itemize}
\item A number represents a page number
\item A page label may be used in place of a number e.g \texttt{[iii]} represents the first page found which is labelled \texttt{iii}.
\item A tilde (\texttt{\~{}}) defines a page number counting from the end of the document rather than the beginning. Page \texttt{\~{}1} is the last page, \texttt{\~{}2} the penultimate page etc.
\item A dash (\texttt{-}) defines ranges, e.g. \texttt{1-5} or \texttt{6-3}.
\item A comma (\texttt{,}) allows one to specify several ranges, e.g. \texttt{1-2,4-5}.
\item The word \texttt{end} represents the last page.
\item The words \texttt{odd} and \texttt{even} can be used in place of or at the end of a page range to restrict to just the odd or even pages.
\item The words \texttt{portrait} and \texttt{landscape} can be used in place of or at the end of a page range to restrict to just those pages which are portrait or landscape. Note that the meaning of ``portrait'' and ``landscape'' does not take account of any viewing rotation in place (use \texttt{-upright} from chapter 3 first, if required). A page with equal width and height is considered neither portrait nor landscape.
\item The word \texttt{reverse} is the same as \texttt{end-1}.
\item The word \texttt{all} is the same as \texttt{1-end}.
\item A range must contain no spaces.
\item Prepending \texttt{NOT} to a whole page range inverts it.
\item Prepending \verb!<n>!\texttt{DUP} to a whole page range duplicates each page of the range \verb!<n>! times.\index{page!duplicate}
\end{itemize}
\noindent For example:
\begin{framed}
\noindent\small\verb!cpdf in.pdf 1,2,7-end -o out.pdf!
\vspace{2.5mm}
\noindent Remove pages three, four, five and six from a document.
\vspace{2.5mm}
\noindent\verb!cpdf in.pdf 1-16odd -o out.pdf!
\vspace{2.5mm}
\noindent Extract the odd pages 1,3,...,13,15.
\vspace{2.5mm}
\noindent\verb!cpdf in.pdf landscape -rotate 90 -o out.pdf!
\vspace{2.5mm}
\noindent Rotate all landscape pages by ninety degrees.
\vspace{2.5mm}
\noindent\verb!cpdf in.pdf 1,all -o out.pdf!
\vspace{2.5mm}
\noindent Duplicate the front page of a document, perhaps as a fax cover sheet.
\vspace{2.5mm}
\noindent\verb!cpdf in.pdf ~3-~1 -o out.pdf!
\vspace{2.5mm}
\noindent Extract the last three pages of a document, in order.
\vspace{2.5mm}
\noindent\verb!cpdf in.pdf 2DUP1-10 -o out.pdf!
\vspace{2.5mm}
\noindent Produce the pages 1,1,2,2,....10,10.
\end{framed}
\index{decryption}
\section{Working with Encrypted Documents}
\index{owner password}
\index{user password}
\index{password}
In order to perform many operations, encrypted input PDF files must be
decrypted. Some require the owner password, some either the user or owner
passwords. Either password is supplied by writing \texttt{user=<password>} or
\texttt{owner=<password>} following each input file requiring it (before or
after any range). The document will \textit{not} be re-encrypted upon writing. For
example:
\begin{framed}
\noindent\small\verb!cpdf in.pdf user=charles -info!\\
\noindent\small\verb!cpdf in.pdf owner=fred reverse -o out.pdf!
\end{framed}
\noindent To re-encrypt the file with its existing encryption upon writing, which is required if only the user password was supplied, but allowed in any case, add the \texttt{-recrypt} option:
\begin{framed}
\noindent\small\verb!cpdf in.pdf user=charles reverse -recrypt -o out.pdf!
\end{framed}
\noindent The password required (owner or user) depends upon the operation
being performed. Separate facilities are provided to decrypt and encrypt files
(See Section \ref{crypt}).
When appropriate passwords are not available, the option \texttt{-decrypt-force} may be added to the command line to process the file regardless.
For decryption with AES256, passwords may be Unicode. However the password, should it contain non-ASCII characters, must be normalized by applying the SASLPrep profile (RFC 4013) of the stringprep algorithm (RFC 3454) using the Normalize and BiDi options. It must then be converted to UTF8 and truncated to 127 bytes. Cpdf does not perform this pre-processing -- it takes its passwords from the command line without processing.
\section{Standard Input and Standard Output}
\index{standard input} \index{standard output}
Thus far, we have assumed that the input PDF will be read from a file on
disk, and the output written similarly. Often it's useful to be able to read
input from \texttt{stdin} (Standard Input) or write output to \texttt{stdout}
(Standard Output) instead. The typical use is to join several programs
together into a \textit{pipe}, passing data from one to the next without the
use of intermediate files. Use \texttt{-stdin} to read from standard input, and
\texttt{-stdout} to write to standard input, either to pipe data between
multiple programs, or multiple invocations of the same program. For example, this sequence of commands (all typed on one line)
\begin{framed}
\noindent\small\begin{verbatim}
cpdf in.pdf reverse -stdout |
cpdf -stdin 1-5 -stdout |
cpdf -stdin reverse -o out.pdf\end{verbatim}
\end{framed}
\noindent extracts the last five pages of \texttt{in.pdf} in the correct order,
writing them to \texttt{out.pdf}. It does this by reversing the input, taking
the first five pages and then reversing the result.
To supply passwords for a file from \texttt{-stdin}, use \texttt{-stdin-owner <password>} and/or \texttt{-stdin-user <password>}.
Using \texttt{-stdout} on the final command in the pipeline to output the PDF
to screen is not recommended, since PDF files often contain compressed sections
which are not screen-readable.
Several Cpdf operations write to standard output by default (for
example, listing fonts). A useful feature of the command line (not specific to
Cpdf) is the ability to redirect this output to a file. This is
achieved with the \texttt{>} operator:
\begin{framed}
\noindent\small\verb!cpdf -info in.pdf > file.txt!
\vspace{2.5mm}
\noindent Use the \texttt{-info} operation (See Section \ref{info}), redirecting the
output to \texttt{file.txt}.
\end{framed}
\section{Doing Several Things at Once with AND}
\index{AND}
The keyword \texttt{AND} can be used to string together several commands in
one. The advantage compared with using pipes is that the file need not be
repeatedly parsed and written out, saving time.
To use \texttt{AND}, simply leave off the output specifier (e.g \texttt{-o}) of
one command, and the input specifier (e.g filename) of the next. For instance:
\begin{framed}
\noindent \small\verb!cpdf -merge in.pdf in2.pdf AND -add-text "Label"!\\
\noindent\small\verb! AND -merge in3.pdf -o out.pdf!
\vspace{2.5mm}
\noindent Merge \texttt{in.pdf} and \texttt{in2.pdf} together, add text to both pages, append \texttt{in3.pdf} and write to \texttt{out.pdf}.
\end{framed}
\noindent To specify the range for each section, use \texttt{-range}:
\begin{framed}
\noindent\small\verb!cpdf -merge in.pdf in2.pdf AND -range 2-4 -add-text "Label"!
\noindent\small\verb! AND -merge in3.pdf -o out.pdf!
\end{framed}
\section{Units}
\index{units}
When measurements are given to Cpdf, they are in points (1 point = 1/72 inch). They may optionally
be followed by some letters to change the measurement. The following are
supported:
\begin{center}
\begin{tabular}{rl}
\texttt{pt} & Points (72 points per inch). The default. \\
\texttt{cm} & Centimeters \\
\texttt{mm} & Millimeters \\
\texttt{in} & Inches \\
\end{tabular}
\end{center}
\noindent For example, one may write \texttt{14mm} or \texttt{21.6in}. In addition, the following letters stand for various page dimensions:
\begin{center}
\begin{tabular}{rl}
\texttt{PW} & Page width\\
\texttt{PH} & Page height\\
\texttt{PMINX} & Page minimum x coordinate\\
\texttt{PMINY} & Page minimum y coordinate\\
\texttt{PMAXX} & Page maximum x coordinate\\
\texttt{PMAXY} & Page maximum y coordinate\\
\texttt{CW} & Crop box width\\
\texttt{CH} & Crop box height\\
\texttt{CMINX} & Crop box minimum x coordinate\\
\texttt{CMINY} & Crop box minimum y coordinate\\
\texttt{CMAXX} & Crop box maximum x coordinate\\
\texttt{CMAXY} & Crop box maximum y coordinate\\
\texttt{AW} & Art box width\\
\texttt{AH} & Art box height\\
\texttt{AMINX} & Art box minimum x coordinate\\
\texttt{AMINY} & Art box minimum y coordinate\\
\texttt{AMAXX} & Art box maximum x coordinate\\
\texttt{AMAXY} & Art box maximum y coordinate\\
\texttt{TW} & Trim box width\\
\texttt{TH} & Trim box height\\
\texttt{TMINX} & Trim box minimum x coordinate\\
\texttt{TMINY} & Trim box minimum y coordinate\\
\texttt{TMAXX} & Trim box maximum x coordinate\\
\texttt{TMAXY} & Trim box maximum y coordinate\\
\texttt{BW} & Bleed box width\\
\texttt{BH} & Bleed box height\\
\texttt{BMINX} & Bleed box minimum x coordinate\\
\texttt{BMINY} & Bleed box minimum y coordinate\\
\texttt{BMAXX} & Bleed box maximum x coordinate\\
\texttt{BMAXY} & Bleed box maximum y coordinate\\
\end{tabular}
\end{center}
\noindent For example, we may write \texttt{PMINX}\ \texttt{PMINY} to stand for the coordinate of the lower left corner of the page.
Simple arithmetic may be performed using the words \texttt{add}, \texttt{sub}, \texttt{mul} and \texttt{div} to stand for addition, subtraction, multiplication and division. For example, one may write \texttt{14in\hspace{-1mm} sub\hspace{-1mm} 30pt} or \texttt{PMINX\hspace{-1mm} mul\hspace{-1mm} 2}
\section{Setting the Producer and Creator}
\index{producer}\index{creator}
The \texttt{-producer} and \texttt{-creator} options may be added to any Cpdf command line to set the producer and/or creator of the PDF file. If the file was converted from another format, the \textit{creator} is the program producing the original, the \textit{producer} the program converting it to PDF.
\begin{framed}
\noindent\small\verb!cpdf -merge in.pdf in2.pdf -producer MyMerger -o out.pdf!