2

I have a dozen essays as PDFs which I want to combine to one concatenated master PDF with a table of content where each entry is a clickable link to the first page of each essay. The TOC could be either a page with internal links or a proper PDF TOC.

The best would be a command line solution on Linux and macOS. So far I have used QPDF, which works great for concatenating the essay PDFs, but it does not build a TOC.

It is a one-off problem, so I am happy to write some (bash, Python or other) scripting code to generate this TOC. For utility it is important that the links are clickable.

Any idea how to do this?

halloleo
  • 9,216
  • 13
  • 64
  • 122
  • Is it a one-off task? If yes, then you can create TOC page as a separate file using e.g. latex and then prepend it to the concateneted file. If you need it to be fully automated, then I think you have to wright a bit of code or ask someone to do it for you. – Yuras Jun 13 '18 at 17:54
  • @Yuras *Writing* the TOC page text with LaTeX, Markdown or else and converting it to PDf I know how to do, but how can I make the clickable links? – halloleo Jun 13 '18 at 20:07
  • In PDF, clickable link is just another type of annotation, like text or highlight annotations. I guess Ghostscript can add annotations. – Yuras Jun 13 '18 at 20:48
  • @Yuras But when i 'write' theTOC page with LaTeX or Markdown or whatever how can I specify the link targets to pages of the concatenated pre-existing PDFs? And which tool then generates the links in the Master PDF? – halloleo Jun 14 '18 at 01:14
  • Just did some research: LaTeX with the [`pdfpages`](https://ctan.org/pkg/pdfpages?lang=en) package should be able to do the concatenation, but can it generate the clickable links as well? – halloleo Jun 14 '18 at 01:15

1 Answers1

3

As I already noted, you can create TOC page manually and append/prepend it to the file.

To make TOC clickable, you need to add link annotations to it. After quick googling I made the following example using GhostScript:

gs -o output.pdf -sDEVICE=pdfwrite -dPDFSETTINGS=/prepress input.pdf an.txt

And an.txt file contains the following:

[ /Subtype /Link
   /SrcPg 1
   /Rect [10 10 50 50]
   /Page 2
   /ANN pdfmark

Here SrcPg is page number to put annotation on; Rect is the area to make clickable; Page is destination page number.

You can find more details on annotation syntax here and here. Hope it helps.

Yuras
  • 13,856
  • 1
  • 45
  • 58