3

I currently have a PDF file without any ToC (for example, in Mac's Preview.app, I can't see the ToC in the sidebar).

But I have the TOC in XML format, where there is a title and a pagenumber where that section starts.

Is there any way I can add that TOC to my PDF file in a batch way?

Since I have the TOC in XML, I can basically parse it in any possible way, so if there were a command line to add an TOC item to a PDF, I could also do that.

Any ideas?

1
  • What is your PDF editor - Adobe Acrobat Pro? Windows or Linux or Mac?
    – harrymc
    Nov 11, 2013 at 8:20

2 Answers 2

4
+50

It is fairly simple to add bookmarks to a PDF using Ghostscript. The syntax is:

gs -dBATCH -dNOPAUSE -sDEVICE=pdfwrite -sOutputFile=out.pdf in.pdf pdfmarks

Where pdfmarks is a text file with contents like:

[/Title (Title Page) /Page 1 /OUT pdfmark
[/Title (Table of Contents) /Page 3 /OUT pdfmark
...

For nested levels, use the /Count attribute. For example:

[/Count 3 /Title (Chapter 1) /Page 1 /OUT pdfmark
[/Count -2 /Title (Section 1.1) /Page 2 /OUT pdfmark
[/Title (Section 1.1.1) /Page 3 /OUT pdfmark
[/Title (Section 1.1.2) /Page 4 /OUT pdfmark
[/Count -1 /Title (Section 1.2) /Page 5 /OUT pdfmark
[/Title (Section 1.2.1) /Page 6 /OUT pdfmark
[/Title (Section 1.3) /Page 7 /OUT pdfmark

The argument to /Count gives the number of immediately subordinate bookmarks. The sign of the argument sets the default display (negative for closed, positive for open).

If the bookmarks are not enough as a Table of Contents, there exist applications that create a Table of Contents from bookmarks. I haven't used any such, but googling turned up for example Mapsoft TOCBuilder which is a commercial Adobe Acrobat plug-in with trial.

Sources :
PDF bookmarks with Ghostscript
How to generate bookmarks via ghostscript/pdfwrite/pdfmark

2
  • That works perfectly under Ubuntu! Thanks a lot :D Although it takes some time on 1200 pages (running Ubuntu in a VM). But still quite a lot easier than manually adding 130 bookmarks :-D
    – Trolley
    Nov 12, 2013 at 19:59
  • Nice! However, unicode characters, such as ’ (RIGHT SINGLE QUOTATION MARK) display weirdly. Any idea why?
    – rien333
    Sep 6, 2019 at 21:03
0

If you have just one file, here's a way you can do so using pdfTeX. It may be slightly convoluted but I know TeX (and don't know other tools) and it does the trick when I need it.

Here's a sample file which will copy the full contents of your input.pdf and adds a Table of Contents containing links to some page numbers.

% Set page size... this is A4, change to whatever you need
\pdfpagewidth=210mm
\pdfpageheight=297mm

% TeX always adds unwanted 1in left and top margins, this counteracts them.
\advance\hoffset by -1in
\advance\voffset by -1in

% This macro inserts a verbatim copy of one page from the source into the output.
\def\copypage#1{%
  \pdfximage page #1 {input.pdf}%
  \shipout\vbox{\pdfrefximage\pdflastximage}}

% Determine the total number of pages
\pdfximage{input.pdf}
\newcount\total
\total=\pdflastximagepages

% Define an iterator to copy the whole file
\def\copy{%
  \copypage{\the\pageno} % Copy the page given by current page number
  \advance\pageno by 1 % Increase page number by one
  \ifnum\pageno > \total
    \let\copy\relax % After the last page has been copied, stop
  \fi
  \copy} % This repeats the cycle until \copy is redefined to \relax at the very end

\copy % Run the cycle

% Here you add your outline
\pdfoutline goto page 1 {/Fit} count 2 {Chapter}
  \pdfoutline goto page 1 {/Fit} count -3 {Section}
    \pdfoutline goto page 1 {/Fit} {Item}
    \pdfoutline goto page 2 {/Fit} {Item}
    \pdfoutline goto page 3 {/Fit} {Item}
  \pdfoutline goto page 4 {/Fit} count 1 {Section}
    \pdfoutline goto page 4 {/Fit} count -2 {Subection}
      \pdfoutline goto page 1 {/Fit} {Item}
      \pdfoutline goto page 5 {/Fit} {Item}

\end

How to use the \pdfoutline command:

  • Always supply a page number, even for section headers which might not be supposed to be used directly
  • Replace "Chapter", "Item" etc. by your desired headers
  • Supply a count for items which contain subitems (note that there's no "closing" command so TeX needs to know the count in advance)
  • EDIT thanks to harrymc: Make the count negative if you want a item with subitems closed by default, leave it positive if you want it open

After preparing this input file, save it as, say, output.tex, and process it using pdftex. Voilà, this produces output.pdf with your shiny new ToC.

4
  • Can I add titles to the ToC with the pdfoutline command?
    – Trolley
    Nov 11, 2013 at 9:51
  • And I can't seem to figure out how to install and run PDFTex on either Windows and Mac? Help appreciated! :-)
    – Trolley
    Nov 11, 2013 at 9:59
  • @Elias Sorry, I forgot to mention. The titles to be displayed in your table are the last item between the {...} in each \pdfoutline line. I just chose "Chapter", "Item" etc. arbitrarily for my example to illustrate the meaning of the respective entries. Also, pdftex is a part of any TeX installation, for example, MikTeX for Windows. But if you have never worked with TeX I'm sure there would be better solutions for you.
    – The Vee
    Nov 11, 2013 at 11:12
  • I haven't used TeX before, so I had hoped there would be a more simple way. I have tried to do it with bookmarks in Acrobat Pro, but there are about 130 bookmarks, so it would take a lot of time.
    – Trolley
    Nov 11, 2013 at 12:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .