OutlineUtils { public static Set<Integer> getOutlineLevelsWithPageDestination(PDDocument document) { return getFlatOutline(document).stream().map(i -> i.level).collect(Collectors.toSet()); } 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 outlineLevels() throws IOException { try (PDDocument doc = PDFParser.parse( SeekableSources.inMemorySeekableSourceFrom(getClass().getResourceAsStream("/pdf/test_outline.pdf")))) { Set<Integer> levels = OutlineUtils.getOutlineLevelsWithPageDestination(doc); assertEquals(3, levels.size()); assertTrue(levels.contains(1)); assertTrue(levels.contains(2)); assertTrue(levels.contains(3)); } }
@Test public void outlineLevelsParentHasNoPageDest() { PDPage page1 = new PDPage(); PDDocument document = new PDDocument(); document.addPage(page1); PDDocumentOutline outlines = new PDDocumentOutline(); PDOutlineItem root = new PDOutlineItem(); root.setTitle("title"); PDOutlineItem child = new PDOutlineItem(); child.setTitle("child"); PDOutlineItem child2 = new PDOutlineItem(); child2.setTitle("child2"); child2.setDestination(page1); child.addFirst(child2); root.addLast(child); outlines.addFirst(root); document.getDocumentCatalog().setDocumentOutline(outlines); Set<Integer> levels = OutlineUtils.getOutlineLevelsWithPageDestination(document); assertEquals(1, levels.size()); assertTrue(levels.contains(3)); }