Would it be possible to (kind of) script this? I’m thinking along these lines:
- convert the MD to PDF
- grep all headlines from the MD document
- for each of them, find the line in the PDF (i.e. the string in the text layer) corresponding to it
- use the complete line (i.e. possible with leading numbers etc.) to find the
PDFSelection
in the document - using the properties from these
PDFSelection
s, build aPDFOutline
for the TOC by creatingPDFOutline
objects withPDFActionGoTo action
s for each selection. - and finally adds this
PDFOutline
to the document.
I can add an outline to the PDF alright (see the code below), but Preview doesn’t show me neither a TOC nor a bookmark. Instead, it gets really unhappy and finally crashes. Acrobat Reader shows me a bookmark, not no TOC. Is there anything special I have to do so that a PDFOutline
is recognized as a TOC (also in Preview, preferably)? The Net is quite terse on that issue.
const outline = $.PDFOutline.alloc.init;
const firstChild = $.PDFOutline.alloc.init;
firstChild.label = $("1st outline line");
const pdfSelection = pdfDoc.findStringWithOptions($(headline),0);
if (pdfSelection.js.length > 0) {
const firstSelection = pdfSelection.js[0];
const page = firstSel.pages.js[0]; // NSPage object!
const bounds = firstSel.boundsForPage(page); //NSRect object
const pt = $.NSPointFromCGPoint($.NSRectToCGRect(bounds).origin);
const destination = $.PDFDestination.alloc.initWithPageAtPoint(page, pt);
const action = $.PDFActionGoTo.alloc.initWithDestination(destination);
firstChild.action = action;
firstChild.isOpen = true;
outline.insertChildAtIndex(firstChild, 0);
pdfDoc.setOutlineRoot(outline);
pdfDoc.writeToFile(r.path());
}