2

How to add Bookmarks to pdf using Pymupdf. I have seen many ways using PyPDF2 but since I'm already using pymupdf for other annotations I would prefer pymupdf for adding bookmarks. Also would like to highlight the text and add bookmarks to it.

Nayana Madhu
  • 1,185
  • 5
  • 17
  • 34

1 Answers1

1

You cannot add single bookmarks like you can in other packages. If you have looked at the details there - or rather in the respective PDF specification, this is an overly / unnecessarily complex task. PyMuPDF in contrast has this simple approach to offer:

Prepare a Python list that looks like a traditional table of contents (TOC): Every line in the list contains the hierarchy level, the text to display and the page number. Optionally also some information where on the target page the pointer goes to.

Then use doc.set_toc(toc_list). All pesky detail is taken care of for you.

If the PDF already has a TOC, extract it to a list of that same structure via toc_list = doc.get_toc(). Then modify as required.

Jorj McKie
  • 2,062
  • 1
  • 13
  • 17