Barcode Fields

Taking the data-entry out of printed forms

Acrobat 7.0 added the ablity to add a Barcode field to a form (although the details for how to do it weren't added to the PDF specification until ISO32000-2, aka PDF 2.0).

Barcode fields are typically set to update whenever any other fields in the form change; the idea being when your user prints out the form, signs it and sends it back, you can then scan the barcode to automatically extract the form data.

Now you can do the same with our PDF Library using the new FormBarCode class.

Creating Barcode Fields

Let's say you have a form with two fields, name and address, which your customer will fill out, print, sign and send back. To make use of the barcode field, create one which will update it's value based on changes to the other fields. You can do this with JavaScript: we'll make the barcode value a simple tab-separated representation of the other fields:

FormBarCode barcode = new FormBarCode();
barcode.addAnnotation(page, x1, y1, x2, y2);
String js = "event.value = this.getField('name').value +'\t' + this.getField('address').value;";
PDFAction action = PDFAction.formJavaScript(js);
barcode.setAction(Event.OTHERCHANGE, action);
pdf.getForm().put("barcode", barcode);

The JavaScript will run when any field in the form is changed - just set event.value to whatever you need to. Here's an example containing a barcode so you can see what it looks like. Try editing it in Acrobat and notice how the barcode changes.

Parsing Barcode fields

Assuming your customer has printed out the form, signed it and sent it back, how do you get the data out of the barcode? That's beyond the remit of our PDF Library (there's no PDF involved here - your scanned image is probably a TIFF). Any Barcode scanner will do, but we recommend the ZXing library which works nicely.

To test either print out the form from Acrobat or our PDF Viewer, or save the PDF from Acrobat and convert it to a bitmap image using our PDFToImage example:

java PDFToImage --format png example.pdf

Once you've followed the installation instructions for ZXing, scanning a barcode is as simple as

java -cp javase/javase.jar:core/core.jar example.png