# Make an index for an external pdf file

Here's what I am trying to do:

I have a large scanned pdf file and I want to make an index page so that I can jump to a given page by clicking on a link. So far, I managed to do that, but I would like to print the page number as well, so that someone with a physical copy can locate a given page. Since there are many entries, I was thinking about somethig like this:

1. Entry 1 (p. xx)
2. Entry 2 (p. yy)
3. Entry 3 (p. zz)

and so on.

I also want to add a red box around the entries so that they show on the pdf file but not on a printed copy. I don't need TOC for the pdf file. Is it possible to do that?

Main code:

\documentclass[12pt,a4paper]{article}
\input{packs.tex}
\title{\Large Fancy title}
\author{\Large Me}
\date{\Large \today}
\begin{document}
\maketitle
Here are some entries...
\begin{multicols}{2}
\begin{foo}[nosep]
\end{foo}
\end{multicols}
\includepdf[pages=-,pagecommand={\thispagestyle{plain}}]{dummy.pdf}
\end{document}


packs.tex:

\usepackage[utf8]{inputenc}
\usepackage{enumitem}
\usepackage{multicol}
\usepackage{pdfpages}
\usepackage[left=2cm,right=2cm,top=2cm,bottom=2cm]{geometry}
\makeatletter
\newcommand{\checkForEnd}{\@ifnextchar\end{}{\item}}
\makeatother
\newenvironment{foo}[1][]
{\par\obeylines
\begingroup\lccode~=\^^M
\lowercase{\endgroup\let~}\checkForEnd
\begin{enumerate}[#1]}
{\end{enumerate}}
\makeatletter
\let\orig@Hy@EveryPageAnchor\Hy@EveryPageAnchor
\def\Hy@EveryPageAnchor{%
\begingroup
\hypersetup{pdfview=Fit}%
\orig@Hy@EveryPageAnchor
\endgroup
}
\makeatother
\usepackage{hyperref}


And the code to generate a dummy pdf file:

\documentclass{report}
\usepackage{blindtext}
\pagenumbering{gobble}
\begin{document}
\Blinddocument
\end{document}


You can define a new command with the entry text and the page number as argument, and then use the page number both in the \hyperlink command and again to print the number itself in the index.

For the red box that is not printed you can set some hyperref options:

• colorlinks=false (true means that the link text itself is colored, which is also printed, false means that the border is shown, which is not printed)
• linkbordercolor={1 0 0} (RGB between 0 and 1, so 1 0 0 is red)
• pdfborder={0 0 1} (link border box of 1pt width)

These values are the default (so you don't need to set them explicitly) but the defaults may change in future versions of hyperref, see better default colors for hyperref links.

MWE:

\documentclass[12pt,a4paper]{article}
\input{packs.tex}

% explicitly set red link boxes

\newcommand{\myindex}[2]{%
}

\title{\Large Fancy title}
\author{\Large Me}
\date{\Large \today}
\begin{document}
\maketitle
Here are some entries...
\begin{multicols}{2}
\begin{foo}[nosep]
\myindex{Entry 1}{2}
\myindex{Entry 2}{4}
\myindex{Entry 3}{6}
\end{foo}
\end{multicols}
\includepdf[pages=-,pagecommand={\thispagestyle{plain}}]{dummy.pdf}
\end{document}


Result:


\documentclass[12pt,a4paper]{article}
\input{extindexpre.tex}
\usepackage{xparse}

% explicitly set red link boxes

\NewDocumentCommand{\myindex}{m m o}{%
\IfValueTF{#3}
{\printmyindex{#2}{#1}{#3}}%
{\printmyindex{#2}{#1}{#2}}%
}

\newcommand{\printmyindex}[3]{%
}

\title{\Large Fancy title}
\author{\Large Me}
\date{\Large \today}
\begin{document}
\maketitle
Here are some entries...
\begin{multicols}{2}
\begin{foo}[nosep]
\myindex{Entry 1}{2}
\myindex{Entry 2}{4}[5]
\myindex{Entry 3}{6}
\end{foo}
\end{multicols}
\includepdf[pages=-,pagecommand={\thispagestyle{plain}}]{dummy.pdf}
\end{document}


• With a new enough xparse you can say \NewDocumentCommand{\myindex}{m m O{#2}} and avoid the \If... ;-) (see manual, section 1.4) Jul 9 '20 at 8:50