9

I'm trying to add a bookmark to a PDF using PyPDF2. I run the following with no problems. But a bookmark is never created. Any thoughts on what I'm doing wrong. The PDF is 2 pages long.

from PyPDF2 import PdfFileReader, PdfFileWriter

reader = PdfFileReader("test.pdf")  # open input
writer = PdfFileWriter()  # open output
writer.addPage(reader.getPage(0))  # insert page
writer.addBookmark("Hello, World Bookmark", 0, parent=None)  # add bookmark
Martin Thoma
  • 124,992
  • 159
  • 614
  • 958
rmp2150
  • 777
  • 1
  • 11
  • 22
  • Possible duplicate of [Adding bookmarks using PyPDF2](http://stackoverflow.com/questions/18855907/adding-bookmarks-using-pypdf2) – Shubham Namdeo Mar 02 '17 at 04:19
  • @ShubhamNamdeo that question, doesnt help; theres an error in the answer – rmp2150 Mar 02 '17 at 06:28
  • do the bookmarks depend on the previous added pages? Can they be added at any moment, for example also at the end when all pages are already added? – cards Jan 30 '23 at 23:25

2 Answers2

13

I ran your code (adding the text below it to write out the pdf) and found a bookmark was, in fact, created.

from PyPDF2 import PdfFileReader, PdfFileWriter

writer = PdfFileWriter()  # open output
reader = PdfFileReader("test.pdf")  # open input
writer.addPage(reader.getPage(0))  # insert page
writer.addBookmark("Hello, World Bookmark", 0, parent=None)  # add bookmark
with open("result.pdf", "wb") as fp:  # creating result pdf JCT
    writer.write(fp)  # writing to result pdf JCT

Check the bookmarks panel in your result. Having bookmarks doesn't automatically cause a PDF to open to the bookmarks panel.

To make it open to the bookmarks panel with PyPDF2, add one line:

writer = PdfFileWriter()  # open output
reader = PdfFileReader("test.pdf")  # open input
writer.addPage(reader.getPage(0))  # insert page
writer.addBookmark("Hello, World Bookmark", 0, parent=None)  # add bookmark
writer.setPageMode("/UseOutlines")  # This is what tells the PDF to open to bookmarks
with open("result.pdf", "wb") as fp:  # creating result pdf JCT
    writer.write(fp)  # writing to result pdf JCT
Martin Thoma
  • 124,992
  • 159
  • 614
  • 958
James C. Taylor
  • 430
  • 3
  • 8
9

Thanks for James' code. But it only outputs one-page pdf file and doesn't show how to add multiple bookmarks. Here is a revised version of his code to do so.

from PyPDF2 import PdfFileReader, PdfFileWriter

reader = PdfFileReader("test.pdf")  # open input
writer = PdfFileWriter()  # open output

n = reader.getNumPages()

for i in range(n):
    writer.addPage(reader.getPage(i))  # insert page


# add a bookmark on the first page
writer.addBookmark("Hello, World Bookmark", 0, parent=None)

# add a bookmark on the sixth page
par = writer.addBookmark("Second Bookmark", 5, parent=None)

# add a child bookmark on the eighth page
writer.addBookmark("Third Bookmark", 7, parent=par)

with open("result.pdf", "wb") as fp:  # creating result pdf JCT
    writer.write(fp)  # writing to result pdf JCT

Martin Thoma
  • 124,992
  • 159
  • 614
  • 958
bfhaha
  • 401
  • 4
  • 6