4

I'm using pdfpages to combine multiple pdf documents; I would also like a front 'contents' page for the new file, using hyperref to allow me to jump directly to the correct page. The 'addtotoc' option in pdfpages almost works... the link produced by that goes to the correct page, but a few lines down from the start. Any suggestions? The sample code below also shows a workaround using \hyperlink - but this file will potentially have to be maintained/updated by other people who may not be so familiar with \LaTeX, so the more that can be automated (ie the less changes needed for each additional file), the better.

\documentclass{article}
\usepackage{pdfpages}
\usepackage[pdfstartview=FitH,bookmarks=true,colorlinks=true,linkcolor=blue]{hyperref}
\usepackage{bookmark}
\bookmarksetup{color=blue}
\setlength\parindent{0pt}

\begin{document}
\tableofcontents
\LARGE\hyperlink{Core1.pdf.1}{{Core 1}}\\
\LARGE\hyperlink{Core2.pdf.1}{{Core 2}}\\
\LARGE\hyperlink{Core3.pdf.1}{{Core 3}}\\
\LARGE\hyperlink{Core4.pdf.1}{{Core 4}}\
\includepdf[pages=-,link,linkfit=FitH,addtotoc={1, section, 1, Core 1, Core1}]{Core1.pdf}
\bookmark[dest=Core1.pdf.1]{Core 1}
\includepdf[pages=-,link,linkfit=FitH]{Core2.pdf}
\bookmark[dest=Core2.pdf.1]{Core 2}
\includepdf[pages=-,link,linkfit=FitH]{Core3.pdf}
\bookmark[dest=Core3.pdf.1]{Core 3}
\includepdf[pages=-,link,linkfit=FitH]{Core4.pdf}
\bookmark[dest=Core4.pdf.1]{Core 4}
\end{document}

2 Answers 2

4

The default use of addtotoc in \includepdf creates a section and uses that as the anchor. The section is defined to be anonymous within the document but to have a name in the table of contents and bookmarks.

One idea I had would be to use the addtotoc mechanism to just add the entries to the table of contents (and thus the bookmarks), but with the hyperlinks pointing to the already defined anchors instead of the section anchors, like so:

\documentclass{article}
\usepackage{mwe}
\usepackage{pdfpages}
\usepackage[pdfstartview=FitH,bookmarks=true,colorlinks=true,linkcolor=blue]{hyperref}
\usepackage{bookmark}
\bookmarksetup{color=blue}
\setlength\parindent{0pt}

\setcounter{errorcontextlines}{\maxdimen}

\makeatletter
\newcommand*{\@pdfpagephantomsection}[2][]{\edef\@currentHref{\AM@linkname.\AM@page}\addcontentsline{toc}{section}{\protect\numberline{}#1}}
\makeatother

\begin{document}
\tableofcontents

\includepdf[pages=-,link,linkfit=FitH,addtotoc={1, pdfpagephantomsection, 1, Core 1, example-image-a}]{example-image-a.pdf}
\includepdf[pages=-,link,linkfit=FitH,addtotoc={1, pdfpagephantomsection, 1, Core 2, example-image-b}]{example-image-b.pdf}

\end{document}

This works because pdfpages only creates a section for TOC purposes if the named sectioning command (when prefixed with @) does not exist, otherwise it uses that command instead. Here, I called it pdfpagesphantomsection.

Note that I did not have the PDF files you named, so I used those provided in the mwe package instead.

1
  • That works perfectly! Many thanks for a speedy response.
    – JustinT
    Dec 11, 2012 at 8:57
1

You can also simply change the top margin of the page that contains the included PDF. The anchor added by addtotoc will then be located at the very top of the page.

You can change the margins mid-document using the newgeometry and restoregeometry commands from the geometry package:

\documentclass{article}

\usepackage{geometry}
\usepackage{pdfpages}
\usepackage{hyperref}

\begin{document}

    % Your overview page goes here

    \newgeometry{top=0pt}  % Remove top margin
    \includepdf[addtotoc={1, section, 1, Core1, core1}]{example-image-a.pdf}
    \includepdf[addtotoc={1, section, 1, Core2, core2}]{example-image-b.pdf}
    \restoregeometry{}     % Restore original top margin
\end{document}

You must log in to answer this question.

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