Last active
September 9, 2017 17:44
-
-
Save ozero/8573328 to your computer and use it in GitHub Desktop.
read every pagelink info from all annotations/actions, with apache pdfbox.
(current page#, linkto page# and annotation coordinates.)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public static void main(String[] args) { | |
| try { | |
| File f = new File("../source.pdf"); | |
| PDDocument pd = PDDocument.load(f); | |
| //PDFTextStripper textStripper = new PDFTextStripper(); | |
| PDDocumentCatalog cat = pd.getDocumentCatalog(); | |
| @SuppressWarnings("unchecked") | |
| List<PDPage> pages = cat.getAllPages(); | |
| Iterator<PDPage> iter = pages.iterator(); | |
| int pagenum = 1; | |
| while (iter.hasNext()) { | |
| PDPage page = (PDPage) iter.next(); | |
| //PDResources resources = page.getResources(); | |
| List<PDAnnotation> l = page.getAnnotations(); | |
| Iterator<PDAnnotation> it = l.iterator(); | |
| while (it.hasNext()) { | |
| PDAnnotation annotation = (PDAnnotation) it.next(); | |
| if (annotation instanceof PDAnnotationLink) { | |
| // System.out.println(annotation); | |
| PDAnnotationLink link = (PDAnnotationLink) annotation; | |
| PDAction action = link.getAction(); | |
| if(!(action instanceof PDActionGoTo)){ | |
| continue; | |
| } | |
| PDActionGoTo go = (PDActionGoTo)action; | |
| PDPageDestination pdest = (PDPageDestination) go.getDestination(); | |
| if(pdest.getPageNumber() > -1){ | |
| continue; | |
| } | |
| int pdestnum = pages.indexOf(pdest.getPage()) + 1; | |
| PDRectangle rect = link.getRectangle(); | |
| System.out.println("page: "+pagenum | |
| +" ,pgn: "+pdestnum | |
| +" ,base: "+rect.getLowerLeftX() + " " +rect.getLowerLeftY() | |
| +" ,size: "+rect.getWidth() +" "+rect.getHeight() | |
| ); | |
| } | |
| } | |
| pagenum++; | |
| } | |
| pd.close(); | |
| } catch (Exception ex) { | |
| ex.printStackTrace(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment