问题
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