Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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.

share|improve this question
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
1  
This worked great, many many thanks!! –  Adam Spiers Oct 27 '13 at 1:16
    
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

1 Answer 1

up vote 5 down vote accepted

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}
share|improve this answer
    
When copying answers from others please mark your question as community wiki. –  Martin Schröder Nov 3 '13 at 17:39
    
@MartinSchröder Where did you come up with this rule? –  Michael Mrozek Nov 4 '13 at 13:35
    
@MichaelMrozek: It's standard on tex.se: It's not your answer so you shouldn't get the credit. –  Martin Schröder Nov 4 '13 at 16:35
    
Karma cabals crack me up. –  rickhg12hs Nov 4 '13 at 17:07

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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