如何提取标题书签下的嵌套书签

时间:2017-07-09 03:08:34

标签: java pdf pdfbox

这是一种奇怪的情况。我正在制作一个pdf文件,其中只有一个书签名称,但在此书签下有42个书签嵌套或子书签。我不知道如何提取这个,因为我没有看到任何可用的选项。虽然我已将此问题发布到pdf框队但尚未回复。任何人都可以建议任何选项或解决方案吗?以下是我的pdf

中书签的结构
MainBookMarkName
|_firstBookMark (1-10 pages)
|_secondBookMark (1-5 pages)
|_thirdBookMark (1-8 pages)
|_FourthBookMark (1-15 pages)
|_
|_
|_lastBookMark (42)

以下代码只会加载" MainBookMarkName"并打印所有嵌套书签,但不循环每个书签或提取其中任何一个。所有页面仅与嵌套书签相关联,而不是主书签。

//Trying to access step1 or step 2 sub book marks and then get their pages but those sub marks am unable to access individually.

How to “Combine Files into a PDF” using Acrobat Pro
    Step 1: Organize Your Activity Files & Create a Table of Contents
    Step 2: Open Adobe Acrobat Pro.
    Step 3: Add Files
    Step 4: Arrange Files in Final Order
Step 5: Combine Files to Finalize the Document




public static PDPage printBookmark(PDDocument document, PDOutlineNode bookmark, String indentation) throws IOException
        {
            PDOutlineItem current = bookmark.getFirstChild();
           PDOutlineItem subBookMark =  current.getFirstChild();


            while( subBookMark!= null )
            {
                System.out.println(subBookMark.getTitle());

                if (subBookMark.getDestination() instanceof PDPageDestination)
                {

                   if(subBookMark.getTitle().equals("SubBookMark10")){


                       PDPageDestination pd = (PDPageDestination) subBookMark.getDestination();
                        System.out.println("Destination page: " + (pd.retrievePageNumber() + 1));

                        return pd.getPage();

                        }
                }

                printBookmark( document, current, indentation + "    " );
               subBookMark = subBookMark.getNextSibling();
            }
            return null;

        }




 //Trying to access step1 or step 2 sub book marks and then get their pages but those sub marks am unable to access individually.

How to “Combine Files into a PDF” using Acrobat Pro
    Step 1: Organize Your Activity Files & Create a Table of Contents
    Step 2: Open Adobe Acrobat Pro.
    Step 3: Add Files
    Step 4: Arrange Files in Final Order
Step 5: Combine Files to Finalize the Document

1 个答案:

答案 0 :(得分:0)

以下是提取子书签的答案。

   public static PDPage printBookmark(PDDocument document, PDOutlineNode bookmark, String indentation) throws IOException
            {
                PDOutlineItem current = bookmark.getFirstChild();
               PDOutlineItem subBookMark =  current.getFirstChild();


                while( subBookMark!= null )
                {
                    System.out.println(subBookMark.getTitle());

                    if (subBookMark.getDestination() instanceof PDPageDestination)
                    {

                       if(subBookMark.getTitle().equals("SubBookMark10")){


                           PDPageDestination pd = (PDPageDestination) subBookMark.getDestination();
                            System.out.println("Destination page: " + (pd.retrievePageNumber() + 1));

                            return pd.getPage();

                            }
                    }

                    printBookmark( document, current, indentation + "    " );
                   subBookMark = subBookMark.getNextSibling();
                }
                return null;

            }