barcode4J and PDFJT

barcode4J and PDFJT

Barcode Image

Update : If you are looking for how to create appearances for barcodes in PDF Forms with Datalogics PDF Java Toolkit, take a look at our new article!

We were recently asked how to add bar code images to a PDF document using the PDF Java Toolkit.

 

The First step is easy: generate yourself a bar code image. The PDF Java Toolkit (PDFJT) doesn’t provide this functionality so to generate a bar code image, we used Barcode4J from http://sourceforge.net/projects/barcode4j/

Out of the box, Barcode4J knows how to generate about 15 different types of bar codes, which can be tweaked in different ways, but for demonstration purposes, we’re going to use it to generate different types of bar codes mostly using the built-in defaults. You will need to add the barcode4j.jar to your classpath, but because we are using Barcode4j’s bean interface, that is the only jar you need for the following code:

// Create the bar code bean
DefaultBarcodeClassResolver resolver = new DefaultBarcodeClassResolver();
Class clazz = resolver.resolveBean(bcType);
AbstractBarcodeBean bean = (AbstractBarcodeBean) clazz.newInstance();

// makes a dot/module exactly 2pts wide
bean.setModuleWidth(UnitConv.in2mm(2.0f / 72f));

boolean antiAlias = false;
int orientation = 0;

// Set up the canvas provider to create a monochrome bitmap
BitmapCanvasProvider canvas = new BitmapCanvasProvider(dpi,
BufferedImage.TYPE_BYTE_BINARY, antiAlias, orientation);

// Generate the barcode
bean.generateBarcode(canvas, message);

// Signal end of generation
canvas.finish();

// return the generated bitmap
return canvas.getBufferedImage();

the bcType is a string identifying the type of barcode to generate. It could be “upc-a”, “ean13”, “pdf417”, “datamatrix” or any one of the 23 strings recognized by the DefaultBarcodeClassResolver, but note that the barcode type will put restrictions on the content and length of the message you want to encode as a bar code. Datamatrix can encode ISO-8859-1 text, pdf417 can encode text using the original msdos code page, others are restricted to strings of numbers of fixed lengths; consult the barcode4J documentation for more details.

But once we have a BufferedImage from barcode4J, we have what we need to stick it on a PDF page using PDFJT:

pdfDoc = SampleFileServices.loadPDFDocument(inputFile.getPath());

// Get the pageTree, then the specified page
PDFPageTree pageTree = pdfDoc.requirePages();
PDFPage page = pageTree.getPage(pageNum);

BufferedImage barcode = generateBarcode();

PDFXObjectImage image = ImageManager.getPDFImage(barcode, pdfDoc);

image.setColorSpace(PDFColorSpaceDeviceGray.newInstance(pdfDoc));

ASMatrix bcMatrix = calcImageMatrix(barcode, xpos, ypos);

ImageManager.insertImageInPDF(image, page, null, bcMatrix);

SampleFileServices.savePDFDocument(outputFilePath, pdfDoc);

We set the image’s color space to DeviceGray because otherwise the bi-level BufferedImage generated by Barcode4J would be treated as having an Indexed color space, with most colors unused. Setting it to DeviceGray allows the image to be stored much more compactly.

Otherwise, the bcMatrix essentially scales and positions the image. Since we generated the image for a specific DPI (dots Per Inch), we need to scale that to points per inch which are the default user units in PDF, where 1point is 1/72 of an inch:

private static ASMatrix calcImageMatrix(BufferedImage im, double xpos,double ypos) {
double scaleX = im.getWidth() * 72d / dpi;
double scaleY = im.getHeight() * 72d / dpi;

return new ASMatrix(scaleX, 0, 0, scaleY, xpos, ypos);
}

After inserting the image into the page’s content, we finish up by saving the document, and we’re done. You can see the final product here.

If you would like to use the code mentioned in this blog, you can download it here.

 

Leave a Reply

Your email address will not be published. Required fields are marked *