# Add TOC to PDF from XML/JSON/file?

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?

• What is your PDF editor - Adobe Acrobat Pro? Windows or Linux or Mac? Nov 11 '13 at 8:20

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
...


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.

• 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 Nov 12 '13 at 19:59
• Nice! However, unicode characters, such as ’ (RIGHT SINGLE QUOTATION MARK) display weirdly. Any idea why? Sep 6 '19 at 21:03

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.

% 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

\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
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.
• @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. Nov 11 '13 at 11:12