3.4.  Bar Code

Overview

PDFUnit can validate bar codes within PDF documents. It uses ZXing as a parser. Detailed information about ZXing can be found on the project's home page https://github.com/zxing/zxing.

The text of a bar code can be validated using the following methods:

// Entry to all bar code validations:
.hasImage().withBarcode()

// Validate text in bar codes:
...withBarcode().containing(..)
...withBarcode().containing(.., WhitespaceProcessing)
...withBarcode().endingWith(..)
...withBarcode().equalsTo(..)
...withBarcode().equalsTo(.., WhitespaceProcessing)
...withBarcode().matchingRegex(..)
...withBarcode().startingWith(..)

// Validate text in bar code in image region:
...withBarcodeInRegion(imageRegion).containing(..)
...withBarcodeInRegion(imageRegion).containing(.., WhitespaceProcessing)
...withBarcodeInRegion(imageRegion).endingWith(..)
...withBarcodeInRegion(imageRegion).equalsTo(..)
...withBarcodeInRegion(imageRegion).equalsTo(.., WhitespaceProcessing)
...withBarcodeInRegion(imageRegion).matchingRegex(..)
...withBarcodeInRegion(imageRegion).startingWith(..)

// Compare with another bar code:
...withBarcode().matchingImage(..)

ZXing detects bar code formats automatically. The following formats can be used in PDFUnit tests:

// 1D bar codes, supported by PDFUnit/ZXing:
CODE_128
CODE_39
CODE_93
EAN_13
EAN_8
CODABAR
UPC_A
UPC_E
UPC_EAN_EXTENSION
ITF

Example - Compare Bar Code with Text

The next examples use these two bar code samples:

The first bar code contains the text 'TESTING BARCODES WITH PDFUNIT'. The second contains the text 'hello, world - 1234567890'.

@Test
public void hasBarCode_Code3of9() throws Exception {
  String filename = "documentUnderTest.pdf";
  
  int leftX  =   0;
  int upperY =  70;
  int width  = 210;
  int height =  30;
  PageRegion pageRegion = new PageRegion(leftX, upperY, width, height);
  
  AssertThat.document(filename)
            .restrictedTo(FIRST_PAGE)
            .restrictedTo(pageRegion)
            .hasImage()
            .withBarcode()
            .containing("TESTING BARCODES WITH PDFUNIT")
  ;
}
@Test
public void hasBarCode_Code128() throws Exception {
  String filename = "documentUnderTest.pdf";
  
  int leftX  =   0;
  int upperY = 105;
  int width  = 210;
  int height =  30;
  PageRegion pageRegion = new PageRegion(leftX, upperY, width, height);
  
  AssertThat.document(filename)
            .restrictedTo(FIRST_PAGE)
            .restrictedTo(pageRegion)
            .hasImage()
            .withBarcode()
            .containing("hello, world")
  ;
}

When multiple images exist in a defined region, each image must pass the test. Default whitespace processing when searching text in bar code is NORMALIZE, but whitespace processing can be controlled using a method parameter.

The internal used bar code parser ZXing supports many, but not all, bar code formats. So PDFUnit provides an external interface to plug in customer specific bar code parsers. This interface is documented separately. You can request it by writing an email to info[at]pdfunit.com.

Example - Compare a Bar Code with an Image

A bar code within a PDF document can also be compared with a bar code image from a file:

@Test
public void hasBarCodeMatchingImage() throws Exception {
  String filename = "documentUnderTest.pdf";
  
  int leftX  =   0;
  int upperY = 105;
  int width  = 210;
  int height =  30;
  PageRegion pageRegion = new PageRegion(leftX, upperY, width, height);
  
  String expectedImageFilename = "images/barcode-128.png";
  AssertThat.document(filename)
            .restrictedTo(FIRST_PAGE)
            .restrictedTo(pageRegion)
            .hasImage()
            .withBarcode()
            .matchingImage(expectedImageFilename);
  ;
}

Important: For the image comparison to work, the type of the external image (PNG or TIFF, for example) must match the type of the bar code image in the PDF document.