如何使用 Apache PdfBox 获取 PDF 文件中的书签页码?

How can I get bookmarks page number in a PDF file with Apache PdfBox?

我已经获得了书签,但我需要知道这些书签在 PDF 中的位置。 (书签 1 = 第 1 页,...,书签 54 = 第 72 页等)。任何人都可以帮助我吗?感谢支持

PDDocument doc = PDDocument.load( ... );
PDDocumentOutline root = doc.getDocumentCatalog().getDocumentOutline();
PDOutlineItem item = root.getFirstChild();
  while( item != null )
  {
      System.out.println( "Item:" + item.getTitle() );
      item = item.getNextSibling();
  }

摘自源代码下载的PrintBookmarks.java示例:

if (item.getDestination() instanceof PDPageDestination)
{
    PDPageDestination pd = (PDPageDestination) item.getDestination();
    System.out.println("Destination page: " + (pd.retrievePageNumber() + 1));
}
else if (item.getDestination() instanceof PDNamedDestination)
{
    PDPageDestination pd = document.getDocumentCatalog().findNamedDestinationPage((PDNamedDestination) item.getDestination());
    if (pd != null)
    {
        System.out.println("Destination page: " + (pd.retrievePageNumber() + 1));
    }
}

if (item.getAction() instanceof PDActionGoTo)
{
    PDActionGoTo gta = (PDActionGoTo) item.getAction();
    if (gta.getDestination() instanceof PDPageDestination)
    {
        PDPageDestination pd = (PDPageDestination) gta.getDestination();
        System.out.println("Destination page: " + (pd.retrievePageNumber() + 1));
    }
    else if (gta.getDestination() instanceof PDNamedDestination)
    {
        PDPageDestination pd = document.getDocumentCatalog().findNamedDestinationPage((PDNamedDestination) gta.getDestination());
        if (pd != null)
        {
            System.out.println("Destination page: " + (pd.retrievePageNumber() + 1));
        }
    }
}