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
    Commented Oct 26, 2013 at 13:41
  • 1
    @rickhg12hs - why not write this up as an answer here?
    – slm
    Commented Oct 26, 2013 at 22:00
  • Answer written. Hopefully @Terdon will un-mark it for deletion soon.
    – rickhg12hs
    Commented Oct 27, 2013 at 22:56
  • Actually @terdon marked it for deletion. Maybe he can unmark it
    – rickhg12hs
    Commented Oct 28, 2013 at 17:00
  • Gave up on @terdon. Answer replicated below.
    – rickhg12hs
    Commented Oct 29, 2013 at 3:18

1 Answer 1


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:



\addcontentsline{toc}{section}{The first section name}% or chapter
\addcontentsline{toc}{section}{The second section name}% or chapter
\addcontentsline{toc}{section}{The third section name}% or chapter
\addcontentsline{toc}{section}{The forth section name}% or chapter

  • When copying answers from others please mark your question as community wiki. Commented Nov 3, 2013 at 17:39
  • @MartinSchröder Where did you come up with this rule? Commented Nov 4, 2013 at 13:35
  • @MichaelMrozek: It's standard on tex.se: It's not your answer so you shouldn't get the credit. Commented Nov 4, 2013 at 16:35
  • Karma cabals crack me up.
    – rickhg12hs
    Commented Nov 4, 2013 at 17:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .