Spring Harbor
Spring Harbor

Reputation: 1

How can I add the sub-bookmark to a PDF with pypdf, and remaining them when I tried many times?

There are 2 questions:

  1. I want to add the sub-bookmark to an existing bookmark. What I know is just to add first-layer bookmark.
  2. I want to know how to remain the existing bookmark to avoid delete an existing bookmark for the second run.

This is the code I tried:

from pypdf import PdfReader, PdfWriter


reader = PdfReader("updated_examplefile.pdf")
writer = PdfWriter()

# Copy all pages from the reader to the writer
for page in reader.pages:
    writer.add_page(page)


# Update the outline
writer.add_outline_item(title="sample chapter 1", page_number= 2)
writer.add_outline_item(title="sample chapter 2", page_number= 60)
writer.add_outline_item(title="sample chapter 3", page_number= 140)

# Update PDF
with open("updated_examplefile.pdf", "wb") as f:
    writer.write(f)

I add three bookmarks to "updated_examplefile.pdf". But the bookmarks will be refreshed on every run. I want to keep the bookmark for first run on the second run.

These bookmarks are just one layer. How to add sub-bookmark to them?

Upvotes: 0

Views: 79

Answers (0)

Related Questions