11

I have an existing PDF (without any corresponding source file), and a text file which is a list of items referencing sections within the PDF file, along with a page number for each item. I would like to generate a new PDF which starts with a generated table of contents index, followed by the contents of the original PDF. The generated index needs to be hyperlinked so that clicking on any item will automatically jump to the page containing the referenced section.

I'm looking for a suitable set of tools (preferably CLI-oriented) to accomplish this on Linux; all the tools I've looked at so far don't seem to offer a solution. I'm an experienced programmer, and am more than happy to write code if required (preferably in Ruby/Python/Perl/shell) to parse my text file and convert it into a hyperlinked index, but I can't find the right tools to solve it. I think part of the problem is that the hyperlinks need to point to within the same file, but their targets won't exist until later when the ToC is joined with the original PDF.

I'm also familiar with various markup languages, e.g. Markdown, reStructuredText, TeX, LaTeX, org-mode, and an ideal solution would use one of these as an intermediate step for generating the hyperlinked ToC.

  • 6
    Check out this very similar question on the TeX StackExchange: Adding Table of Contents to existing PDF – rickhg12hs Oct 26 '13 at 13:41
  • 1
    @rickhg12hs - why not write this up as an answer here? – slm Oct 26 '13 at 22:00
  • Answer written. Hopefully @Terdon will un-mark it for deletion soon. – rickhg12hs Oct 27 '13 at 22:56
  • Actually @terdon marked it for deletion. Maybe he can unmark it – rickhg12hs Oct 28 '13 at 17:00
  • Gave up on @terdon. Answer replicated below. – rickhg12hs Oct 29 '13 at 3:18
7

This is taken in whole from @Herbert answering a very similar question on the TeX StackExchange:

Adding Table of Contents to existing PDF

use package pdfpages and then:

\documentclass{article}
\usepackage{pdfpages}
\usepackage{hyperref}

\begin{document}

\tableofcontents
\clearpage\phantomsection
\addcontentsline{toc}{section}{The first section name}% or chapter
\includepdf[pages={1-10},linktodoc,linktodocfit=/Fit]{texte/dtk/dtk11-1/komoedie.pdf}
\clearpage\phantomsection
\addcontentsline{toc}{section}{The second section name}% or chapter
\includepdf[pages={11-19},linktodoc,linktodocfit=/Fit]{texte/dtk/dtk11-1/komoedie.pdf}
\clearpage\phantomsection
\addcontentsline{toc}{section}{The third section name}% or chapter
\includepdf[pages={20-29},linktodoc,linktodocfit=/Fit]{texte/dtk/dtk11-1/komoedie.pdf}
\clearpage\phantomsection
\addcontentsline{toc}{section}{The forth section name}% or chapter
\includepdf[pages={21-39},linktodoc,linktodocfit=/Fit]{texte/dtk/dtk11-1/komoedie.pdf}

\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.