How to get the pagenumber of the content of a bookmark in a PDF with PDFBox

久未见 提交于 2020-01-16 12:02:16

问题


I am using Apache PDFBox version 2.0.x. I am trying to search a PDF using bookmarks and when I hit my target I should be able to get the Pagenumber the bookmark is referring to. This is my code to print all bookmarks. I can do an equals search like searchText.equals(current.getTitle())

public static void printBookmark(PDOutlineNode bookmark, String indentation) throws IOException {
    PDOutlineItem current = bookmark.getFirstChild();
    COSObject targetPageRef = null;
    while (current != null) {
        System.out.println(indentation + current.getTitle());           
        printBookmark(current, indentation + "    ");
        current = current.getNextSibling();
    }
}

If the title matches my search text then that is my target bookmark. Anyone tried this before?


回答1:


I found the solution.

public static void printBookmark(PDOutlineNode bookmark, String indentation) throws IOException
{
    PDOutlineItem current = bookmark.getFirstChild();
    COSObject targetPageRef = null;
    while (current != null)
    {
        System.out.println(indentation + current.getTitle());       
        PDPageFitWidthDestination destination = (PDPageFitWidthDestination) current.getDestination();
        System.out.println("Page Number " + destination.retrievePageNumber());
        printBookmark(current, indentation + "    ");
        current = current.getNextSibling();
    }

}


来源:https://stackoverflow.com/questions/38358472/how-to-get-the-pagenumber-of-the-content-of-a-bookmark-in-a-pdf-with-pdfbox

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!