[英]How to get the pagenumber of the content of a bookmark in a PDF with PDFBox
我使用的是 Apache PDFBox 2.0.x 版。 我正在嘗試使用書簽搜索 PDF,當我達到目標時,我應該能夠獲得書簽所指的頁碼。 這是我打印所有書簽的代碼。 我可以做一個像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();
}
}
如果標題與我的搜索文本匹配,那么這就是我的目標書簽。 有人試過這個嗎?
我找到了解決方案。
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();
}
}
給定一個 PDDocument 對象和該文檔對象的 PDOutlineItem,另一種查找書簽指向的頁面索引的方法如下:
public static void printBookmark2(PDDocument document, PDOutlineNode outline) throws IOException {
PDOutlineItem current = outline.getFirstChild();
while (current != null)
{
//use PDOutlineItem and PDDocument objects to find the page which the bookmark
//points to
PDPage currentPage = current.findDestinationPage(document);
//use the document and the page to find the index of the page
Integer currentPageIndex = document.getPages().indexOf(currentPage);
...
//Do other operations
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.