Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

By getting metadata from some pdf files with bookmarks by pdftk, I observe the format of bookmarks in pdf metadata. So I am guessing that with pdftk, it is possible to add and edit bookmarks of a pdf file.

Here are three steps that I follow:

  1. I firstly get the metadata (including bookmarks if any) from a pdf file into a text file by pdftk in.pdf dump_data > in.info.
  2. Next I add some bookmarks into the metadata text file in.info, changing it from

    InfoKey: Creator
    InfoValue: PScript5.dll Version 5.2
    InfoKey: Title
    InfoValue: SSReader Print.
    InfoKey: Producer
    InfoValue: Acrobat Distiller 7.0 (Windows)
    InfoKey: Author
    InfoValue: Administrator
    InfoKey: ModDate
    InfoValue: D:20050605073244+08'00'
    InfoKey: CreationDate
    InfoValue: D:20050605073244+08'00'
    PdfID0: 591a87c91dc76881fdf2ccf3811e72a5
    PdfID1: 6b6ab11de8824e438e4f5eb1d85ec72
    NumberOfPages: 400
    PageLabelNewIndex: 1
    PageLabelStart: 1
    PageLabelNumStyle: DecimalArabicNumerals
    

    to

    InfoKey: Creator
    InfoValue: PScript5.dll Version 5.2
    InfoKey: Title
    InfoValue: SSReader Print.
    InfoKey: Producer
    InfoValue: Acrobat Distiller 7.0 (Windows)
    InfoKey: Author
    InfoValue: Administrator
    InfoKey: ModDate
    InfoValue: D:20050605073244+08'00'
    InfoKey: CreationDate
    InfoValue: D:20050605073244+08'00'
    PdfID0: 591a87c91dc76881fdf2ccf3811e72a5
    PdfID1: 6b6ab11de8824e438e4f5eb1d85ec72
    NumberOfPages: 400
    BookmarkTitle: Front cover
    BookmarkLevel: 1
    BookmarkPageNumber: 1
    BookmarkTitle: About the Author
    BookmarkLevel: 1
    BookmarkPageNumber: 5
    BookmarkTitle: Title page
    BookmarkLevel: 1
    BookmarkPageNumber: 6
    BookmarkTitle: Copyright page
    BookmarkLevel: 1
    BookmarkPageNumber: 7
    BookmarkTitle: Foreword
    BookmarkLevel: 1
    PageLabelNewIndex: 1
    PageLabelStart: 1
    PageLabelNumStyle: DecimalArabicNumerals
    
  3. Then I try to put the edited metadata back to the pdf file by pdftk in.pdf update_info in.info output out.pdf.

But when I open the new pdf file out.pdf in evince or adobe reader, there is no bookmark in the new pdf file.

If I get the metadata from the new pdf file out.pdf by pdftk out.pdf dump_data > out.info, there will be no bookmarks in out.info. It looks like the bookmarks were not added successfully.

I was wondering if there are some mistakes? How shall I add and edit bookmarks of a pdf file, not necessarily by pdftk?

share|improve this question

5 Answers 5

up vote 4 down vote accepted

It looks to me like from version 1.45 (2012-12-06), pdftk does allow modification of bookmarks via the update_info command:

You can now add or change a PDF’s bookmarks using update_info.

via: http://www.pdflabs.com/docs/pdftk-version-history/

share|improve this answer
    
This worked very good for me. One should add that in the input file above the BookmarkBegin tags before each bookmark entry are missing. –  danielp Jul 21 at 20:01

If you still stick with those unix scripts, then

  1. extract bookmark data dumped from pdftk
  2. write one extra script to convert dumped bookmark data to pdfmarks format, which ghostscript command gs is accepted.
  3. use gs script to merge them together with pdfmarks

Take a look at http://blog.tremily.us/posts/PDF_bookmarks_with_Ghostscript/ the script pdf-merge.py do exactly what you (or I) want.

pdf-merge.py --output=merged.pdf input1.pdf input2.pdf

Some minor improvements could be done in his script

  1. unicode handling
  2. output bookmark files, so people can adjust it as well

Anyway, it should work

share|improve this answer
    
One upvote! Thanks! I am looking forward to your script! –  Tim Feb 7 '12 at 3:14
    
Seems the link is not working. Can you fix it please? –  fossilet Jul 22 '13 at 6:42
    
thanks, now the link is updated. –  larrycai Jul 30 '13 at 5:43

Caleb,

As far as I understand, bookmarks as per the PDF spec can't be injected via a diagnostic tool like pdftk. Updating the metadata to refer to chapters and bookmark landing anchors that don't exist will definitely not work, it may even make your PDF inconsistent or unopenable.

I ended up using java+iText libraries to do what you're suggesting as per this tutorial, but we're dealing with hundreds of pdfs daily, so it needed to be automated. If you're doing a one-off, Adobe Acrobat should be able to do this.

share|improve this answer

jPDFtweak (Java, so runs on Unix/Linux) can alter bookmarks, but I don't know if you can script anything with it.

For scripting, I'd guess your only native Unix/Linux option would be pdflatex with the pdfpages package. But that's a learning curve if you're not already a LaTeX user.

EDIT: Actually it may be possible with ghostscript: See here or here or here

share|improve this answer

To summarise all this good answers:

There is bmconverter project on GitHub that can convert various PDF bookmark formats. It can convert pdftk output to jpdftweak format, although if you will use jpdftweak then you wont need to convert pdftk output to csv, as you can do all work from within jpdftweak.

Unfortunately pdfmarks is not supported by the project, but fortunately someone posted script in bmconverter issues that can convert pdftk output to pdfmarks. So ghostscript batch option is an option

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.