@Test public void discardBeads() throws IOException { try (PDDocument document = PDFParser.parse(SeekableSources.inMemorySeekableSourceFrom( getClass().getClassLoader().getResourceAsStream("pdf/shared_resource_dic_w_images.pdf")))) { PDPage page = document.getPage(0); page.setThreadBeads(Arrays.asList(new PDThreadBead())); assertFalse(page.getThreadBeads().isEmpty()); PDPage copy = new PageCopier(false).copyOf(page); assertTrue(copy.getThreadBeads().isEmpty()); } }
public PDPage copyOf(PDPage page) { PDPage copy = new PDPage(page.getCOSObject().duplicate()); copy.setCropBox(page.getCropBox()); copy.setMediaBox(page.getMediaBox()); copy.setResources(page.getResources()); copy.setRotation(page.getRotation()); copy.getCOSObject().removeItem(COSName.B); COSArray annots = page.getCOSObject().getDictionaryObject(COSName.ANNOTS, COSArray.class); if (nonNull(annots)) { COSArray cleanedAnnotationsCopy = new COSArray(); annots.stream().map(COSBase::getCOSObject).filter(d -> d instanceof COSDictionary) .map(d -> (COSDictionary) d).map(COSDictionary::duplicate).forEach(a -> { a.removeItem(COSName.P); a.removeItem(COSName.DEST); a.removeItem(COSName.getPDFName("Popup")); a.removeItem(COSName.PARENT); if (ofNullable(a.getDictionaryObject(COSName.A, COSDictionary.class)) .map(d -> d.containsKey(COSName.D)).orElse(false)) { a.removeItem(COSName.A); } cleanedAnnotationsCopy.add(a); }); copy.getCOSObject().setItem(COSName.ANNOTS, cleanedAnnotationsCopy); } if (optimize) { COSDictionary resources = ofNullable(copy.getResources().getCOSObject()).map(COSDictionary::duplicate) .orElseGet(COSDictionary::new); copy.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(copy); cleaner.clean(copy); } duplicatePageStreams(page, copy); copy.sanitizeDictionary(); return copy; }
PageCopier { public PDPage copyOf(PDPage page) { PDPage copy = new PDPage(page.getCOSObject().duplicate()); copy.setCropBox(page.getCropBox()); copy.setMediaBox(page.getMediaBox()); copy.setResources(page.getResources()); copy.setRotation(page.getRotation()); copy.getCOSObject().removeItem(COSName.B); COSArray annots = page.getCOSObject().getDictionaryObject(COSName.ANNOTS, COSArray.class); if (nonNull(annots)) { COSArray cleanedAnnotationsCopy = new COSArray(); annots.stream().map(COSBase::getCOSObject).filter(d -> d instanceof COSDictionary) .map(d -> (COSDictionary) d).map(COSDictionary::duplicate).forEach(a -> { a.removeItem(COSName.P); a.removeItem(COSName.DEST); a.removeItem(COSName.getPDFName("Popup")); a.removeItem(COSName.PARENT); if (ofNullable(a.getDictionaryObject(COSName.A, COSDictionary.class)) .map(d -> d.containsKey(COSName.D)).orElse(false)) { a.removeItem(COSName.A); } cleanedAnnotationsCopy.add(a); }); copy.getCOSObject().setItem(COSName.ANNOTS, cleanedAnnotationsCopy); } if (optimize) { COSDictionary resources = ofNullable(copy.getResources().getCOSObject()).map(COSDictionary::duplicate) .orElseGet(COSDictionary::new); copy.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(copy); cleaner.clean(copy); } duplicatePageStreams(page, copy); copy.sanitizeDictionary(); return copy; } }
PageCopier { public PDPage copyOf(PDPage page) { PDPage copy = new PDPage(page.getCOSObject().duplicate()); copy.setCropBox(page.getCropBox()); copy.setMediaBox(page.getMediaBox()); copy.setResources(page.getResources()); copy.setRotation(page.getRotation()); copy.getCOSObject().removeItem(COSName.B); COSArray annots = page.getCOSObject().getDictionaryObject(COSName.ANNOTS, COSArray.class); if (nonNull(annots)) { COSArray cleanedAnnotationsCopy = new COSArray(); annots.stream().map(COSBase::getCOSObject).filter(d -> d instanceof COSDictionary) .map(d -> (COSDictionary) d).map(COSDictionary::duplicate).forEach(a -> { a.removeItem(COSName.P); a.removeItem(COSName.DEST); a.removeItem(COSName.getPDFName("Popup")); a.removeItem(COSName.PARENT); if (ofNullable(a.getDictionaryObject(COSName.A, COSDictionary.class)) .map(d -> d.containsKey(COSName.D)).orElse(false)) { a.removeItem(COSName.A); } cleanedAnnotationsCopy.add(a); }); copy.getCOSObject().setItem(COSName.ANNOTS, cleanedAnnotationsCopy); } if (optimize) { COSDictionary resources = ofNullable(copy.getResources().getCOSObject()).map(COSDictionary::duplicate) .orElseGet(COSDictionary::new); copy.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(copy); cleaner.clean(copy); } duplicatePageStreams(page, copy); copy.sanitizeDictionary(); return copy; } PageCopier(boolean optimize); }
PageCopier { public PDPage copyOf(PDPage page) { PDPage copy = new PDPage(page.getCOSObject().duplicate()); copy.setCropBox(page.getCropBox()); copy.setMediaBox(page.getMediaBox()); copy.setResources(page.getResources()); copy.setRotation(page.getRotation()); copy.getCOSObject().removeItem(COSName.B); COSArray annots = page.getCOSObject().getDictionaryObject(COSName.ANNOTS, COSArray.class); if (nonNull(annots)) { COSArray cleanedAnnotationsCopy = new COSArray(); annots.stream().map(COSBase::getCOSObject).filter(d -> d instanceof COSDictionary) .map(d -> (COSDictionary) d).map(COSDictionary::duplicate).forEach(a -> { a.removeItem(COSName.P); a.removeItem(COSName.DEST); a.removeItem(COSName.getPDFName("Popup")); a.removeItem(COSName.PARENT); if (ofNullable(a.getDictionaryObject(COSName.A, COSDictionary.class)) .map(d -> d.containsKey(COSName.D)).orElse(false)) { a.removeItem(COSName.A); } cleanedAnnotationsCopy.add(a); }); copy.getCOSObject().setItem(COSName.ANNOTS, cleanedAnnotationsCopy); } if (optimize) { COSDictionary resources = ofNullable(copy.getResources().getCOSObject()).map(COSDictionary::duplicate) .orElseGet(COSDictionary::new); copy.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(copy); cleaner.clean(copy); } duplicatePageStreams(page, copy); copy.sanitizeDictionary(); return copy; } PageCopier(boolean optimize); PDPage copyOf(PDPage page); }
PageCopier { public PDPage copyOf(PDPage page) { PDPage copy = new PDPage(page.getCOSObject().duplicate()); copy.setCropBox(page.getCropBox()); copy.setMediaBox(page.getMediaBox()); copy.setResources(page.getResources()); copy.setRotation(page.getRotation()); copy.getCOSObject().removeItem(COSName.B); COSArray annots = page.getCOSObject().getDictionaryObject(COSName.ANNOTS, COSArray.class); if (nonNull(annots)) { COSArray cleanedAnnotationsCopy = new COSArray(); annots.stream().map(COSBase::getCOSObject).filter(d -> d instanceof COSDictionary) .map(d -> (COSDictionary) d).map(COSDictionary::duplicate).forEach(a -> { a.removeItem(COSName.P); a.removeItem(COSName.DEST); a.removeItem(COSName.getPDFName("Popup")); a.removeItem(COSName.PARENT); if (ofNullable(a.getDictionaryObject(COSName.A, COSDictionary.class)) .map(d -> d.containsKey(COSName.D)).orElse(false)) { a.removeItem(COSName.A); } cleanedAnnotationsCopy.add(a); }); copy.getCOSObject().setItem(COSName.ANNOTS, cleanedAnnotationsCopy); } if (optimize) { COSDictionary resources = ofNullable(copy.getResources().getCOSObject()).map(COSDictionary::duplicate) .orElseGet(COSDictionary::new); copy.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(copy); cleaner.clean(copy); } duplicatePageStreams(page, copy); copy.sanitizeDictionary(); return copy; } PageCopier(boolean optimize); PDPage copyOf(PDPage page);  }