位元詩人 技術雜談: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) 是資訊領域碩士,喜歡用開源技術來解決各式各樣的問題。這類技術跨平台、重用性高、技術生命長。

除了開源技術以外,位元詩人喜歡日本料理和黑咖啡,會一些日文,有時會自助旅行。