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
\define@key{pdfpages}{linkname}{\def\AM@linkname@option{#1}\label{pdfpages@#1@begin}}
\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,
colorlinks=true,
linkcolor= purple,
bookmarks=true,
}
\renewcommand{\headrulewidth}{0pt}% % No header rule
\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=-,
link,
scale=0.95,
linkname=Test1,
addtotoc={1,section,1,file 1,aa},
addtolist=
{
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=-,
link,
scale=0.95,
linkname=Test2,
addtotoc={1,section,1,file 2,aa},
addtolist=
{
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).