यहाँ एक अजगर लिपि है जो एक अल्पविकसित टीओसी का निर्माण करने के लिए MacOS पर चलेगी। आपको इनपुट फ़ाइल और आउटपुट फ़ाइलनाम के लिए अपने स्वयं के मान दर्ज करने की आवश्यकता होगी, और प्रत्येक प्रविष्टि के लिए पृष्ठ संख्या और लेबल।
#!/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)
cpdf -add-bookmarksपीडीएफ की सामग्री की पुरानी तालिका को अधिलेखित किया गया है, इसलिए इसका उपयोग करके बुकमार्क को संपादित करना प्रतीत होता हैcpdf, जाने का तरीका है: (1)cpdf -list-bookmarks in.pdf > out.txt; (२) संपादित करनाout.txt; (३)cpdf -add-bookmarks...।