Month: April 2017

Applies To: PDF Manipulation

Have you ever needed to add those fancy navigational elements to a PDF document? Some people (incorrectly) call these the table of contents, most interfaces call them Bookmarks and internally they’re collectively known as the outline. I’m talking about these things:

But what if you’re generating PDFs programatically? For instance, you are converting a bunch of image files to a PDF using something like ImageMagick(or even GraphicsMagick). Or maybe your fancy software doesn’t add them and you need a way to get them added. Fortunately, you can add them through the somewhat obscure pdfmark interface! WOWEE!

There are several paid tools out there to do this and there are even some free ones that do an okay job. For instance, jpdfbookmarks is free and does an alright job (the bookmarks are there, but other PDF processors will likely mark the PDF as invalid and “repair” these by removing them). However, these often introduce an extra interface or simplified format that may not fully support everything you can do with bookmarks (let alone the rest of pdfmark). There is actually a ton you can do (even bookmarks can do far more than just navigate to another page) and the syntax isn’t that complicated.

Anatomy of a PDF Bookmark

Generally, a basic bookmark simply jumps to a specific page. Here’s a very basic example of a bookmark that will open page 2 of a PDF document (pages start at 1) with a child bookmark that will open page 3:

The whitespace (tabs and spaces) makes no difference, but I find it easier to indent child bookmarks. Here’s what’s happening:

[ represents the start of a new pdfmark “command” (you can have multiple commands in a single file)

/Title defines the text of the bookmark (everything in the parenthesis)

/Count indicates the number of child bookmarks

When the number is positive the bookmark is expanded (it’s children showing by default)

When the number is negative the bookmark is collapsed (it’s children hidden by default)

If there are no children, leave it out

Child bookmarks should follow their parent. When the children are done, the next bookmark belongs to the parent level

/Page defines which page number (starting with 1) to navigate to (this is not required since bookmarks can do other actions as well – see below)

/View defines the zoom level the destination page will have (see below for details)

/OUT pdfmark defines the “command” as a bookmark (/OUT) and is the end of the “command”

View Magnification

The zoom level of the destination page is set using the /View option. There are a ton of options here (see page 11 of the Cooking up Enhanced PDF with pdfmark Recipes eBook by Lynn Mead for more examples). But here are the most common values:

[/Fit] – Fits the page to the window (the whole page is visible)

[/FitH top] – Fits the width of the page to the window, replace top with a number

The top value is the distance from the page origin to the top of the window (offset) e.g. [/FitH 32]

[/FitH -32768] – Fits the width of the page to the window (top value is automatic)

[/FitV left] – Fits the height of the page to the window, replace left with a number

The left value is the distance in from the page origin to the left edge of the window (offset) e.g. [/FitV -17]

[/XYZ left top zoom] – Gives a specific origin offset and zoom level, replace left, top, and zoom with either a number or the word null e.g. [/XYZ 3 5 10] or [/XYZ null null 0]

The left value is the distance in from the page origin to the left edge of the window (offset)

The top value is the distance from the page origin to the top of the window (offset)

The zoom level is the magnification (0-100)

To simply keep whatever zoom level the user is using just use [/XYZ null null 0].

Other Options

There are additional parameters for bookmarks that can be added as necessary: