The pdf.tocgen tool consists of three parts: pdfxmeta extracts metadata from PDF files, pdftocgen generates a table of contents based on the metadata, and pdftocio imports the contents into the PDF document. An example using Paul Graham’s “On Lisp” demonstrates these steps. Users create a recipe file using pdfxmeta and then use pdftocgen to generate the table of contents. The tool is best for PDFs generated from software like pdftex and does not work with scanned PDFs. It is free software available on GitHub under the GPLv3 license. LaTeX users are advised to use the hyperref package for better results.
https://krasjet.com/voice/pdf.tocgen/