I wrote an open source command line toolset called pdf.tocgen just for doing this. It uses the embedded font attributes and position information of headings to generate a table of contents automatically.
For example, for the PDF version Paul Graham's On Lisp, available for download on his website but comes without a table of contents. You could use the pdfxmeta
tool to build a "recipe" file
[[heading]]
level = 1
font.name = "Times-Bold"
font.size = 19.92530059814453
[[heading]]
level = 2
font.name = "Times-Bold"
font.size = 11.9552001953125
save it as recipe.toml
, and use the pdftocgen
command to automatically generate an outline
$ pdftocgen onlisp.pdf < recipe.toml
"Preface" 5
"Bottom-up Design" 5
"Plan of the Book" 7
"Examples" 9
"Acknowledgements" 9
"Contents" 11
"The Extensible Language" 14
"1.1 Design by Evolution" 14
"1.2 Programming Bottom-Up" 16
"1.3 Extensible Software" 18
"1.4 Extending Lisp" 19
"1.5 Why Lisp (or When)" 21
"Functions" 22
"2.1 Functions as Data" 22
"2.2 Defining Functions" 23
"2.3 Functional Arguments" 26
"2.4 Functions as Properties" 28
"2.5 Scope" 29
"2.6 Closures" 30
"2.7 Local Functions" 34
"2.8 Tail-Recursion" 35
"2.9 Compilation" 37
"2.10 Functions from Lists" 40
[--snip--]
You could save the output to a file called toc
$ pdftocgen onlisp.pdf < recipe.toml > toc
and import it to the PDF file using pdftocio
:
$ pdftocio -o output.pdf onlisp.pdf < toc
Please read the homepage for the details on how to use this toolset. I hope you find it useful.