# Can bookmarks be indexed pointing to particular pages inserted using \includepdf?

This question was asked previously (Is it possible to index non-latex PDF inserted using \includepdf). It attracted no answers.

Bookmarks can be made using \bookmark that are directed to particular pages compiled using the \includepdf command from the pdfpages package (e.g. \pdfbookmark to page number and location with \includepdf.

We also know it is possible to get the correct page number of pages inserted using \includepdf. This is demonstrated using the option addtolist of \includepdf, which can be used to build a list of items in a list of tables or list of figures format.

Therefore, is it possible to attach index entries to the same page numbers used by the addtolist feature?

My MWE does the following: A simple 2-page pdf file named test.pdf is made using \lipsum and an image.

The second part of the MWE uses \includepdf twice to join two copies of test.pdf. This gives a file with 4 pages. Production of the table of contents adds a page at the front of the document. Using the addtolist option, a list of bookmarks is created using @starttoc{lot}. These bookmarks point to page 1 and 2 of the first test.pdf file and then page 1 and 2 of the second test.pdf file. The bookmarks are created using a macro that uses \bookmark and which was posted on TeX.SE here (\pdfbookmark to page number and location with \includepdf). I modified the macro by adding \index{#5} where #5 is a label for the bookmarked page (I wasn't really expecting it to work). Using \makeidx, an index is created on page 6 of the document.

However, the page numbers in the index are not correct. The index entries attached to the bookmarks for the first copy of test.pdf refer to just the first page of the file (page 1), while the index entries associated with the bookmarks in the second file refer to page 3. At least the indexing recognizes the physical page numbers, but bookmarks 2 and 4 are associated with pages 2 and 4, whereas the index reports pages 1 and 3. This contrasts with the association with page numbers when using the addtolist feature, which does produce table entries with the correct page numbers.

I would prefer to have an index to the pages rather than use the list of tables feature. This is because in an index we can have nested itemization and index items can point to multiple pages.

This creates test.pdf for the purpose of testing.

\documentclass[10pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage{lipsum}
\usepackage{graphicx}
\usepackage[left=3.00cm, right=3.00cm, top=3.00cm, bottom=3.00cm]{geometry}
\begin{document}
\pagestyle{empty}
\lipsum[1-10]

\includegraphics{example-image-a}

\lipsum[2]
\end{document}


This MWE joins 2 test.pdf files producing a list of bookmarked pages (with correct page numbers) and an index with incorrect page numbers.

\documentclass[a4paper]{article}

\usepackage{fancyhdr}
\usepackage{pdfpages}
\usepackage{multicol}
\usepackage{refcount}
\usepackage{imakeidx}
\usepackage{hyperref}
\usepackage{bookmark}

\makeindex[program=makeindex,columns=2,intoc=true]

%https://tex.stackexchange.com/a/55654
\makeatletter
\newcommand*{\mypdfbookmark}[5]{\bookmark[page=\numexpr\getpagerefnumber{pdfpages@#1@begin}+#2\relax,view={#3}]{#4}\index{#5}}
\makeatother

\hypersetup{
backref=true,
pagebackref=true,
hyperindex=true,
bookmarks=true,
}

\renewcommand{\footrulewidth}{0pt}% % No footer rule

\begin{document}

\thispagestyle{empty}

\tableofcontents

\section*{List of bookmarked items}
\begin{multicols}{2}
\makeatletter
\@starttoc{lot}
\makeatother
\end{multicols}

\clearpage
\setcounter{page}{1}
\pagestyle{plain}

\mypdfbookmark{Test1}{1}{FitH \calc{\paperheight-2cm}}{Mark1}{Cat!a}
\mypdfbookmark{Test1}{2}{FitH \calc{\paperheight-4cm}}{Mark2}{Cat!b}

\includepdf[
pages=-,
scale=0.95,
{
1,table,{Cat a},lab:CatA,
2,table,{Cat b},lab:CatB
},
pagecommand={\pagestyle{plain}},
]{test.pdf}

\mypdfbookmark{Test2}{1}{FitH \calc{\paperheight-2cm}}{Mark3}{Dog!a}
\mypdfbookmark{Test2}{2}{FitH \calc{\paperheight-4cm}}{Mark4}{Dog!b}

\includepdf[
pages=-,
scale=0.95,
{
1,table,{Dog a},lab:DogA,
2,table,{Dog b},lab:DogB
},
pagecommand={\pagestyle{plain}},
]{test.pdf}
\printindex

\end{document}


This is the output (after joining the contents page (physical page 1) with the index page (physical page 6).

You are right, there's no addtoindex option in pdfpages. And actually I'm quite astonished that there isn't. Probably because no one ever asked for this feature.

Anyway,it's possible to use the pagecommand option and a small macro to get index entries. Here's an example:

\documentclass[a4paper]{article}
\usepackage{pdfpages}
\usepackage{makeidx}\makeindex
\usepackage{hyperref}

\makeatletter
\ifnum\AM@page=#2\relax\index{#1}\fi
}
\makeatother %

\begin{document}

\includepdf[
pages=1-3,
]{dummy.pdf}

\includepdf[
pages=1-3,

• (+1) Could you say what AM@page gives? I assume it gives the relative page number, but this is just a guess. – cfr Apr 15 '17 at 17:07
• Yes, \AM@page is a macro (not a count register!) reflecting the page number of the inserted PDF. Internally pdfpages calls \includegraphics[page=\AM@page]{dummy.pdf}. – Andreas Matthias Apr 15 '17 at 17:27
• @Andreas. When I add \pagestyle{fancy} with \addtoindex, no header or footer is produced. Removing \addtoindex restores the headers and footers. – Ross Apr 15 '17 at 19:07
• @Ross I have corrected my answer. \thispagestyle{empty} was mistakenly set inside of \addtoindex. Now it should work with \thispagestyle{fancy} as well. – Andreas Matthias Apr 15 '17 at 20:15