# How to add a clickable TOC to a PDF file

Submitted by Nicola Rainiero on 2012-07-19 (last updated on 2015-05-12)

The idea is like to adding bookmark using LaTeX, but in this case I insert a new table of contents, the resulting page numbering and a direct anchor to TOC in every page. How? Using LaTeX of course!

Bookmarks are a valid tool, but contents perfectly integrated into a PDF is the best solution, above all you are reading using an eBook reader. With my Asus DR-900 reader, I have to do these operations in order to access in the bookmarks menu: push the Function Key (in the bottom right), click on the book title (in top left of the screen) and finally click on the Summary menu item... And wait the loading time and the scrolling until the point of my interest.

However with a clickable TOC I can view and jump directly in the point of my interest, without intermediate steps. If every page is linked to the TOC, I avoid these boring steps: push Function Key, hold down for three seconds the number of page (top right on the screen) and in the new appearing menu remember the correct page of contents.

This can be done using LaTeX, writing a few line of code. In this article I use the output PDF of a previous article: How to crop a PDF using LaTeX. That file after the cutting lost bookmarks and the TOC is unuseful on the screen.

 Input PDF Output PDF da_two_side.pdf (without hypertext) add_toc.pdf (with hypertext)

## Requirements

LaTeX with following packages:

• geometry, to define the exact size of the paper and to setup the position of page number from bottom;
• pdfpages, to include PDF pages in a LaTeX document;
• hyperref, to transform the output PDF in a hypertext;
• color, to enlarge the default number of colors (set to eight).

I suggest you to read the official documentation (always enclosed in all packages), so you can know features and incompatibilities. In this tutorial I will use the essential code in order to make it more usable and legible.

## Bookmarks and TOC

To enable bookmarks, you have to insert in the preface of the TEX file this text:

\usepackage[pagebackref]{hyperref}


To the TOC, the needed code will be inserted in the exact point where you want the TOC appears:

\tableofcontents
\label{contents}


Two specifications:

1. The “contents” label must be defined, because LaTeX doesn't know the exact position of the contents inside the document.
2. When you insert the PDF pages, you can omit the original contents.

## New page numbering and direct link to the TOC

The only solution is to create a new pagination style. The target is cover the existent page number and give it the anchor to the first page of the TOC. In the preamble you define the color of the box containing the page number and then the new style:

\usepackage{color}
\definecolor{Light}{gray}{.80}

\makeatletter
\def\[email protected]{%
\def\@oddfoot{\hfil\Huge{\colorbox{Light}{-\hyperref[contents]{\arabic{page}}-}}\hfil}%
\def\@evenfoot{\hfil\Huge{\colorbox{Light}{-\hyperref[contents]{\arabic{page}}-}}\hfil}%
}
\makeatother


Odd and even prefix describes the odd and even page, in my case I have to cover the bottom of the page, so I left empty the head suffix and I changed only the foot. In order to center the number of the page, I used twice \hfil descriptor: on the left and on the right. If I need to cover the page number on the left, I take off the \hfil on the left, vice versa for the right side.

The new style, called mystyle, should be inserted in the properties of \includepdf like this:

\includepdf[pages={%page range%},pagecommand=\thispagestyle{mystyle},addtotoc={
%
% all the pages you want to retrieve in the bookmarks and TOC
%
}]{PDF input file}


\documentclass{book}
\usepackage{pdfpages}
\author{Nicola Rainiero}
\title{Esempio di PDF con indice cliccabile}
\usepackage[pagebackref]{hyperref}

% Load this package and define these two new command to print the TOC into two columns
\usepackage{multicol}
\newcommand{\starttwocolumns}{\setlength{\columnsep}{10mm}\begin{multicols}{2}}
\newcommand{\stoptwocolumns}{\end{multicols}}

\usepackage{color}
\definecolor{Light}{gray}{.80}

\makeatletter
\def\[email protected]{%
\def\@oddfoot{\hfil\Huge{\colorbox{Light}{-\hyperref[contents]{\arabic{page}}-}}\hfil}%
\def\@evenfoot{\hfil\Huge{\colorbox{Light}{-\hyperref[contents]{\arabic{page}}-}}\hfil}%
}
\makeatother

% If you want to remove the automatic sectioning
%\setcounter{secnumdepth}{-2}

\begin{document}

1,chapter,1,{Copertina},a
}]{da_two_side.pdf}

\frontmatter
%3,chapter,1,{Indice},a
%}]{da_two_side.pdf}

% Classical TOC
\tableofcontents
\label{contents}

% TOC into two columns (remove the comments)
%\starttwocolumns
%\tableofcontents
%\label{Contents}
%\stoptwocolumns

\mainmatter
5,chapter,1,{Primo capitolo},a,
5,section,1,{Introduzione},a,
6,section,1,{II Paragrafo},a,
6,section,1,{III Paragrafo},a,
9,chapter,1,{Secondo capitolo},a,
9,section,1,{I Paragrafo},a,
9,section,1,{II Paragrafo},a
}]{da_two_side.pdf}

\end{document}


Notes: I suggest you to launch twice or more the pdflatex compilation, because the final PDF could have wrong references.

PDFs of this guide and the corresponding TEX file have been compressed here: Example_add_toc_pdf.zip

Coming soon to my site a bash script to convert the ouptut generated by JPDFBookmarks (an useful bookmarks editor) in a LaTeX code. So the tedious and annoying aspect, i.e. the manual choice of the pages that will appearing in the TOC and bookmarks, will become more simple and faster.

## Related Content:

### Nicola Rainiero

A civil geotechnical engineer with the ambition to facilitate own work with free software for a knowledge and collective sharing. Also, I deal with green energy and in particular shallow geothermal energy. I have always been involved in web design and 3D modelling.