@Test
public void clean() {

    PDDocument doc = new PDDocument();
    COSDictionary rootRes = new COSDictionary();
    COSDictionary rootXobjects = new COSDictionary();
    rootXobjects.setItem(COSName.getPDFName("keepMe"), imageStream);
    rootXobjects.setItem(COSName.getPDFName("keepMeForm"), formStream);
    rootXobjects.setItem(COSName.getPDFName("discardMe"), image);
    rootXobjects.setItem(COSName.getPDFName("discardMeForm"), form);
    rootRes.setItem(COSName.XOBJECT, rootXobjects);
    COSDictionary rootFonts = new COSDictionary();
    rootFonts.setItem(COSName.getPDFName("keepMe"), inUseFont);
    rootFonts.setItem(COSName.getPDFName("discardMe"), new COSDictionary());
    rootRes.setItem(COSName.FONT, rootFonts);
    COSDictionary pageRes = new COSDictionary();
    COSDictionary pageXobjects = new COSDictionary();
    pageXobjects.setItem(COSName.getPDFName("keepMe"), imageStream);
    pageXobjects.setItem(COSName.getPDFName("keepMeForm"), formStream);
    pageXobjects.setItem(COSName.getPDFName("discardMe"), image);
    pageXobjects.setItem(COSName.getPDFName("discardMeToo"), image);
    pageXobjects.setItem(COSName.getPDFName("discardMeForm"), form);
    pageRes.setItem(COSName.XOBJECT, pageXobjects);
    COSDictionary pageFonts = new COSDictionary();
    pageFonts.setItem(COSName.getPDFName("keepMe"), inUseFont);
    pageFonts.setItem(COSName.getPDFName("discardMe"), new COSDictionary());
    pageFonts.setItem(COSName.getPDFName("discardMeToo"), new COSDictionary());
    pageRes.setItem(COSName.FONT, pageFonts);
    doc.getDocumentCatalog().getPages().getCOSObject().setItem(COSName.RESOURCES, rootRes);
    PDPage page0 = new PDPage();
    PDPage page1 = new PDPage();
    page1.getCOSObject().setItem(COSName.RESOURCES, pageRes);
    doc.getDocumentCatalog().getPages().add(page0);
    doc.getDocumentCatalog().getPages().add(page1);
    assertTrue(page0.getResources().isImageXObject(COSName.getPDFName("keepMe")));
    assertTrue(page0.getResources().isImageXObject(COSName.getPDFName("discardMe")));
    assertTrue(page0.getResources().isFormXObject(COSName.getPDFName("keepMeForm")));
    assertTrue(page0.getResources().isFormXObject(COSName.getPDFName("discardMeForm")));
    assertTrue(page1.getResources().isImageXObject(COSName.getPDFName("keepMe")));
    assertTrue(page1.getResources().isFormXObject(COSName.getPDFName("keepMeForm")));
    assertTrue(page1.getResources().isImageXObject(COSName.getPDFName("discardMe")));
    assertTrue(page1.getResources().isImageXObject(COSName.getPDFName("discardMeToo")));
    assertTrue(page1.getResources().isFormXObject(COSName.getPDFName("discardMeForm")));
    COSDictionary page0fonts = page0.getResources().getCOSObject().getDictionaryObject(COSName.FONT,
            COSDictionary.class);
    assertNotNull(page0fonts.getDictionaryObject(COSName.getPDFName("keepMe")));
    assertNotNull(page0fonts.getDictionaryObject(COSName.getPDFName("discardMe")));
    COSDictionary page1fonts = page1.getResources().getCOSObject().getDictionaryObject(COSName.FONT,
            COSDictionary.class);
    assertNotNull(page1fonts.getDictionaryObject(COSName.getPDFName("keepMe")));
    assertNotNull(page1fonts.getDictionaryObject(COSName.getPDFName("discardMe")));
    assertNotNull(page1fonts.getDictionaryObject(COSName.getPDFName("discardMeToo")));
    new ResourceDictionaryCleaner().accept(doc);
    assertTrue(page0.getResources().isImageXObject(COSName.getPDFName("keepMe")));
    assertTrue(page0.getResources().isFormXObject(COSName.getPDFName("keepMeForm")));
    assertFalse(page0.getResources().isImageXObject(COSName.getPDFName("discardMe")));
    assertFalse(page0.getResources().isFormXObject(COSName.getPDFName("discardMeForm")));
    assertTrue(page1.getResources().isImageXObject(COSName.getPDFName("keepMe")));
    assertTrue(page1.getResources().isFormXObject(COSName.getPDFName("keepMeForm")));
    assertFalse(page1.getResources().isImageXObject(COSName.getPDFName("discardMe")));
    assertFalse(page1.getResources().isImageXObject(COSName.getPDFName("discardMeToo")));
    assertFalse(page1.getResources().isFormXObject(COSName.getPDFName("discardMeForm")));
    page0fonts = page0.getResources().getCOSObject().getDictionaryObject(COSName.FONT, COSDictionary.class);
    assertNotNull(page0fonts.getDictionaryObject(COSName.getPDFName("keepMe")));
    assertNull(page0fonts.getDictionaryObject(COSName.getPDFName("discardMe")));
    page1fonts = page1.getResources().getCOSObject().getDictionaryObject(COSName.FONT, COSDictionary.class);
    assertNotNull(page1fonts.getDictionaryObject(COSName.getPDFName("keepMe")));
    assertNull(page1fonts.getDictionaryObject(COSName.getPDFName("discardMe")));
    assertNull(page1fonts.getDictionaryObject(COSName.getPDFName("discardMeToo")));
}