[Tex/LaTex] One page table of contents with hyperlinks for use with pdftk

hyperrefpdftable of contents

I am using python and pdftk to create a portfolio from many pdf files. About half the files are generate by LaTeX and the other half are scanned in documents. When I am done, I want to create a table of contents with hyperlinks to specific pages in the final, assembled document. I have a table of contents that I have generated with correct page numbers for each document. It looks nice (in my opinion), but the hyperlinks don't work. I believe the problem is that the toc tex file is only one page, so links to other pages don't seem to work. Here is my code for the toc file:

\input{toc_header}
\begin{document}
\begin{center}
\begin{tabularx}{0.95\textwidth}{Xr}
{\large \textbf{Table of Contents}} & page \\
Table of Contents & \hyperlink{page.2}{2} \rule{0pt}{1.2EM} \\
\hline
CV & \hyperlink{page.3}{3} \rule{0pt}{1.2EM} \\
\hline
Support Letter from Nominator & \hyperlink{page.7}{7} \rule{0pt}{1.2EM} \\
\hline
Section 1: Philosophy of Teaching & \hyperlink{page.10}{10} \rule{0pt}{1.2EM} \\
\hline
Section 2: Implementation and Outcomes of the Teaching Philosophy & \hyperlink{page.12}{12} \rule{0pt}{1.2EM} \\
\hline
\hspace{0.25in} 2. A: Evidence of Acting on Teaching Evaluations & \hyperlink{page.13}{13}    \rule{0pt}{1.2EM} \\
\hline
\hspace{0.25in} 2. B: Examples of How My Teaching Philosophy Has Been Implemented &    \hyperlink{page.26}{26} \rule{0pt}{1.2EM} \\
\hline
\hspace{0.25in} 2. C: Evidence of the Effectiveness of My Teaching & \hyperlink{page.45}{45} \rule{0pt}{1.2EM} \\
\hline
 \hspace{0.25in} 2. D: Syllabi for the Past Three Semesters & \hyperlink{page.58}{58} \rule{0pt}{1.2EM} \\
%many more entries
\end{tabularx}
\end{center}
\end{document}

Is there any way to create a single page document like this with links to pages that technically don't exist, so that when I merge it with other pdfs using pdftk, the hyperlinks work?

Best Answer

pdfpages can be used to embed PDFs in a document and to add entries to the ToC.

Assuming you have a file kant.pdf, which you want to embed in your main file, you could do so by calling includepdf with the following parameters:

\includepdf[%
    pages=-,
    addtotoc={% You can add more than one entry to the toc
            1,                       % page number 
            section,                 % latex section name
            1,                       % level: 1 for section, 2 for subsection...
            Important paper on Kant, % title text for toc
            pdf:kant                 % label
        }
    ]{kant.pdf}  

Here is a minimal kant paper:

% filename: kant.tex
\documentclass[a4paper]{article}
\usepackage{kantlipsum}
\usepackage{hyperref}

\title{Important paper on Kant}

\begin{document}
    \maketitle
    \tableofcontents
    \section{Practical reason}
        \kant
    \section{Impractical reason}
        \kant
\end{document}

And a main file, which includes kant.pdfand sets three bookmarks (to the title and the two sections):

% filename: main.tex
\documentclass[a4paper]{article}
\usepackage{pdfpages}
\usepackage{hyperref}

\begin{document}
    \tableofcontents
    \includepdf[%
        pages=-,
        addtotoc={%           You can add more than one entry to the toc
            1,              % page number 
            section,        % latex section name
            1,              % level: 1 for section, 2 for subsection...
            Important paper on Kant,           % title text for toc
            pdf:kant,      % label
            1, subsection, 2, Practical reason, pdf:kant:sec1,
            3, subsection, 2, Impactical reason, pdf:kant:sec2
        }
    ]{kant.pdf}
\end{document}

If you prefer keeping your existing table, then don't use \tableofcontents and use \pageref instead of \hyperlink:

% filename: main2.tex
\documentclass[a4paper]{article}
\usepackage{pdfpages}
\usepackage{hyperref,bookmark}

\begin{document}

The paper on kant is on page \pageref{pdf:kant}.
\clearpage

    \includepdf[%
        pages=-,
        addtotoc={%           You can add more than one entry to the toc
            1,              % page number 
            section,        % latex section name
            1,              % level: 1 for section, 2 for subsection...
            Important paper on Kant,           % title text for toc
            pdf:kant,      % label
            1, subsection, 2, Practical reason, pdf:kant:sec1,
            3, subsection, 2, Impactical reason, pdf:kant:sec2
        }
    ]{kant.pdf}
\end{document}

Caveat: Links (and annotations) in the included pdf files are lost.

If you want consistent page numbers, headers and/or footers, you can define a pagestyle and use it with the option pagecommand (the default is pagecommand={\thispagestyle{empty}}).

Related Question