SamboxOutlineLevelsHandler implements org.sejda.model.outline.OutlineLevelsHandler { @Override public OutlinePageDestinations getPageDestinationsForLevel(int level) { OutlinePageDestinations destinations = new OutlinePageDestinations(); OutlineUtils.getFlatOutline(document).stream().filter(i -> i.level == level).filter(i -> isNotBlank(i.title)) .filter(i -> titleMatchingPattern.matcher(i.title).matches()) .forEach(i -> destinations.addPage(i.page, i.title)); return destinations; } SamboxOutlineLevelsHandler(PDDocument document, String matchingTitleRegEx); @Override OutlinePageDestinations getPageDestinationsForLevel(int level); @Override OutlineExtractPageDestinations getExtractPageDestinations(int level, boolean includePageAfter);  }
@Test public void getPageNumbersAtOutlineLevel() throws IOException { try (PDDocument document = fromResource("pdf/test_outline.pdf")) { org.sejda.model.outline.OutlineLevelsHandler victim = new SamboxOutlineLevelsHandler(document, null); assertTrue(victim.getPageDestinationsForLevel(4).getPages().isEmpty()); assertEquals(2, victim.getPageDestinationsForLevel(2).getPages().size()); assertEquals(1, victim.getPageDestinationsForLevel(3).getPages().size()); } }
@Test public void getPageNumbersAtOutlineLevelNoOutline() throws IOException { try (PDDocument document = fromResource("pdf/test_no_outline.pdf")) { org.sejda.model.outline.OutlineLevelsHandler victim = new SamboxOutlineLevelsHandler(document, null); assertEquals(0, victim.getPageDestinationsForLevel(2).getPages().size()); } }
@Test public void getPageNumbersAtOutlineLevelMatching() throws IOException { try (PDDocument document = fromResource("pdf/test_outline.pdf")) { org.sejda.model.outline.OutlineLevelsHandler victim = new SamboxOutlineLevelsHandler(document, "(.+)page(.*)"); assertEquals(1, victim.getPageDestinationsForLevel(2).getPages().size()); } }
@Test public void parentsNoPageDests() { 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); org.sejda.model.outline.OutlineLevelsHandler victim = new SamboxOutlineLevelsHandler(document, null); assertEquals(0, victim.getPageDestinationsForLevel(2).getPages().size()); assertEquals(1, victim.getPageDestinationsForLevel(3).getPages().size()); }
@Test public void newLinesInBookmarks() throws IOException { try (PDDocument document = fromResource("pdf/new_line_in_bookmarks.pdf")) { org.sejda.model.outline.OutlineLevelsHandler victim = new SamboxOutlineLevelsHandler(document, null); assertEquals(3, victim.getPageDestinationsForLevel(1).getPages().size()); } }