Skip to content

Instantly share code, notes, and snippets.

@bombs-kim
Last active March 4, 2020 10:20
Show Gist options
  • Save bombs-kim/bbed41bf5376f4dfbe645ca758e99a72 to your computer and use it in GitHub Desktop.
Save bombs-kim/bbed41bf5376f4dfbe645ca758e99a72 to your computer and use it in GitHub Desktop.
How to add bookmarks to pdf using python pypdf2
from PyPDF2 import PdfFileReader, PdfFileMerger
pdf_merger = PdfFileMerger()
f = open('book.pdf', 'rb')
pdf_merger.append(f)
offset = 25
addbm = pdf_merger.addBookmark
addbm('Contents', 5)
ch1_bm = addbm('1 Computations with Structured Matrices', offset + 1)
# Children
for title, page in ch1_sub_boomarks_contents:
addbm(title, offset+page, parent=ch1_bm)
addbm('2 Toeplitz/Hankel Matrix, Polynomial Computations', offset + 23)
with open('out.pdf', 'wb') as f:
pdf_merger.write(f)
# To check
f = open('out.pdf', 'rb')
pdf_reader = PdfFileReader(f)
print(pdf_reader.outlines)
# Edit bookmarks
# pdf_merger.bookmarks.extend(pdf_reader.outlines)
f = open('book.pdf', 'rb')
# May need to hack _trim_outline method...
pdf_merger.append(f, import_bookmarks=True)
def reduce_top(bookmarks):
for bm in bookmarks:
if isinstance(bm, dict):
bm[NameObject('/Top')] = NumberObject(bm['/Top']+500)
elif isinstance(bm, list):
reduce_top(bm)
reduce_top(pdf_merger.bookmarks)
@bombs-kim
Copy link
Author

PyPDF2 is B.S. For example, it mutates pdf_merger.bookmarks when it writes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment