Return to Snippet

Revision: 72669
at October 13, 2017 01:30 by markt22


Initial Code
/**
 * @param args the command line arguments
 * @throws java.io.IOException
 */
public static void main(String[] args) throws IOException {
    PDDocument document = PDDocument.load(new File(""));

    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;
    }

    System.out.println(numImages);
}

static class CountImages extends PDFGraphicsStreamEngine {
    public int numImages = 0;
    private final Set<COSStream> duplicates = new HashSet<>();

    protected CountImages(PDPage page) throws IOException
    {
        super(page);
    }

    @Override
    public void appendRectangle(Point2D pd, Point2D pd1, Point2D pd2, Point2D pd3) throws IOException {
    }

    @Override
    public void drawImage(PDImage pdImage) throws IOException {
        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
    public void clip(int i) throws IOException {
    }

    @Override
    public void moveTo(float f, float f1) throws IOException {
    }

    @Override
    public void lineTo(float f, float f1) throws IOException {
    }

    @Override
    public void curveTo(float f, float f1, float f2, float f3, float f4, float f5) throws IOException {
    }

    @Override
    public Point2D getCurrentPoint() throws IOException {
        return new Point2D.Float(0, 0);
    }

    @Override
    public void closePath() throws IOException {
    }

    @Override
    public void endPath() throws IOException {
    }

    @Override
    public void strokePath() throws IOException {
    }

    @Override
    public void fillPath(int i) throws IOException {
    }

    @Override
    public void fillAndStrokePath(int i) throws IOException {
    }

    @Override
    public void shadingFill(COSName cosn) throws IOException {
    }
}

Initial URL

                                

Initial Description
Opens a PDF document and counts the number of images used on all pages.

Initial Title
PDFBox: Counting the number of images in a document

Initial Tags

                                

Initial Language
Java