# Make a table of content from page number

I have a some PDF files and i wanna make them one PDF by including each of them in order into a Latex file. How can i have table of content which reference to first page of each PDF. Suppose i have two PDF files first have two pages and second one have 4 pages i wanna a clickable table of contents like below

pdf1 .......... 2
pdf2 .......... 4


and by clicking each number it goes to first page of each PDF.

I add PDFs by \include{filename} using \usepackage{pdfpages}.

With option addtotoc of \includepdf you can add items to the TOC:

\documentclass[a4paper]{article}
\usepackage{mwe}
\usepackage{pdfpages}
\usepackage{hyperref}

\begin{document}
\tableofcontents

\section{Test}
\lipsum

\includepdf[
pages=1-2,
]{example-image-a4-numbered}

\includepdf[
pages=-,
]{example-image-a4}

\end{document}


Edit: Here's another example with some explanations about the meaning of the parameter list of addtotoc:

\documentclass[a4paper]{article}
\usepackage{mwe}
\usepackage{pdfpages}
\usepackage{hyperref}

\begin{document}
\tableofcontents

\includepdf[
pages=3-4,
3,section,1,AAA,sec:AAA,
3,subsection,2,BBB,sec:BBB,
4,section,1,CCC,sec:CCC}
]{example-image-a4-numbered}

See \ref{sec:BBB}.

See \autoref{sec:BBB}.

See \nameref{sec:BBB}.

\end{document}


<page number>, <section>, <section level>, <heading>, <label>

In the example I included pages 3 and 4 of the PDF and added TOC entries for both pages. Page 3 comprises one section (with name AAA and label sec:AAA) and one subsection (name BBB and label sec:BBB). Page 4 comprises just one section (name CCC and label sec:CCC). The label can be referenced like any other LaTeX label, e.g. \ref{sec:BBB}.

One thing that's a bit weird is the <section level>. It's a kind of internal book-keeping used by LaTeX and denotes the depth of the section:

   part: -1
chapter: 0
section: 1
subsection: 2
...


Note that these numbers are fixed. Even if your LaTeX class does not have a chapter level, part level will always be -1, and section level will always be 1. For more details about these LaTeX internals see the explanation of \@startsection in the LaTeX source documentation source2e.pdf. (Try: texdoc source2e)

• could you please explain arguments you passed to  addtotoc ? Feb 27 '21 at 22:01
• when i set page number for all file it do not work. but when for all pages i set page = - it works correctly. and for combinations of them still not sure. Feb 27 '21 at 22:19
• @alfred I added another example with additional explanations concerning the parameter list of addtotoc. Feb 27 '21 at 23:28
• thank a lot. even first example solved my problem. Feb 27 '21 at 23:29