Chapter 6

PDF Bookmarks (properly called the document outline) represent a tree of references to parts of the file, typically displayed at the side of the screen. The user can click on one to move to the specified place. cpdf provides facilities to list, add, and remove bookmarks. The format used by the list and add operations is the same, so you can feed the output of one into the other, for instance to copy bookmarks.

6.1 List Bookmarks

The -list-bookmarks operation prints (to standard output) the bookmarks in a file. The first column gives the level of the tree at which a particular bookmark is. Then the text of the bookmark in quotes, then the page number which the bookmark points to, then (optionally) the word ”open” if the bookmark should have its children (at the level immediately below) visible when the file is loaded. For example, upon executing

the result might be:

0 "Part 1" 1 open  
1 "Part 1A" 2  
1 "Part 1B" 3  
0 "Part 2" 4  
1 "Part 2a" 5

If the page number is 0, it indicates that clicking on that entry doesn’t move to a page.

By default, cpdf converts unicode to ASCII text, dropping characters outside the ASCII range. To prevent this, and return unicode UTF8 output, add the -utf8 option to the command. To prevent any processing, use the -raw option.

6.2 Remove Bookmarks

The -remove-bookmarks operations removes all bookmarks from the file.

6.3 Add Bookmarks

The -add-bookmarks file adds bookmarks as specified by a bookmarks file, a text file in ASCII or UTF8 encoding and in the same format as that produced by the -list-bookmarks option. If there are any bookmarks in the input PDF already, they are discarded. For example, if the file bookmarks.txt contains the output from -list-bookmarks above, then the command

adds the bookmarks to the input file, writing to out.pdf. An error will be given if the bookmarks file is not in the correct form (in particular, the numbers in the first column which specify the level must form a proper tree with no entry being more than one greater than the last).