@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()); } }
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 { 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 { 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); }
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); }
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);  }