5

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?

3
  • 2
    It maybe simpler to merge the PDFs using pdfpages. That way a ToC with working hyperlinks could be generated automatically. – DG' Nov 30 '13 at 16:55
  • I started trying to use pdfpages and the TOC was easy and automatic, but I can't seem to get working bookmarks. I currently have working bookmarks. – ryanGT Nov 30 '13 at 17:21
  • I don't know why bookmarks aren't working with pdfpages. I have this in my call to use hyperref: \usepackage[pdfauthor={XX}, pdftitle={TT}, bookmarks=true, pdftex]{hyperref} and then I include the documents like this: \includepdf[pages=-,linktodoc=false]{cover_page-page001.pdf} – ryanGT Nov 30 '13 at 17:25
4

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

1
  • Thanks DG'. pdfpages really is the right way to go. With the addtotoc option, I am getting exactly what I want. My only regret is that this means I kind of wasted my time with my python + pdftk approach that is basically a hack to do what pdfpages does. – ryanGT Dec 1 '13 at 5:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.