LaTeX – How to Create a Table of Contents for Merged PDF Files Using LaTeX

bookmarkspdftable of contents

I have a set of pdf files which I want to merge into a larger pdf files. Each individual pdf file is an article with sections. I am hoping to make the merged pdf file into a book format with each article being a chapter, whose sections and subsections correspond to that of the individual chapter.

What I need is a table of contents page which shows the chapters, the sections, and subsections and links to them with this table of contents page being displayed in the sidebar of a pdf reader such as evince (which calls it an index).

What I have right now:

\documentclass{report}
\author{<somename>}
\title{<sometitle>}
\date{<somedate>}
\usepackage{pdfpages}
\usepackage[pdfauthor={<somename>},%
        pdftitle={<sometitle>},%
        pdftex]{hyperref}

\begin{document}

\tableofcontents

\clearpage\phantomsection
\addcontentsline{toc}{chapter}{<chaptername>}
\includepdf[pages=-,linktodoc=false]{file1.pdf}

\clearpage\phantomsection
\addcontentsline{toc}{chapter}{<chaptername>}
\includepdf[pages=-,linktodoc=false]{file2.pdf}

\clearpage\phantomsection
\addcontentsline{toc}{chapter}{<chaptername>}
\includepdf[pages=-,linktodoc=false]{file3.pdf}

\clearpage\phantomsection
\addcontentsline{toc}{chapter}{<chaptername>}
\includepdf[pages=-,linktodoc=false]{file4.pdf}

\end{document}

Now file3.pdf and file4.pdf have sections and subsections in them. How do I display these sections and subsections in the table of contents and link from there to the actual page? (And I also would like this info to be displayed in the sidebar of a pdf reader such as evince).

Best Answer

The entries in the "table of contents page [which is] being displayed in the sidebar of a pdf reader" are called PDF bookmarks in Adobe Acrobat. The hyperref and bookmark packages provide functionality to place own bookmarks into your PDF.

You should go for the bookmark package (which loads hyperref internally). It allows you to place links from the sidebar to specific pages of the current (or even an external) PDF with specific page, view port and zoom levels. You would need to collect the pages (and maybe zoom settings) for each (sub-)section by yourself if you don't have the LaTeX source of them.

An example for an bookmark entry which should appear in a certain hierarchy level and point to a certain page with a certain height (FitH) is:

\bookmark[level=<num>,page=<num>,view={FitH 842}]{<title>}

(See also the similar questions if you want to place links inside the document:
PDF hyperlinks to a given page.
pdfpages and linktodoc)