views:

803

answers:

2

Hi, 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  A: 

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.");
JonMR
+1  A: 

Thanks JonMR

For those who need it, here is the same code in VB.net

    Private Function GetDataMatrixBarcode(ByVal message As String) As iTextSharp.text.Image

        Dim barcode As BarcodeDatamatrix = New BarcodeDatamatrix()
        Dim barcodeDimensions() As Integer = New Integer() {10, 12, 14, 16, 18, 20, 22, 24, 26, 32, 36, 40, 44, 48, 52, 64, 72, 80, 88, 96, 104, 120, 132, 144}
        Dim returnResult As Integer

        barcode.Options = BarcodeDatamatrix.DM_AUTO

        For generateCount As Integer = 0 To barcodeDimensions.Length - 1
            barcode.Width = barcodeDimensions(generateCount)
            barcode.Height = barcodeDimensions(generateCount)
            returnResult = barcode.Generate(message)
            If returnResult = BarcodeDatamatrix.DM_NO_ERROR Then
                Return barcode.CreateImage
            End If
        Next

        Throw New Exception("Error generating datamatrix barcode for text '" & message & "'")

    End Function
CResults