Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iText datamatrix generation problem

I am attempting to generate datamatrix barcodes from within itext. This works fine for most of my codes but not for some codes. One example is:

HEnSh0701003-2V1

This produces a non square barcode for some reason. When I use encoders from other companies (such as IDAutomation) I do get a valid square barcode.

Does anyone have an idea why this is happening? I am looking for a solution so I can use the embedded iTest DataMatrix class and not have to use a third party one.

A sample of the code I am using:

BarcodeDatamatrix bar = new BarcodeDatamatrix();
 bar.setOptions(BarcodeDatamatrix.DM_AUTO);
bar.generate("HEnSh0701003-2V1");
bcd.addCell(bar.createImage());

where bcd is a PdfTable with 2 columns.


1 Answers

I ran into this exact issue. I ended up digging into the iText source code to figure this one out. iText is resizing the barcode to fit the text you provided.

iText supports the following sizes for datamatrix barcodes: 10x10, 12x12, 8x18, 14x14, 8x32, 16x16, 12x26, 18x18, 20x20, 12x36, 22x22, 16x36, 24x24, 26x26, 16x48, 32x32, 36x36, 40x40, 44x44, 48x48, 52x52, 64x64, 72x72, 80x80, 88x88, 96x96, 104x104, 120x120, 132x132, 144x144

As you can see, there are a number of non-square sizes in there. What I did was create a list of square barcode sizes and then try each size while checking the return value of the generate() call.

// supported square barcode dimensions
int[] barcodeDimensions = {10, 12, 14, 16, 18, 20, 22, 24, 26, 32, 36, 40, 44, 48, 52, 64, 72, 80, 88, 96, 104, 120, 132, 144};

BarcodeDatamatrix barcode = new BarcodeDatamatrix();
barcode.setOptions(BarcodeDatamatrix.DM_AUTO);

// try to generate the barcode, resizing as needed.
for (int generateCount = 0; generateCount < barcodeDimensions.length; generateCount++) {
    barcode.setWidth(barcodeDimensions[generateCount]);
    barcode.setHeight(barcodeDimensions[generateCount]);
    int returnResult = barcode.generate(text);
    if (returnResult == BarcodeDatamatrix.DM_NO_ERROR) {
        return barcode.createImage();
    }
}

throw new Exception("Error generating barcode.");
like image 117
JonMR Avatar answered Apr 18 '26 19:04

JonMR



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!