Chapter 6

cpdf -list-bookmarks [-utf8 | -raw] in.pdf

cpdf -remove-bookmarks in.pdf -o out.pdf

cpdf -add-bookmarks <bookmark file> in.pdf -o out.pdf

cpdf -bookmarks-open-to-level <n> in.pdf -o out.pdf

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. Then the destination (see below). For example, upon executing

cpdf -list-bookmarks doc.pdf

the result might be:

0 "Part 1" 1 open  
1 "Part 1A" 2 "[2 /XYZ 200 400 null]"  
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. See Section 1.17 for more information. A newline in a bookmark is represented as "\n".

6.1.1 Destinations

The destination is an extended description of where the bookmark should point to (i.e it can be more detailed than just giving the page). For example, it may point to a section heading halfway down a page. Here are the possibilities:

-F[por/mXaYtZ-lefttopzoom]-------DD-eisscprliapytiopnage n-umber-p------------------------
                          at u pper-left of w indow and magn ification of zoom.
                          W riting“null” for anyofleft,topor zoom specifiesn o
                          change. A zoom of0isthesam eas“nu ll”.
 [p /Fit]                   D isplay p age num ber p so as to fit fully within the
                          w in dow.
 [p /FitH top]              D isplay page num ber p with vertical coord inate to
                          at the top of th e w ind ow and the page m agnifi ed
                          so its width fits the window . A null value for top
                          im pliesno ch an ge.
 [p /FitV left]               D isplay p age num ber p w ith ho rizontal coordinat
                          leftattheleftofthew ind ow,and the pagem agnifi ed
                          so its h eight fi ts the w indo w. A null valu e for left
                          im pliesno ch an ge.
 [p /FitR leftbottom righttop]  D isplay page n umber p magnified so &#x
                          tirelyw ithintherectanglespecifiedby theotherpa-
                          ram eters.
 [p /FitB]                  A s for /F it bu t with the page’s bounding box (see
 [p /FitBH top]             A sfor/FitH bu twith thepage’sbound ing box(see
 [p /FitBV left]             A s for /FitV but with the page’s bound ing box (se

The bounding box is the intersection of the page’s crop box and the bounding box of the page contents. Some other kinds of destination may be produced by -list-bookmarks. They will be preserved by -add-bookmarks and may be edited as your risk.

6.2 Remove Bookmarks

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

cpdf -remove-bookmarks in.pdf -o out.pdf

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 operation. 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

cpdf -add-bookmarks bookmarks.txt in.pdf -o out.pdf

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).

6.4 Opening bookmarks

As an alternative to extracting a bookmark file and manipulating the open-status of bookmarks, mass manipulation may be achieved by the following operation:

cpdf -bookmarks-open-to-level <level> in.pdf -o out.pdf

A level of 0 will close all bookmarks, level 1 will open just the top level, closing all others etc. To open all of them, pick a sufficiently large level.

Python Interface

# CHAPTER 6. Bookmarks 
def getBookmarks(pdf): 
    """Get the bookmarks for a PDF as a list of tuples of the form: 
    (level : int, page : int, text : string, openstatus : bool)""" 
def setBookmarks(pdf, marks): 
    """Set the bookmarks for a PDF as a list of tuples of the form: 
    (level : int, page : int, text : string, openstatus : bool)"""