[Tex/LaTex] pdfpages/hyperref – Contents link jumps to wrong part of page

hyperrefpdfpdfpagestable of contents

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}

Best Answer

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.

Related Question