Как получить номер страницы закладок в файле PDF с помощью Apache PdfBox? - PullRequest
1 голос
/ 24 июня 2019

Я уже получил закладки, но мне нужно знать, где находятся эти закладки в 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();
  }

1 Ответ

0 голосов
/ 24 июня 2019

Выдержка из примера 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));
        }
    }
}
...