@Test public void cleanIsEmpty() { assertTrue(victim.isEmpty()); victim.addLookupEntry("this", "that"); assertFalse(victim.isEmpty()); victim.clear(); assertTrue(victim.isEmpty()); }
@Test public void first() { victim.addLookupEntry("this", "that"); victim.addLookupEntry("this1", "that1"); victim.addLookupEntry("this2", "that2"); assertEquals("that", victim.first()); }
@Test public void lookup() { victim.addLookupEntry("this", "that"); victim.addLookupEntry("this1", "that1"); victim.addLookupEntry("this2", "that2"); assertEquals("that2", victim.lookup("this2")); }
@Test public void values() { victim.addLookupEntry("this", "that"); victim.addLookupEntry("this1", "that1"); victim.addLookupEntry("this2", "that2"); assertEquals(3, victim.values().size()); }
LookupTable<PDPage> pagesLookup = new LookupTable<>(); LOG.debug("Adding pages and back pages"); for (PDPage current : sourceDocumentHandler.getPages()) { executionContext().assertTaskNotCancelled(); pagesLookup.addLookupEntry(current, destinationDocument.importPage(current)); pageCounter++; if (pageCounter % parameters.getStep() == 0) { clipSignatures(annotationsLookup.values());
/** * @return the first item or null if the table is empty. */ public I first() { if (!isEmpty()) { return oldToNew.values().iterator().next(); } return null; }
BiConsumer<PDField, LookupTable<PDField>> createNonTerminalField) { AcroFormUtils.mergeDefaults(originalForm, form); LookupTable<PDField> fieldsLookup = new LookupTable<>(); .collect(Collectors.toList())); annotationsLookup.values().stream().filter(a -> a instanceof PDAnnotationWidget) .filter(a -> a.getCOSObject().containsKey(COSName.T)).map(a -> (PDAnnotationWidget) a).collect(toList()) .forEach(w -> {
public void addLookupEntry(PDPage current, PDPage importPage) { lookups.addLookupEntry(current, importPage); }
public void optimize() { LOG.trace("Optimizing document"); ResourcesHitter hitter = new ResourcesHitter(); pagesLookup.values().forEach(p -> { // each page must have it's own resource dic and it's own xobject and font name dic // so we don't optimize shared resource dic or xobjects/fonts name dictionaries COSDictionary resources = ofNullable(p.getResources().getCOSObject()).map(COSDictionary::duplicate) .orElseGet(COSDictionary::new); // resources are cached in the PDPage so make sure they are replaced p.setResources(new PDResources(resources)); ofNullable(resources.getDictionaryObject(COSName.XOBJECT, COSDictionary.class)).filter(Objects::nonNull) .map(COSDictionary::duplicate).ifPresent(d -> resources.setItem(COSName.XOBJECT, d)); ofNullable(resources.getDictionaryObject(COSName.FONT, COSDictionary.class)).filter(Objects::nonNull) .map(COSDictionary::duplicate).ifPresent(d -> resources.setItem(COSName.FONT, d)); hitter.accept(p); }); new ResourceDictionaryCleaner().accept(destinationDocument.getUnderlyingPDDocument()); }
/** * @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); } }
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); relevantPages.lookup(page).setAnnotations(new ArrayList<>(keptAnnotations)); } catch (IOException e) { LOG.warn("Failed to process annotations for page", e);
@Override public void close() throws IOException { handler.close(); lookups.clear(); }
private PDOutlineItem updateOneEntryPerDoc(String sourceName, LookupTable<PDPage> pagesLookup) { if (StringUtils.isNotBlank(sourceName)) { LOG.debug("Adding outline entry for {}", sourceName); PDOutlineItem item = new PDOutlineItem(); item.setTitle(removeExtension(sourceName)); PDPageFitDestination destination = new PDPageFitDestination(); destination.setPage(pagesLookup.first()); item.setDestination(destination); outline.addLast(item); return item; } LOG.warn("Unable to create an outline item for a source with blank name"); return null; }
@Before public void setUp() { victim = new LookupTable<>(); }
LookupTable<PDPage> pagesLookup = new LookupTable<>(); LOG.debug("Adding pages and back pages"); for (PDPage current : sourceDocumentHandler.getPages()) { executionContext().assertTaskNotCancelled(); pagesLookup.addLookupEntry(current, destinationDocument.importPage(current)); pageCounter++; if (pageCounter % parameters.getStep() == 0) { clipSignatures(annotationsLookup.values());
/** * @return the first item or null if the table is empty. */ public I first() { if (!isEmpty()) { return oldToNew.values().iterator().next(); } return null; }
@Test public void keys() { victim.addLookupEntry("this", "that"); victim.addLookupEntry("this1", "that1"); victim.addLookupEntry("this", "that2"); assertEquals(2, victim.values().size()); } }
BiConsumer<PDField, LookupTable<PDField>> createNonTerminalField) { AcroFormUtils.mergeDefaults(originalForm, form); LookupTable<PDField> fieldsLookup = new LookupTable<>(); .collect(Collectors.toList())); annotationsLookup.values().stream().filter(a -> a instanceof PDAnnotationWidget) .filter(a -> a.getCOSObject().containsKey(COSName.T)).map(a -> (PDAnnotationWidget) a).collect(toList()) .forEach(w -> {
public void addLookupEntry(PDPage current, PDPage importPage) { lookups.addLookupEntry(current, importPage); }