如何使用PDFBox获取PDF中书签内容的页码 [英] How to get the pagenumber of the content of a bookmark in a PDF with PDFBox

查看:673
本文介绍了如何使用PDFBox获取PDF中书签内容的页码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Apache PDFBox 2.0.x版.我正在尝试使用书签搜索PDF,当我命中目标时,我应该能够获得书签所指的页码.这是我打印所有书签的代码.我可以像searchText.equals(current.getTitle())

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?

推荐答案

我找到了解决方案.

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();
    }

}

这篇关于如何使用PDFBox获取PDF中书签内容的页码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆