Modify PDF PageLayout and PageMode using Pdftk and Sed

Thursday, April 07 2005 @ 01:04 PM PDT

Contributed by: Admin

PDF has features for controlling how a document first appears in the viewer. These include page layout settings: Single Page, Continuous, and Continuous Facing Pages. These also include page mode settings: Show Bookmarks, Show Thumbnails, and Full Screen.

Pdftk does not currently have built-in features for setting these options, but you will find they are easy to set using a sed script. My article about editing PDF using sed will give you the background on this technique. Here I will desciribe how it applies to page layout and page mode settings.

1, 2, 3

First, normalize your PDF's code by passing it through pdftk:

  pdftk mydoc.pdf output mydoc.1.pdf

Next, edit it using this sed script. This example uses the -c option required when using MSYS sed on Windows. Windows users beware: use the sed that comes with MSYS inside MSYS's bash shell. Also note that the line break after a is necessary.

  sed -c '/^/PageMode/d;/^/Type /Catalog/a
/PageMode /UseOutlines' mydoc.1.pdf > mydoc.2.pdf

Finally, pass the results through pdftk to fix the PDF's cross reference table:

  pdftk mydoc.2.pdf output mydoc.done.pdf

Open in Acrobat/Reader to test.

So, what's going on, here?

The /^/PageMode/d part of the sed script deletes the current /PageMode setting. The /^/Type /Catalog/a part of the sed script appends the following text into the PDF's Catalog. The /PageMode /UseOutlines is what gets inserted into the PDF. It is the PDF code for displaying the bookmarks when the PDF opens. Here are some more options:

/PageMode Options

Another setting that lends itself to this kind of manipulation is the PDF's /PageLayout:

/PageLayout Options

More Options: the /ViewerPreferences PDF dictionary

For further control over a PDF's appearance and behavior, investigate the /ViewerPreferences dictionary documented in the PDF Reference.

When playing with PDF this way, it helps to also have a binary-safe text editor for viewing/editing the PDF. On Windows, I use NT Emacs (not X Emacs) or gVim for this purpose. Also, acquaint yourself with the PDF Reference, which is freely available here.

1 comments



http://www.accesspdf.com/article.php/20050407130414434