How to select pdf page using bookmark in pdf box?

淺唱寂寞╮ 提交于 2019-12-20 05:10:16

问题


Sorry i am new to PDF box and was looking for a solution on how to get a specific pdf page using the bookmark name? Like the below code snippet am trying to loop all the pages but stuck to link the book mark with the page i need. Can any one please help?

import java.awt.print.PrinterException;
import java.io.File;
import java.io.IOException;
import org.apache.pdfbox.cos.COSObject;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.encryption.InvalidPasswordException;
import org.apache.pdfbox.pdmodel.interactive.documentnavigation.destination.PDPageFitWidthDestination;
import org.apache.pdfbox.pdmodel.interactive.documentnavigation.outline.PDDocumentOutline;
import org.apache.pdfbox.pdmodel.interactive.documentnavigation.outline.PDOutlineItem;
import org.apache.pdfbox.pdmodel.interactive.documentnavigation.outline.PDOutlineNode;

public class PDFLoader {

    public static void main(String[] args) throws InvalidPasswordException, IOException, PrinterException {


          File file = new File("d:\\pdf\\sample.pdf"); 
          PDDocument document = PDDocument.load(file); 
          PDPage page01 = document.getDocumentCatalog().getPages().get(0);
          PDPage page02 = document.getDocumentCatalog().getPages().get(1);

          PDDocumentOutline outline =  document.getDocumentCatalog().getDocumentOutline();
          printBookmark(outline, "");
          PDDocument doc = new PDDocument();
          doc.addPage(page01);
          doc.addPage(page02);
          doc.save("d:\\pdf\\newSample.pdf");
          doc.close();

    }



public static PDPage getBookmark(PDOutlineNode bookmark, String indentation) throws IOException
    {
        PDOutlineItem current = bookmark.getFirstChild();
        while (current != null)

        {
            System.out.println(indentation + current.getTitle());



            if (current.getAction() instanceof PDActionGoTo)
            {
                PDActionGoTo gta = (PDActionGoTo) current.getAction();
                if (gta.getDestination() instanceof PDPageDestination)
                {

                    if(current.getTitle().equals("MyBookMark")){


                        PDPageDestination pd = (PDPageDestination) current.getDestination();
                        System.out.println("Destination page: " + pd.retrievePageNumber());
                        return pd.getPage();

                        }
                }
            }

            getBookmark(current, indentation + "    ");
            current = current.getNextSibling();

            }


        return null;
    }







//Stack Trace
    Exception in thread "main" java.lang.NullPointerException
    at com.mypackage.PDFLoader.getBookmark(PDFLoader.java:67)
    at com.mypackage.PDFLoader.main(PDFLoader.java:40)

回答1:


It turns out that in your PDF the page destination is not in the bookmark's destination entry, but in the bookmark's action entry (yes, PDF makes it possible to have two ways to do the same thing). Add this to your code:

if (current.getDestination() instanceof PDPageDestination)
{
    PDPageDestination pd = (PDPageDestination) current.getDestination();
    System.out.println("Destination page: " + (pd.retrievePageNumber() + 1));
    return pd.getPage();
}
if (current.getAction() instanceof PDActionGoTo)
{
    PDActionGoTo gta = (PDActionGoTo) current.getAction();
    if (gta.getDestination() instanceof PDPageDestination)
    {
        PDPageDestination pd = (PDPageDestination) gta.getDestination();
        System.out.println("Destination page: " + (pd.retrievePageNumber() + 1));
        return pd.getPage();
    }
}


来源:https://stackoverflow.com/questions/44982486/how-to-select-pdf-page-using-bookmark-in-pdf-box

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!