Introduction

edit

This page explains different methods for adding a custom index to a PDF file. All the methods use FOSS.

Content

edit

Non-nested levels

edit

The image below shows a screenshot of Firefox when the resulting file in this section is opened.

 

Let's create a simple PDF file for our experimentation. The following command uses pandoc to create a simple PDF file named input.pdf that contains 4 pages.

$ pandoc -o input.pdf <<EOF
my page 1 \newpage
my page 2 \newpage
my page 3 \newpage
my page 4
EOF

Using gs

edit

We need to create a file that stores the information of our bookmarks. I decided to call that file my-bookmarks-for-gs.txt. The following code block shows the content of that file.

[/Page 1 /Title (My title 1) /OUT pdfmark
[/Page 2 /Title (My title 2) /OUT pdfmark
[/Page 3 /Title (My title 3) /OUT pdfmark
[/Page 4 /Title (My title 4) /OUT pdfmark

The gs command that we need to execute to insert the bookmarks in our PDF file is shown in the code block below.

$ gs -o output-by-gs.pdf -sDEVICE=pdfwrite input.pdf my-bookmarks-for-gs.txt

Using pdftk

edit
BookmarkBegin
BookmarkTitle: My title 1
BookmarkLevel: 1
BookmarkPageNumber: 1

BookmarkBegin
BookmarkTitle: My title 2
BookmarkLevel: 1
BookmarkPageNumber: 2

BookmarkBegin
BookmarkTitle: My title 3
BookmarkLevel: 1
BookmarkPageNumber: 3

BookmarkBegin
BookmarkTitle: My title 4
BookmarkLevel: 1
BookmarkPageNumber: 4
$ pdftk input.pdf update_info my-bookmarks-for-pdftk.txt output output-by-pdftk.pdf
{
  My title 1, 1
  My title 2, 2
  My title 3, 3
  My title 4, 4
}
$ ./booky/booky.sh input.pdf my-bookmarks-for-booky.txt

Nested levels

edit

The image below shows a screenshot of Firefox when the resulting file in this section is opened.

 

Let's create a simple PDF file for our experimentation. The following command uses pandoc to create a simple PDF file named input.pdf that contains 4 pages.

$ pandoc -o input.pdf <<EOF
my page 1 (should appear in bookmark 1) \newpage
my page 2 (should appear in bookmark 1.1) \newpage
my page 3 (should appear in bookmark 1.1.1) \newpage
my page 4 (should appear in bookmark 1.1.2)
EOF

Using gs

edit
[/Page 1 /Title (My title 1 foo) /Count 1 /OUT pdfmark
[/Page 2 /Title (My title 1.1 foo) /Count 2 /OUT pdfmark
[/Page 3 /Title (My title 1.1.1 foo) /OUT pdfmark
[/Page 4 /Title (My title 1.1.2 foo) /OUT pdfmark
$ gs -o output-by-gs.pdf -sDEVICE=pdfwrite input.pdf my-bookmarks-for-gs.txt

Using pdftk

edit
BookmarkBegin
BookmarkTitle: My title 1 foo
BookmarkLevel: 1
BookmarkPageNumber: 1

BookmarkBegin
BookmarkTitle: My title 1.1 foo
BookmarkLevel: 2
BookmarkPageNumber: 2

BookmarkBegin
BookmarkTitle: My title 1.1.1 foo
BookmarkLevel: 3
BookmarkPageNumber: 3

BookmarkBegin
BookmarkTitle: My title 1.1.2 foo
BookmarkLevel: 3
BookmarkPageNumber: 4
$ pdftk input.pdf update_info my-bookmarks-for-pdftk.txt output output-by-pdftk.pdf
{
  My title 1 foo, 1
  {
    My title 1.1 foo, 2
    {
      My title 1.1.1 foo, 3
      My title 1.1.2 foo, 4
    }
  }
}
$ ./booky/booky.sh input.pdf my-bookmarks-for-booky.txt

Nested levels (advanced example)

edit

The image below shows a screenshot of Firefox when the resulting file in this section is opened.

 

Let's create a simple PDF file for our experimentation. The following command uses pandoc to create a simple PDF file named input.pdf that contains 14 pages.

$ pandoc -o input.pdf <<EOF
my page 1 (should appear in bookmark 1) \newpage
my page 2 (should appear in bookmark 1.1) \newpage
my page 3 (should appear in bookmark 1.1.1) \newpage
my page 4 (should appear in bookmark 1.1.2) \newpage
my page 5 (should appear in bookmark 1.2) \newpage
my page 6 (should appear in bookmark 1.2.1) \newpage
my page 7 (should appear in bookmark 1.2.2) \newpage
my page 8 (should appear in bookmark 2) \newpage
my page 9 (should appear in bookmark 2.1) \newpage
my page 10 (should appear in bookmark 2.1.1) \newpage
my page 11 (should appear in bookmark 2.1.2) \newpage
my page 12 (should appear in bookmark 2.2) \newpage
my page 13 (should appear in bookmark 2.2.1) \newpage
my page 14 (should appear in bookmark 2.2.2)
EOF

Using gs

edit
[/Page 1 /Title (My title 1 foo) /Count 2 /OUT pdfmark
[/Page 2 /Title (My title 1.1 foo) /Count 2 /OUT pdfmark
[/Page 3 /Title (My title 1.1.1 foo) /OUT pdfmark
[/Page 4 /Title (My title 1.1.2 foo) /OUT pdfmark
[/Page 5 /Title (My title 1.2 foo) /Count 2 /OUT pdfmark
[/Page 6 /Title (My title 1.2.1 foo) /OUT pdfmark
[/Page 7 /Title (My title 1.2.2 foo) /OUT pdfmark
[/Page 8 /Title (My title 2 foo) /Count 2 /OUT pdfmark
[/Page 9 /Title (My title 2.1 foo) /Count 2 /OUT pdfmark
[/Page 10 /Title (My title 2.1.1 foo) /OUT pdfmark
[/Page 11 /Title (My title 2.1.2 foo) /OUT pdfmark
[/Page 12 /Title (My title 2.2 foo) /Count 2 /OUT pdfmark
[/Page 13 /Title (My title 2.2.1 foo) /OUT pdfmark
[/Page 14 /Title (My title 2.2.2 foo) /OUT pdfmark
$ gs -o output-by-gs.pdf -sDEVICE=pdfwrite input.pdf my-bookmarks-for-gs.txt

Using pdftk

edit
BookmarkBegin
BookmarkTitle: My title 1 foo
BookmarkLevel: 1
BookmarkPageNumber: 1

BookmarkBegin
BookmarkTitle: My title 1.1 foo
BookmarkLevel: 2
BookmarkPageNumber: 2

BookmarkBegin
BookmarkTitle: My title 1.1.1 foo
BookmarkLevel: 3
BookmarkPageNumber: 3

BookmarkBegin
BookmarkTitle: My title 1.1.2 foo
BookmarkLevel: 3
BookmarkPageNumber: 4

BookmarkBegin
BookmarkTitle: My title 1.2 foo
BookmarkLevel: 2
BookmarkPageNumber: 5

BookmarkBegin
BookmarkTitle: My title 1.2.1 foo
BookmarkLevel: 3
BookmarkPageNumber: 6

BookmarkBegin
BookmarkTitle: My title 1.2.2 foo
BookmarkLevel: 3
BookmarkPageNumber: 7

BookmarkBegin
BookmarkTitle: My title 2 foo
BookmarkLevel: 1
BookmarkPageNumber: 8

BookmarkBegin
BookmarkTitle: My title 2.1 foo
BookmarkLevel: 2
BookmarkPageNumber: 9

BookmarkBegin
BookmarkTitle: My title 2.1.1 foo
BookmarkLevel: 3
BookmarkPageNumber: 10

BookmarkBegin
BookmarkTitle: My title 2.1.2 foo
BookmarkLevel: 3
BookmarkPageNumber: 11

BookmarkBegin
BookmarkTitle: My title 2.2 foo
BookmarkLevel: 2
BookmarkPageNumber: 12

BookmarkBegin
BookmarkTitle: My title 2.2.1 foo
BookmarkLevel: 3
BookmarkPageNumber: 13

BookmarkBegin
BookmarkTitle: My title 2.2.2 foo
BookmarkLevel: 3
BookmarkPageNumber: 14
$ pdftk input.pdf update_info my-bookmarks-for-pdftk.txt output output-by-pdftk.pdf
{
  My title 1 foo, 1
  {
    My title 1.1 foo, 2
    {
      My title 1.1.1 foo, 3
      My title 1.1.2 foo, 4
    }
    My title 1.2 foo, 5
    {
      My title 1.2.1 foo, 6
      My title 1.2.2 foo, 7
    }
  }
  My title 2 foo, 8
  {
    My title 2.1 foo, 9
    {
      My title 2.1.1 foo, 10
      My title 2.1.2 foo, 11
    }
    My title 2.2 foo, 12
    {
      My title 2.2.1 foo, 13
      My title 2.2.2 foo, 14
    }
  }
}
$ ./booky/booky.sh input.pdf my-bookmarks-for-booky.txt