OutlineUtils { public static Optional<PDPageDestination> toPageDestination(PDOutlineItem current, PDDocumentCatalog catalog) { try { PDDestination dest = current.getDestination(); if (dest == null) { PDAction outlineAction = current.getAction(); if (outlineAction instanceof PDActionGoTo) { dest = ((PDActionGoTo) outlineAction).getDestination(); } } if (dest instanceof PDNamedDestination && catalog != null) { dest = catalog.findNamedDestinationPage((PDNamedDestination) dest); } if (dest instanceof PDPageDestination) { return Optional.of((PDPageDestination) dest); } } catch (IOException e) { LOG.warn("Unable to get outline item destination ", e); } return Optional.empty(); } 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 toPageDestinationEmpty() { PDOutlineItem victim = new PDOutlineItem(); assertFalse(OutlineUtils.toPageDestination(victim, null).isPresent()); }
@Test public void toPageDestinationAction() { PDPageFitDestination destination = new PDPageFitDestination(); PDPage page = new PDPage(); destination.setPage(page); PDActionGoTo action = new PDActionGoTo(); action.setDestination(destination); PDOutlineItem victim = new PDOutlineItem(); victim.setAction(action); assertEquals(destination.getPage(), OutlineUtils.toPageDestination(victim, null).get().getPage()); }
@Test public void toPageDestinationDestination() { PDPageFitDestination destination = new PDPageFitDestination(); destination.setPageNumber(5); PDOutlineItem victim = new PDOutlineItem(); victim.setDestination(destination); PDDocumentCatalog catalog = mock(PDDocumentCatalog.class); assertEquals(5, OutlineUtils.toPageDestination(victim, catalog).get().getPageNumber()); }
@Test public void toPageDestinationDestinationNullCatalog() { PDPageFitDestination dest = new PDPageFitDestination(); dest.setPageNumber(5); PDNamedDestination destination = new PDNamedDestination(); destination.setNamedDestination("ChuckNorris"); PDOutlineItem victim = new PDOutlineItem(); victim.setDestination(destination); PDDocumentCatalog catalog = null; assertFalse(OutlineUtils.toPageDestination(victim, catalog).isPresent()); }
@Test public void toPageDestinationNamedDestinationNullNames() { PDNamedDestination destination = new PDNamedDestination(); PDOutlineItem victim = new PDOutlineItem(); victim.setDestination(destination); assertFalse(OutlineUtils.toPageDestination(victim, null).isPresent()); }
@Test public void toPageDestinationNamedDestination() throws IOException { PDPageFitDestination dest = new PDPageFitDestination(); dest.setPageNumber(5); PDNamedDestination destination = new PDNamedDestination(); destination.setNamedDestination("ChuckNorris"); PDOutlineItem victim = new PDOutlineItem(); victim.setDestination(destination); PDDocumentCatalog catalog = mock(PDDocumentCatalog.class); when(catalog.findNamedDestinationPage(any())).thenReturn(dest); assertEquals(dest, OutlineUtils.toPageDestination(victim, catalog).get()); }