位元詩人 技術雜談:Add Bookmarks with Coherent PDF Tools

Facebook Twitter LinkedIn LINE Skype EverNote GMail Yahoo Email

Coherent PDF Tools is a command line PDF tool for Windows, Mac and Unix. It is free for non-commercial use. I wanted to remove all bookmarks in the PDF file and re-edit them and Coherent PDF Tools can remove all bookmarks in single command. After some try, it simply worked. Here I introduce how to edit bookmarks with Coherent PDF Tools. This tutorial was tested on OS X 10.9.

Coherent PDF Tools is a single command line tool without external dependency, so just download the binary release and put it into execution path like /usr/local/bin.

Before editing bookmarks, dump out the bookmark file.


$ cpdf -list-bookmarks input.pdf > bookmarks.txt
{{< / highlight >}}

*bookmarks.txt* is a plain text file, the format looks like the following content:

```console
0 "Cover" 0
0 "Chapter 1" 10
1 "1.1 Introduction" 10
1 "1.2 Get Help" 12
0 "Chapter 2" 16
{{< / highlight >}}

Each single line presents one bookmark in PDF file.  The first part means the hierarchy of bookmarks.  The next bookmark should only be one layer deeper than the previous bookmark.  The second part is the bookmark text showed in a PDF file.  Text should be quoted with double quotation marks and no inner double quotation marks are allowed.  `cpdf` seems unable to escape double quotation marks.  The final part is the page of the bookmark.  Page number should be counted from the cover page and started from 1.

After editing *bookmarks.txt*, save back to the PDF file:

```console
$ cpdf -add-bookmarks bookmarks.txt input.pdf -o output.pdf
{{< / highlight >}}

If any error occurs, the step will not succeed.  Check *bookmarks.txt* for possible errors.

If you intend to delete all bookmarks on a PDF file, do this:

```console
$ cpdf -remove-bookmarks input.pdf -o output.pdf
{{< / highlight >}}

There are a lot thing can be done with Coherent PDF Tools, see their manual for the details.
關於作者

身為資訊領域碩士,位元詩人 (ByteBard) 認為開發應用程式的目的是為社會帶來價值。如果在這個過程中該軟體能成為永續經營的項目,那就是開發者和使用者雙贏的局面。

位元詩人喜歡用開源技術來解決各式各樣的問題,但必要時對專有技術也不排斥。閒暇之餘,位元詩人將所學寫成文章,放在這個網站上和大家分享。