Get the page number from document outline (bookmarks)

后端 未结 1 999
感情败类
感情败类 2021-01-17 02:09

I am using the itext7 library to manipulate some existing PDFs. For some reason, I am not able to get the page number from the outline. I guess I somehow should get it from

相关标签:
1条回答
  • 2021-01-17 02:38

    Regarding the levels of the outline hierarchy, in order to traverse the whole hierarchy you will have to check for each PdfOutline's children and traverse them recursively.

    The names parameter that was confusing to you is the parameter that is responsible for resolving named destinations which is necessary to get the page numbers correctly in general case because your PDF document may contains explicit as well as named destinations. To get the names map you can use pdfDocument.getCatalog().getNameTree(PdfName.Dests).getNames();

    To find the page number by a page object, you should use pdfDocument.getPageNumber(PdfDictionary).

    Overall, the method walking through the outlines may look as following:

    void walkOutlines(PdfOutline outline, Map<String, PdfObject> names, PdfDocument pdfDocument) {
        if (outline.getDestination() != null) {
            System.out.println(outline.getTitle() + ": page " +
                    pdfDocument.getPageNumber((PdfDictionary) outline.getDestination().getDestinationPage(names)));
        }
        for (PdfOutline child : outline.getAllChildren()) {
            walkOutlines(child, names, pdfDocument);
        }
    }
    

    And the main entry point to call the method to traverse the outline root:

    PdfNameTree destsTree = pdfDocument.getCatalog().getNameTree(PdfName.Dests);
    PdfOutline root = pdfDocument.getOutlines(false);
    walkOutlines(root, destsTree.getNames(), pdfDocument);
    

    Please note that the code sample is for Java, but it should be similar in C# except some case changes and IDictionary instead if Map.

    0 讨论(0)
提交回复
热议问题