简体   繁体   中英

How to find page to jump to. I using PDFBox 2.0.0 and PDActionGoTo

I using pdfbox 2.0 and i find all link internal in a file pdf. however i don't know how to find page that to jump to ? I has a internal link "Chapter 1" in page 20 , this is file that i had used jquery cookbook.pdf

public static void main(String[] args) throws IOException {
            PDDocument doc = null;
                doc = PDDocument.load(new File("D:\\demoConverter\\input\\jQuery_Cookbook.pdf"));
                int pageNum = 0;
                doc.getNumberOfPages();
                for (PDPage page : doc.getPages()) {
                    pageNum++;
                    List<PDAnnotation> annotations = page.getAnnotations();
                    for (PDAnnotation annot : annotations) {
                        if (annot instanceof PDAnnotationLink) {
                            PDAnnotationLink link = (PDAnnotationLink) annot;

                            PDAction action = link.getAction();
                            if (action instanceof PDActionURI) {
                                PDActionURI uri = (PDActionURI) action;
                            }
                            // internal link
                            if (action instanceof PDActionGoTo) {
                               PDDestination  destination = ((PDActionGoTo)action).getDestination();
                               // how to get page will to jump to
                           }
                        } 
                    }
                }

Such destinations can either be a page destination or a named destination. Page destinations are easy, named destinations require a lookup in the name table.

if (action instanceof PDActionGoTo) 
{
     PDPageDestination pageDestination = null;
     PDPage page = null;
     PDDestination destination = ((PDActionGoTo)action).getDestination();
     if (destination instanceof PDPageDestination)
     {
         pageDestination = (PDPageDestination) destination;
         page = pageDestination.getPage();
     }
     else if (destination instanceof PDNamedDestination)
     {
         pageDestination = doc.getDocumentCatalog().findNamedDestinationPage((PDNamedDestination) destination);
         if (pageDestination == null)
         {
             return null;
         }
         page = pageDestination.getPage();
     }
     else
     {
         // error handling
     }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM