/ Published in: Java
Opens a PDF document and counts the number of images used on all pages.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
/** * @param args the command line arguments * @throws java.io.IOException */ int numImages = 0; for (int i = 0; i < document.getNumberOfPages(); i++) { PDPage page = document.getPage(i); CountImages countImages = new CountImages(page); countImages.processPage(page); numImages += countImages.numImages; } } static class CountImages extends PDFGraphicsStreamEngine { public int numImages = 0; private final Set<COSStream> duplicates = new HashSet<>(); { super(page); } @Override } @Override if (pdImage instanceof PDImageXObject) { PDImageXObject xobject = (PDImageXObject)pdImage; if (duplicates.contains(xobject.getCOSObject()) == false) { numImages++; duplicates.add(xobject.getCOSObject()); } } else { numImages++; //means its an inline image } } @Override } @Override } @Override } @Override } @Override } @Override } @Override } @Override } @Override } @Override } @Override } }