drmariod drmariod - 3 months ago 20
R Question

Create bookmarks into a PDF file via command line

I am searching for a command line tool to add bookmarks to a PDF file.

What I have is a

page number
and a
label
. Would love to create bookmark called
label
linking to page
page number
.

Does any one know a command line tool (preferably OSX) for doing this?

I have about 4000 pages PDF files and about 150 bookmarks and would love to automate it.

My plan is to use a system call within a r-script.

EDIT

I create about 4000 single PDF files with graphs and I am using the OSX system command
/System/Library/Automator/Combine PDF Pages.action/Contents/Resources/join.py
to join the PDFs together. Previously I was using
pdfjoin
from
pdfjam
package, but this was way too slow. In the end, this is how I get my PDF where I add the bookmarks by hand with Adobe Acrobat Professional at the moment.

Answer

You can also use pdftk. It is also available for OS X.

I'm not going through all the details here and now, because it's been done elsewhere at great length already. Just briefly:

  1. Create a sample PDF from your original files (without bookmarks).
  2. Add some bookmarks with Adobe Acrobat (which you seem to have access to).
  3. Run one of these commands:

    pdftk my.pdf dump_data output -
    pdftk my.pdf dump_data output bookmarks+otherdata.txt
    
  4. Study the format of the output.

  5. Modify the output .txt file by adding all the entries you want.
  6. Run PDFTK again:

    pdftk my.pdf update_info bookmarks.txt output bookmarked.pdf
    

Additional Information

This is the Bookmark format I noticed after inspecting in Step 4 above.

BookmarkBegin
BookmarkTitle: -- Your Title 1 --
BookmarkLevel: 1
BookmarkPageNumber: 1
BookmarkBegin
BookmarkTitle: -- Your Title 2 --
BookmarkLevel: 1
BookmarkPageNumber: 2
BookmarkBegin
BookmarkTitle: -- Your Title 3 --
...
...
and so on...

And replace the above.. in the appropriate place.