Chapter 9. Working with Pdftk

Pdftk is a multiplatform command-line tool built on the iText library (which is described in iText for Java and C#). It has facilities for merging, splitting, and stamping documents, and for setting and reading metadata.

Command Line Syntax

Pdftk has a somewhat unusual command-line interface, where elements often have to appear in a particular order. We can split them into four groups, in the order they are specified:

  1. The input file or files, and possible input passwords.

  2. The operation and any arguments it requires.

  3. The output and any output passwords and permissions.

  4. Sundry output and other options.

The full details can be found in the manual for pdftk—in this chapter, we give only the subset needed for our examples.

Merging Documents

To merge documents, we use the cat operation. This is the default operation, so we don’t actually need to specify the cat keyword. For example, to merge the pages of three files into one, in order, we need:

pdftk file1.pdf file1.pdf file3.pdf output output.pdf

This writes a new file to output.pdf containing all the pages of file1.pdf, file2.pdf, and file3.pdf, in order. The output file may not be the same as any of the input ...

Get PDF Explained now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.