OutlineUtils { public static PDPage resolvePageDestination(PDPageDestination destination, PDDocument document) { PDPage page = destination.getPage(); if (isNull(page) && destination.getPageNumber() >= 0) { try { LOG.debug("Found page number in page destination, expected a page reference"); return document.getPage(destination.getPageNumber()); } catch (PageNotFoundException e) { LOG.warn( "Unable to resolve page destination pointing to page {} (a page reference was expected, a number was found)", destination.getPageNumber()); } } return page; } private  OutlineUtils(); static Set<Integer> getOutlineLevelsWithPageDestination(PDDocument document); static Optional<PDPageDestination> toPageDestination(PDOutlineItem current, PDDocumentCatalog catalog); static PDPageXYZDestination pageDestinationFor(PDPage page); static PDPageDestination clonePageDestination(PDPageDestination dest, PDPage destPage); static PDPage resolvePageDestination(PDPageDestination destination, PDDocument document); static void copyOutlineDictionary(PDOutlineItem from, PDOutlineItem to); static List<OutlineItem> getFlatOutline(PDDocument document); static void printOutline(PDDocument document);  }
@Test public void resolvePageDestination() throws IOException { PDPage page1 = new PDPage(); PDPage page2 = new PDPage(); PDDocument document = new PDDocument(); document.addPage(page1); document.addPage(page2); PDDocumentOutline outlines = new PDDocumentOutline(); PDOutlineItem root = new PDOutlineItem(); root.setTitle("title"); PDOutlineItem child = new PDOutlineItem(); child.setTitle("child"); child.setDestination(page2); PDOutlineItem child2 = new PDOutlineItem(); child2.setTitle("child2"); child2.setDestination(page1); child2.getCOSObject().getDictionaryObject(COSName.DEST, COSArray.class).set(0, COSInteger.ONE); root.addFirst(child); root.addLast(child2); outlines.addFirst(root); document.getDocumentCatalog().setDocumentOutline(outlines); assertNotNull(OutlineUtils.resolvePageDestination((PDPageDestination) child2.getDestination(), document)); assertNotNull(OutlineUtils.resolvePageDestination((PDPageDestination) child.getDestination(), document)); PDPageDestination negative = new PDPageXYZDestination(); negative.setPageNumber(-1); assertNull(OutlineUtils.resolvePageDestination(negative, document)); PDPageDestination notFound = new PDPageXYZDestination(); notFound.setPageNumber(10); assertNull(OutlineUtils.resolvePageDestination(notFound, document)); }