Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add a bookmark to a PDF with PyPDF2

Tags:

python

pypdf2

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
like image 822
rmp2150 Avatar asked Mar 02 '17 03:03

rmp2150


People also ask

How do I add a bookmark to a PDF in Linux?

In the PDF document itself, navigate to the page to which you would like to add the bookmark. In the side pane, select the Bookmarks button at the bottom. Press the + button at the bottom of the side pane. The default name of the bookmark you just added is the page number of the document.

What can PyPDF2 do?

PyPDF2 is a Python library for working with PDF documents. It can be used to parse PDFs, modify them, and create new PDFs. PyPDF2 can be used to extract some text and metadata from a PDF. This can be helpful if you're automating some processes on your existing PDF files.


2 Answers

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
like image 116
James C. Taylor Avatar answered Oct 06 '22 18:10

James C. Taylor


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

like image 45
bfhaha Avatar answered Oct 06 '22 17:10

bfhaha