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