ResourceDictionaryCleaner implements Consumer<PDDocument> { @Override public void accept(PDDocument p) { LOG.debug("Cleaning resource dictionaries from unused resources"); clean(p.getPages().streamNodes()); }  @Override void accept(PDDocument p); void clean(PDPage page);  }
@Test public void noExceptionMissingResources() { PDDocument doc = new PDDocument(); PDPage page0 = new PDPage(); page0.getCOSObject().setItem(COSName.RESOURCES, null); doc.getDocumentCatalog().getPages().add(page0); new ResourceDictionaryCleaner().accept(doc); }
@Test public void noExceptionMissingXObjects() { PDDocument doc = new PDDocument(); PDPage page0 = new PDPage(); COSDictionary rootRes = new COSDictionary(); page0.getCOSObject().setItem(COSName.RESOURCES, rootRes); rootRes.setItem(COSName.XOBJECT, null); doc.getDocumentCatalog().getPages().add(page0); new ResourceDictionaryCleaner().accept(doc); }
@Test public void noExceptionMissingFonts() { PDDocument doc = new PDDocument(); PDPage page0 = new PDPage(); COSDictionary rootRes = new COSDictionary(); page0.getCOSObject().setItem(COSName.RESOURCES, rootRes); rootRes.setItem(COSName.FONT, null); doc.getDocumentCatalog().getPages().add(page0); new ResourceDictionaryCleaner().accept(doc); }