pythonpdfpypdf

Collapse Bookmarks using PyPDF2


When I use PyPDF2 to merge two PDF documents, I set the Page Mode to /UseOutlines so that the PDF will display the bookmark pane when the document is opened.

from PyPDF2 import PdfMerger, PdfReader

merger = PdfMerger()
merger.append(PdfReader(filename), import_bookmarks=True)
merger.setPageMode('/UseOutlines')
merger.setPageLayout('/SinglePage')

However, whenever the PDF document is opened the bookmarks are always expanded. Is there a property that I can modify to force the bookmarks to be collapsed when the document is opened?


Solution

  • An open outline in PDF contains the /Count key in the dictionary indicating the number of children inside the outline. To display an outline as closed it should have this key removed or set to -1. But unfortunately no way to specify it in PyPDF2 yet.