Here's a python script that will run on MacOS to produce a rudimentary TOC.
You'll need to enter your own values for the input file and output filename, and the page number and label for each entry.
#!/usr/bin/python
# -*- coding: utf-8 -*-
# CREATE PDF OUTLINES v.1.0 : Add a simple list of Bookmarks to a PDF.
from Foundation import NSURL, NSString
import Quartz as Quartz
import sys
# You will need to change these filepaths to a local test pdf and an output file.
infile = "/path/to/file.pdf"
outfile = '/path/to/output.pdf'
def getOutline(page, label):
myPage = myPDF.pageAtIndex_(page)
pageSize = myPage.boundsForBox_(Quartz.kCGPDFMediaBox)
x = 0
y = Quartz.CGRectGetMaxY(pageSize)
pagePoint = Quartz.CGPointMake(x,y)
myDestination = Quartz.PDFDestination.alloc().initWithPage_atPoint_(myPage, pagePoint)
myLabel = NSString.stringWithString_(label)
myOutline = Quartz.PDFOutline.alloc().init()
myOutline.setLabel_(myLabel)
myOutline.setDestination_(myDestination)
return myOutline
pdfURL = NSURL.fileURLWithPath_(infile)
myPDF = Quartz.PDFDocument.alloc().initWithURL_(pdfURL)
if myPDF:
# Create Outlines. Add the Page Index (from 0) and label in pairs here:
myTableOfContents = [
(0, 'Page 1'),
(1, 'Page 2'),
(2, 'Page 3')
]
allMyOutlines = []
for index, outline in myTableOfContents:
allMyOutlines.append(getOutline(index, outline))
rootOutline = Quartz.PDFOutline.alloc().init()
for index, value in enumerate(allMyOutlines):
rootOutline.insertChild_atIndex_(value, index)
myPDF.setOutlineRoot_(rootOutline)
myPDF.writeToFile_(outfile)