1

Related to this: How do I know if PDF pages are color or black-and-white?

I need to know if the current page is a color or black&white one using java.

I tried using PDFBox, doing the following:

public void checkColor(final File pdfFile) {
    PDDocument document;
    try {
        document = PDDocument.load(pdfFile);

        List<PDPage> pages = document.getDocumentCatalog().getAllPages();
        for (int i = 0; i < pages.size(); i++) {
            System.out.println();
            PDPage page = pages.get(i);
            //BufferedImage image = page.convertToImage();
            BufferedImage image = page.convertToImage(BufferedImage.TYPE_INT_RGB, 72);

            parseColor(image, i);
        }

        printPages();

    } catch (IOException ex) {
        Logger.getLogger(PdfBoxParser.class.getName()).log(Level.SEVERE, null, ex);
    }
}
public static boolean isColorPixel(final int pixel) {
    //took from some post from stackoverflow
    System.out.print(pixel);
    System.out.print(",");
    int alpha = (pixel >> 24) & 0xff;
    int red = (pixel >> 16) & 0xff;
    int green = (pixel >> 8) & 0xff;
    int blue = (pixel) & 0xff;
    // gray: R = G = B
    boolean gray = (red == green && green == blue);
    return gray;
}

protected void parseColor(BufferedImage pImage, int pPageNumber) {
    int thresholdColor = Main.COLOR_THRESHOLD_PER_PAGE;
    for (int h = 0; h < pImage.getHeight(); h++) {
        for (int w = 0; w < pImage.getWidth(); w++) {
            int pixel = pImage.getRGB(w,h);
            boolean color = Main.isColorPixel(pixel);
            if (color) {
                thresholdColor--;
                if (thresholdColor == 0) {
                //do something like store this page number...
                .
                .
                .

Problem is, i tried various PDFs (ebooks, one-page pdfs, etc) and every "final int pixel" returns "-1", along with a bunch of warnings (org.apache.pdfbox.util.PDFStreamEngine processOperator unsupported/disabled operation: i/EMC/BMC/ri). Can this be solved?

Community
  • 1
  • 1
StackUnder
  • 252
  • 8
  • 18

0 Answers0