AnnotationsDistiller { public LookupTable<PDAnnotation> retainRelevantAnnotations(LookupTable<PDPage> relevantPages) { LOG.debug("Filtering annotations"); for (PDPage page : relevantPages.keys()) { try { Set<PDAnnotation> keptAnnotations = new LinkedHashSet<>(); for (PDAnnotation annotation : page.getAnnotations()) { PDAnnotation mapped = annotationsLookup.lookup(annotation); if (nonNull(mapped)) { keptAnnotations.add(mapped); } else { if (annotation instanceof PDAnnotationLink) { processLinkAnnotation(relevantPages, keptAnnotations, (PDAnnotationLink) annotation); } else { processNonLinkAnnotation(relevantPages, keptAnnotations, annotation); } } } relevantPages.lookup(page).setAnnotations(new ArrayList<>(keptAnnotations)); } catch (IOException e) { LOG.warn("Failed to process annotations for page", e); } } return annotationsLookup; } AnnotationsDistiller(PDDocument document); LookupTable<PDAnnotation> retainRelevantAnnotations(LookupTable<PDPage> relevantPages);  }
@Test public void orderIsKept() { PDAnnotationSquareCircle one = new PDAnnotationSquareCircle(SUB_TYPE_SQUARE); one.setAnnotationName("one"); PDAnnotationText two = new PDAnnotationText(); two.setAnnotationName("two"); PDAnnotationLine three = new PDAnnotationLine(); three.setAnnotationName("three"); PDAnnotationSquareCircle four = new PDAnnotationSquareCircle(SUB_TYPE_SQUARE); four.setAnnotationName("four"); PDAnnotationLine five = new PDAnnotationLine(); five.setAnnotationName("five"); PDAnnotationSquareCircle six = new PDAnnotationSquareCircle(SUB_TYPE_SQUARE); six.setAnnotationName("six"); List<PDAnnotation> annotations = Arrays.asList(one, two, three, four, five, six); oldPage.setAnnotations(annotations); PDDocument doc = new PDDocument(); doc.addPage(oldPage); new AnnotationsDistiller(doc).retainRelevantAnnotations(lookup); List<PDAnnotation> annots = newPage.getAnnotations(); assertEquals(6, annots.size()); assertThat(annots.stream().map(PDAnnotation::getAnnotationName).collect(Collectors.toList()), Matchers.contains(annotations.stream().map(PDAnnotation::getAnnotationName) .map(d -> Matchers.equalTo(d)).collect(Collectors.toList()))); }
@Test public void noLinks() { List<PDAnnotation> annotations = Arrays.asList(new PDAnnotationText()); oldPage.setAnnotations(annotations); PDDocument doc = new PDDocument(); doc.addPage(oldPage); new AnnotationsDistiller(doc).retainRelevantAnnotations(lookup); assertEquals(1, newPage.getAnnotations().size()); }
@Test public void noLinks_PageNotRelevant() { PDAnnotationText annotation = new PDAnnotationText(); annotation.setPage(new PDPage()); List<PDAnnotation> annotations = Arrays.asList(annotation); oldPage.setAnnotations(annotations); PDDocument doc = new PDDocument(); doc.addPage(oldPage); LookupTable<PDAnnotation> annotationsLookup = new AnnotationsDistiller(doc).retainRelevantAnnotations(lookup); assertEquals(0, newPage.getAnnotations().size()); assertTrue(annotationsLookup.isEmpty()); }
@Test public void noLinks_OnePageNotRelevantOneRelevant() { PDPage destPage = new PDPage(); PDAnnotationText annotation = new PDAnnotationText(); annotation.setPage(destPage); PDAnnotationText annotation2 = new PDAnnotationText(); annotation2.setPage(oldPage); List<PDAnnotation> annotations = Arrays.asList(annotation, annotation2); oldPage.setAnnotations(annotations); PDDocument doc = new PDDocument(); doc.addPage(oldPage); LookupTable<PDAnnotation> annotationsLookup = new AnnotationsDistiller(doc).retainRelevantAnnotations(lookup); assertEquals(annotationsLookup.lookup(annotation2), newPage.getAnnotations().get(0)); }
@Test public void linksNoGoTo() { PDAnnotationLink annotation = new PDAnnotationLink(); annotation.setAction(new PDActionJavaScript()); List<PDAnnotation> annotations = Arrays.asList(annotation); oldPage.setAnnotations(annotations); PDDocument doc = new PDDocument(); doc.addPage(oldPage); LookupTable<PDAnnotation> annotationsLookup = new AnnotationsDistiller(doc).retainRelevantAnnotations(lookup); assertEquals(annotationsLookup.lookup(annotation), newPage.getAnnotations().get(0)); }
@Test public void links_PageNotRelevant() { PDPage destPage = new PDPage(); PDAnnotationLink annotation = new PDAnnotationLink(); PDPageDestination dest = new PDPageFitDestination(); dest.setPage(destPage); annotation.setDestination(dest); List<PDAnnotation> annotations = Arrays.asList(annotation); oldPage.setAnnotations(annotations); PDDocument doc = new PDDocument(); doc.addPage(oldPage); LookupTable<PDAnnotation> annotationsLookup = new AnnotationsDistiller(doc).retainRelevantAnnotations(lookup); assertEquals(0, newPage.getAnnotations().size()); assertTrue(annotationsLookup.isEmpty()); }
@Test public void links_PageRelevant() { PDAnnotationLink annotation = new PDAnnotationLink(); PDPageDestination dest = new PDPageFitDestination(); dest.setPage(oldPage); annotation.setDestination(dest); List<PDAnnotation> annotations = Arrays.asList(annotation); oldPage.setAnnotations(annotations); PDDocument doc = new PDDocument(); doc.addPage(oldPage); LookupTable<PDAnnotation> annotationsLookup = new AnnotationsDistiller(doc).retainRelevantAnnotations(lookup); assertEquals(annotationsLookup.lookup(annotation), newPage.getAnnotations().get(0)); }
@Test public void links_OnePageNotRelevantOneRelevant() { PDPage destPage = new PDPage(); PDAnnotationLink annotation = new PDAnnotationLink(); PDPageDestination dest = new PDPageFitDestination(); dest.setPage(destPage); annotation.setDestination(dest); PDAnnotationLink annotation2 = new PDAnnotationLink(); PDPageDestination dest2 = new PDPageFitDestination(); dest2.setPage(oldPage); annotation2.setDestination(dest2); List<PDAnnotation> annotations = Arrays.asList(annotation, annotation2); oldPage.setAnnotations(annotations); PDDocument doc = new PDDocument(); doc.addPage(oldPage); LookupTable<PDAnnotation> annotationsLookup = new AnnotationsDistiller(doc).retainRelevantAnnotations(lookup); assertEquals(annotationsLookup.lookup(annotation2), newPage.getAnnotations().get(0)); }
@Test public void linksGoTo_PageNotRelevant() { PDPage destPage = new PDPage(); PDAnnotationLink annotation = new PDAnnotationLink(); PDPageDestination dest = new PDPageFitDestination(); dest.setPage(destPage); PDActionGoTo action = new PDActionGoTo(); action.setDestination(dest); annotation.setAction(action); List<PDAnnotation> annotations = Arrays.asList(annotation); oldPage.setAnnotations(annotations); PDDocument doc = new PDDocument(); doc.addPage(oldPage); LookupTable<PDAnnotation> annotationsLookup = new AnnotationsDistiller(doc).retainRelevantAnnotations(lookup); assertEquals(0, newPage.getAnnotations().size()); assertTrue(annotationsLookup.isEmpty()); }
@Test public void linksGoTo_PageRelevant() { PDAnnotationLink annotation = new PDAnnotationLink(); PDPageDestination dest = new PDPageFitDestination(); dest.setPage(oldPage); PDActionGoTo action = new PDActionGoTo(); action.setDestination(dest); annotation.setAction(action); List<PDAnnotation> annotations = Arrays.asList(annotation); oldPage.setAnnotations(annotations); PDDocument doc = new PDDocument(); doc.addPage(oldPage); LookupTable<PDAnnotation> annotationsLookup = new AnnotationsDistiller(doc).retainRelevantAnnotations(lookup); assertEquals(annotationsLookup.lookup(annotation), newPage.getAnnotations().get(0)); }
@Test public void linksGoTo_OnePageNotRelevantOneRelevant() { PDPage destPage = new PDPage(); PDAnnotationLink annotation = new PDAnnotationLink(); PDPageDestination dest = new PDPageFitDestination(); dest.setPage(destPage); PDActionGoTo action = new PDActionGoTo(); action.setDestination(dest); annotation.setAction(action); PDAnnotationLink annotation2 = new PDAnnotationLink(); PDPageDestination dest2 = new PDPageFitDestination(); dest2.setPage(oldPage); PDActionGoTo action2 = new PDActionGoTo(); action2.setDestination(dest2); annotation2.setAction(action2); List<PDAnnotation> annotations = Arrays.asList(annotation, annotation2); oldPage.setAnnotations(annotations); PDDocument doc = new PDDocument(); doc.addPage(oldPage); LookupTable<PDAnnotation> annotationsLookup = new AnnotationsDistiller(doc).retainRelevantAnnotations(lookup); assertEquals(annotationsLookup.lookup(annotation2), newPage.getAnnotations().get(0)); }
@Test public void links_NamedPageNotRelevant() throws IOException { PDPage destPage = new PDPage(); PDAnnotationLink annotation = new PDAnnotationLink(); PDNamedDestination namedDest = new PDNamedDestination(COSName.AESV3); PDPageDestination dest = new PDPageFitDestination(); dest.setPage(destPage); annotation.setDestination(namedDest); List<PDAnnotation> annotations = Arrays.asList(annotation); oldPage.setAnnotations(annotations); PDDocument doc = mock(PDDocument.class); PDDocumentCatalog catalog = mock(PDDocumentCatalog.class); when(doc.getDocumentCatalog()).thenReturn(catalog); when(catalog.findNamedDestinationPage(any(PDNamedDestination.class))).thenReturn(dest); doc.addPage(oldPage); LookupTable<PDAnnotation> annotationsLookup = new AnnotationsDistiller(doc).retainRelevantAnnotations(lookup); assertEquals(0, newPage.getAnnotations().size()); assertTrue(annotationsLookup.isEmpty()); }
@Test public void links_NamedPageRelevant() throws IOException { PDAnnotationLink annotation = new PDAnnotationLink(); PDNamedDestination namedDest = new PDNamedDestination(COSName.AESV3); PDPageDestination dest = new PDPageFitDestination(); dest.setPage(oldPage); annotation.setDestination(namedDest); List<PDAnnotation> annotations = Arrays.asList(annotation); oldPage.setAnnotations(annotations); PDDocument doc = mock(PDDocument.class); PDDocumentCatalog catalog = mock(PDDocumentCatalog.class); when(doc.getDocumentCatalog()).thenReturn(catalog); when(catalog.findNamedDestinationPage(namedDest)).thenReturn(dest); doc.addPage(oldPage); LookupTable<PDAnnotation> annotationsLookup = new AnnotationsDistiller(doc).retainRelevantAnnotations(lookup); assertEquals(annotationsLookup.lookup(annotation), newPage.getAnnotations().get(0)); }
@Test public void popupRelevant() throws IOException { try (PDDocument doc = PDFParser.parse(SeekableSources.inMemorySeekableSourceFrom( getClass().getClassLoader().getResourceAsStream("pdf/popup_annotation.pdf")))) { PDPage firstOrigin = doc.getPage(0); PDPage firstNew = new PDPage(); lookup.addLookupEntry(firstOrigin, firstNew); new AnnotationsDistiller(doc).retainRelevantAnnotations(lookup); List<PDAnnotation> annotations = firstNew.getAnnotations(); assertFalse(annotations.isEmpty()); List<PDAnnotationMarkup> parent = annotations.stream().filter(a -> a instanceof PDAnnotationPopup) .map(a -> ((PDAnnotationPopup) a).getParent()).collect(Collectors.toList()); assertEquals(1, parent.size()); assertTrue(annotations.contains(parent.get(0))); } }
@Test public void removePopupIfGarbage() throws IOException { try (PDDocument doc = PDFParser.parse(SeekableSources.inMemorySeekableSourceFrom( getClass().getClassLoader().getResourceAsStream("pdf/popup_annotation.pdf")))) { PDPage firstOrigin = doc.getPage(0); firstOrigin.getAnnotations().stream().filter(a -> !(a instanceof PDAnnotationPopup)) .forEach(a -> a.getCOSObject().setItem(COSName.POPUP, new PDPage())); PDPage firstNew = new PDPage(); lookup.addLookupEntry(firstOrigin, firstNew); new AnnotationsDistiller(doc).retainRelevantAnnotations(lookup); List<PDAnnotation> annotations = firstNew.getAnnotations(); assertFalse(annotations.isEmpty()); PDAnnotationMarkup parent = annotations.stream().filter(a -> a instanceof PDAnnotationMarkup) .map(a -> ((PDAnnotationMarkup) a)).findFirst().get(); assertNull(parent.getPopup()); } }
@Test public void popupRelevantRevertedOrder() throws IOException { try (PDDocument doc = PDFParser.parse(SeekableSources.inMemorySeekableSourceFrom( getClass().getClassLoader().getResourceAsStream("pdf/popup_annotation.pdf")))) { PDPage firstOrigin = doc.getPage(0); List<PDAnnotation> annots = firstOrigin.getAnnotations(); Collections.reverse(annots); firstOrigin.setAnnotations(annots); PDPage firstNew = new PDPage(); lookup.addLookupEntry(firstOrigin, firstNew); new AnnotationsDistiller(doc).retainRelevantAnnotations(lookup); List<PDAnnotation> annotations = firstNew.getAnnotations(); assertFalse(annotations.isEmpty()); List<PDAnnotationMarkup> parent = annotations.stream().filter(a -> a instanceof PDAnnotationPopup) .map(a -> ((PDAnnotationPopup) a).getParent()).collect(Collectors.toList()); assertEquals(1, parent.size()); assertTrue(annotations.contains(parent.get(0))); } }
@Test public void pagesHaveTheSameAnnotations() { PDAnnotationLink annotation = new PDAnnotationLink(); PDPageDestination dest = new PDPageFitDestination(); dest.setPage(oldPage); annotation.setDestination(dest); PDPage secondOld = new PDPage(); PDPage secondNew = new PDPage(); lookup.addLookupEntry(secondOld, secondNew); List<PDAnnotation> annotations = Arrays.asList(annotation, new PDAnnotationText()); oldPage.setAnnotations(annotations); secondOld.setAnnotations(annotations); PDDocument doc = new PDDocument(); doc.addPage(oldPage); doc.addPage(secondOld); new AnnotationsDistiller(doc).retainRelevantAnnotations(lookup); assertEquals(oldPage.getAnnotations().size(), newPage.getAnnotations().size()); assertEquals(secondOld.getAnnotations().size(), secondNew.getAnnotations().size()); }