This example shows how to read barcode values from images in PDFs using the Apache PDFBox and ZXing libraries.

The code originates from a PDFBox enhancement contributed by David Keller.


import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;

public class PdDocumentBarcodeScanner {

    private int maximumBlankPixelDelimiterCount = 20;

    public static void main (String[] args) throws Exception {
        String filename = "mail_with_barcode.pdf";
        if (args.length > 0)
            filename = args[0];

        FileInputStream pdfInputStream = new FileInputStream(new File(filename));
        PDDocument doc = PDDocument.load(pdfInputStream);
        pdfInputStream.close();

        List<PdPageBarcodeScanner> pageScanners = new ArrayList<PdPageBarcodeScanner>();
        scan(doc, pageScanners);
        displayResults(pageScanners);
    }

    private static void scan (PDDocument doc, List<PdPageBarcodeScanner> pageScanners) throws IOException {
        int pageNumber = 0;
        for (PDPage pdPage : doc.getDocumentCatalog().getPages()) {
            PdPageBarcodeScanner pageScanner =
                    new PdPageBarcodeScanner(pdPage, pageNumber, maximumBlankPixelDelimiterCount);
            pageScanners.add(pageScanner);
            pageScanner.scan();
            pageNumber++;
        }
    }

    private static void displayResults (List<PdPageBarcodeScanner> pageScanners) {
        for (PdPageBarcodeScanner pageScanner : pageScanners) {
            pageScanner.displayResults();
        }
    }
}

XZing is available from the Maven repositories, so here's a Gradle build file for running the code using gradle run. The PdPageBarcodeScanner class is also needed. You can test the code with this PDF.


apply plugin: 'java'
apply plugin: 'application'

repositories {
    mavenCentral()
}

sourceCompatibility = 1.8
targetCompatibility = 1.8

application {
    mainClassName = "PdDocumentBarcodeScanner"
}

dependencies {
    implementation 'org.apache.pdfbox:pdfbox:2.0.+'
    implementation 'com.google.zxing:javase:3.4.+'
}


CodeSnippets