Java:使用itext读取PDF书签名称 [英] Java: Reading PDF bookmark names with itext

查看:495
本文介绍了Java:使用itext读取PDF书签名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理包含多个文档的单个PDF.每个文档都有一个书签.我需要阅读我正在构建的对帐应用程序的书签名称.下面的代码对我不起作用.我正在尝试将书签名称放置在 title 字符串中.谁能提供任何指导?非常感谢.

I am working with a single PDF containing multiple documents. Each document has a bookmark. I need to read the bookmark names for a reconciliation application that I am building. The code below is not working for me. I am trying to place the bookmark name in the title string. Can anyone provide any guidance? Thank you very much.

PdfReader reader = new PdfReader("C:\\Work\\Input.pdf");
List<HashMap<String,Object>> bookmarks = SimpleBookmark.getBookmark(reader);

for(int i = 0; i < bookmarks.size(); i++){

    HashMap<String, Object> bm = bookmarks.get(i);
    String title = ((String)bm.get("Title"));

}

推荐答案

您没有考虑到书签存储在带有树枝和树叶的树形结构中(在PDF规范中,它称为大纲树).

You are not taking into account that bookmarks are stored in a tree structure with branches and leaves (in the PDF specification, it's called the outline tree).

正如@Todoy在评论部分中所述,您的代码适用于顶层,但如果要查看 all 标题,则需要使用一种递归方法,该方法还应查看"Kids".

As @Todoy says in the comment section, your code works for the top-level, but if you want to see all the titles, you need to use a recursive method that also looks at the "Kids".

看看此代码示例:

public void inspectPdf(String filename) throws IOException, DocumentException {
    PdfReader reader = new PdfReader(filename);
    List<HashMap<String,Object>> bookmarks = SimpleBookmark.getBookmark(reader);
    for (int i = 0; i < bookmarks.size(); i++){
        showTitle(bookmarks.get(i));
    }
    reader.close();
}

public void showTitle(HashMap<String, Object> bm) {
    System.out.println((String)bm.get("Title"));
    List<HashMap<String,Object>> kids = (List<HashMap<String,Object>>)bm.get("Kids");
    if (kids != null) {
        for (int i = 0; i < kids.size(); i++) {
            showTitle(kids.get(i));
        }
    }
}

showTitle()方法是递归的.如果检查的书签条目中有孩子,它会自行调用.使用此代码段,您可以遍历大纲树的所有分支和叶子.

The showTitle() method is recursive. It calls itself if an examined bookmark entry has kids. With this code snippet, you can walk through all the branches and leaves of the outline tree.

这篇关于Java:使用itext读取PDF书签名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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