Set<PDAnnotation> keptAnnotations = new LinkedHashSet<>(); for (PDAnnotation annotation : page.getAnnotations()) { PDAnnotation mapped = annotationsLookup.lookup(annotation); if (nonNull(mapped)) { keptAnnotations.add(mapped); relevantPages.lookup(page).setAnnotations(new ArrayList<>(keptAnnotations)); } catch (IOException e) { LOG.warn("Failed to process annotations for page", e);
Set<PDAnnotation> keptAnnotations = new LinkedHashSet<>(); for (PDAnnotation annotation : page.getAnnotations()) { PDAnnotation mapped = annotationsLookup.lookup(annotation); if (nonNull(mapped)) { keptAnnotations.add(mapped); relevantPages.lookup(page).setAnnotations(new ArrayList<>(keptAnnotations)); } catch (IOException e) { LOG.warn("Failed to process annotations for page", e);
private PDAnnotation duplicate(PDAnnotation annotation, LookupTable<PDPage> relevantPages) { PDPage p = annotation.getPage(); PDAnnotation duplicate = PDAnnotation.createAnnotation(annotation.getCOSObject().duplicate()); if (nonNull(p)) { duplicate.setPage(relevantPages.lookup(p)); LOG.trace("Updated annotation page reference with the looked up page"); } annotationsLookup.addLookupEntry(annotation, duplicate); return duplicate; }
private PDAnnotation duplicate(PDAnnotation annotation, LookupTable<PDPage> relevantPages) { PDPage p = annotation.getPage(); PDAnnotation duplicate = PDAnnotation.createAnnotation(annotation.getCOSObject().duplicate()); if (nonNull(p)) { duplicate.setPage(relevantPages.lookup(p)); LOG.trace("Updated annotation page reference with the looked up page"); } annotationsLookup.addLookupEntry(annotation, duplicate); return duplicate; }
/** * @param origin * @return a clone of the origin leaf if its page destination falls in the range of the needed pages. Cloned item destination is offset by the given offset. */ private Optional<PDOutlineItem> cloneLeafIfNeeded(PDOutlineItem origin, LookupTable<PDPage> pagesLookup) { return toPageDestination(origin, document.getDocumentCatalog()).flatMap(d -> { PDPage mapped = pagesLookup.lookup(d.getPage()); if (mapped != null) { PDOutlineItem retVal = new PDOutlineItem(); copyOutlineDictionary(origin, retVal); retVal.setDestination(clonePageDestination(d, mapped)); return Optional.of(retVal); } return Optional.empty(); }); } }
/** * @param origin * @return a clone of the origin leaf if its page destination falls in the range of the needed pages. Cloned item destination is offset by the given offset. */ private Optional<PDOutlineItem> cloneLeafIfNeeded(PDOutlineItem origin, LookupTable<PDPage> pagesLookup) { return toPageDestination(origin, document.getDocumentCatalog()).flatMap(d -> { PDPage mapped = pagesLookup.lookup(d.getPage()); if (mapped != null) { PDOutlineItem retVal = new PDOutlineItem(); copyOutlineDictionary(origin, retVal); retVal.setDestination(clonePageDestination(d, mapped)); return Optional.of(retVal); } return Optional.empty(); }); } }
private void processNonLinkAnnotation(LookupTable<PDPage> relevantPages, Set<PDAnnotation> keptAnnotations, PDAnnotation annotation) { PDPage p = annotation.getPage(); if (isNull(p) || relevantPages.hasLookupFor(p)) { PDAnnotation duplicate = duplicate(annotation, relevantPages); if (duplicate instanceof PDAnnotationMarkup) { PDAnnotationPopup popup = ((PDAnnotationMarkup) duplicate).getPopup(); if (nonNull(popup)) { COSName subtype = popup.getCOSObject().getCOSName(COSName.SUBTYPE); if (COSName.POPUP.equals(subtype)) { PDAnnotationPopup popupDuplicate = ofNullable( (PDAnnotationPopup) annotationsLookup.lookup(popup)) .orElseGet(() -> (PDAnnotationPopup) duplicate(popup, relevantPages)); ((PDAnnotationMarkup) duplicate).setPopup(popupDuplicate); if (nonNull(popupDuplicate.getParent())) { popupDuplicate.setParent((PDAnnotationMarkup) duplicate); LOG.trace("Popup parent annotation updated"); } keptAnnotations.add(popupDuplicate); } else { ((PDAnnotationMarkup) duplicate).setPopup(null); LOG.warn("Removed Popup annotation of unexpected subtype {}", subtype); } } } keptAnnotations.add(duplicate); } }
private Optional<PDOutlineItem> cloneNode(PDOutlineItem node, LookupTable<PDPage> pagesLookup) { if (node.hasChildren()) { final PDOutlineItem clone = new PDOutlineItem(); for (PDOutlineItem current : node.children()) { if(current.equals(node)) { LOG.warn("Outline item has a child pointing to the parent, skipping at cloning"); } else { cloneNode(current, pagesLookup).ifPresent(clonedChild -> { clone.addLast(clonedChild); }); } } Optional<PDPageDestination> pageDestination = toPageDestination(node, document.getDocumentCatalog()); Optional<PDPage> destinationPage = pageDestination.map(PDPageDestination::getPage) .map(p -> pagesLookup.lookup(p)); if (clone.hasChildren() || destinationPage.isPresent()) { copyOutlineDictionary(node, clone); destinationPage.ifPresent(p -> clone.setDestination(clonePageDestination(pageDestination.get(), p))); return Optional.of(clone); } return Optional.empty(); } return cloneLeafIfNeeded(node, pagesLookup); }
private Optional<PDOutlineItem> cloneNode(PDOutlineItem node, LookupTable<PDPage> pagesLookup) { if (node.hasChildren()) { final PDOutlineItem clone = new PDOutlineItem(); for (PDOutlineItem current : node.children()) { if(current.equals(node)) { LOG.warn("Outline item has a child pointing to the parent, skipping at cloning"); } else { cloneNode(current, pagesLookup).ifPresent(clonedChild -> { clone.addLast(clonedChild); }); } } Optional<PDPageDestination> pageDestination = toPageDestination(node, document.getDocumentCatalog()); Optional<PDPage> destinationPage = pageDestination.map(PDPageDestination::getPage) .map(p -> pagesLookup.lookup(p)); if (clone.hasChildren() || destinationPage.isPresent()) { copyOutlineDictionary(node, clone); destinationPage.ifPresent(p -> clone.setDestination(clonePageDestination(pageDestination.get(), p))); return Optional.of(clone); } return Optional.empty(); } return cloneLeafIfNeeded(node, pagesLookup); }
private void processNonLinkAnnotation(LookupTable<PDPage> relevantPages, Set<PDAnnotation> keptAnnotations, PDAnnotation annotation) { PDPage p = annotation.getPage(); if (isNull(p) || relevantPages.hasLookupFor(p)) { PDAnnotation duplicate = duplicate(annotation, relevantPages); if (duplicate instanceof PDAnnotationMarkup) { PDAnnotationPopup popup = ((PDAnnotationMarkup) duplicate).getPopup(); if (nonNull(popup)) { COSName subtype = popup.getCOSObject().getCOSName(COSName.SUBTYPE); if (COSName.POPUP.equals(subtype)) { PDAnnotationPopup popupDuplicate = ofNullable( (PDAnnotationPopup) annotationsLookup.lookup(popup)) .orElseGet(() -> (PDAnnotationPopup) duplicate(popup, relevantPages)); ((PDAnnotationMarkup) duplicate).setPopup(popupDuplicate); if (nonNull(popupDuplicate.getParent())) { popupDuplicate.setParent((PDAnnotationMarkup) duplicate); LOG.trace("Popup parent annotation updated"); } keptAnnotations.add(popupDuplicate); } else { ((PDAnnotationMarkup) duplicate).setPopup(null); LOG.warn("Removed Popup annotation of unexpected subtype {}", subtype); } } } keptAnnotations.add(duplicate); } }
private void processLinkAnnotation(LookupTable<PDPage> relevantPages, Set<PDAnnotation> keptAnnotations, PDAnnotationLink annotation) throws IOException { PDDestination destination = getDestinationFrom(annotation); if (destination instanceof PDPageDestination) { PDPage destPage = relevantPages.lookup(((PDPageDestination) destination).getPage()); if (nonNull(destPage)) { // relevant page dest PDAnnotationLink duplicate = (PDAnnotationLink) duplicate(annotation, relevantPages); duplicate.getCOSObject().removeItem(COSName.A); PDPageDestination newDestination = (PDPageDestination) PDDestination .create(((PDPageDestination) destination).getCOSObject().duplicate()); newDestination.setPage(destPage); duplicate.setDestination(newDestination); keptAnnotations.add(duplicate); } else { LOG.trace("Removing not relevant link annotation"); } } else { // not a page dest keptAnnotations.add(duplicate(annotation, relevantPages)); } }
private void processLinkAnnotation(LookupTable<PDPage> relevantPages, Set<PDAnnotation> keptAnnotations, PDAnnotationLink annotation) throws IOException { PDDestination destination = getDestinationFrom(annotation); if (destination instanceof PDPageDestination) { PDPage destPage = relevantPages.lookup(((PDPageDestination) destination).getPage()); if (nonNull(destPage)) { // relevant page dest PDAnnotationLink duplicate = (PDAnnotationLink) duplicate(annotation, relevantPages); duplicate.getCOSObject().removeItem(COSName.A); PDPageDestination newDestination = (PDPageDestination) PDDestination .create(((PDPageDestination) destination).getCOSObject().duplicate()); newDestination.setPage(destPage); duplicate.setDestination(newDestination); keptAnnotations.add(duplicate); } else { LOG.trace("Removing not relevant link annotation"); } } else { // not a page dest keptAnnotations.add(duplicate(annotation, relevantPages)); } }
@Test public void lookup() { victim.addLookupEntry("this", "that"); victim.addLookupEntry("this1", "that1"); victim.addLookupEntry("this2", "that2"); assertEquals("that2", victim.lookup("this2")); }