Alex Rivera | Logout

How to get bookmark's page number

Asked 2011-11-30T16:52:25.197
9
from typing import List
from PyPDF2 import PdfFileReader
from PyPDF2.generic import Destination


def get_outlines(pdf_filepath: str) -> List[Destination]:
    """Get the bookmarks of a PDF file."""
    with open(pdf_filepath, "rb") as fp:
        pdf_file_reader = PdfFileReader(fp)
        outlines = pdf_file_reader.getOutlines()
    return outlines


print(get_outlines("PDF-export-example.pdf"))

pyPdf.pdf.Destination has many properties, but I can't find any referring page number of that bookmark. How can I get the page number of the bookmarks?


For example outlines[1].page.idnum returns a number which is approximately 3 times bigger than referenced page number in PDF document, which I assume references some object smaller then page, as running .page.idnum on whole PDF document outline returns array of numbers which is not even linearly correlated with "real" page number destinations in PDF document and it's roughly multiple by ~ 3


Update: This question is same as this: split a pdf based on outline although I don't understand what author did in his self answer there. Seems too complicated to me to be usable

Edit
Report

1 Answer

0

I'm not sure but according to the docs for pypdf.Destination the page number for the bookmark is just Destination.page .

answered 2011-11-30T18:19:53.780

Your Answer