Reputation:
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.
Upvotes: 3
Views: 8065
Reputation: 11
Implementation in Java to add barcode to existing pdf file using itext.
Jar files used: itext, commons-codec-1.6.jar
InputStream inputStream = new ByteArrayInputStream(FileUtils.readFileToByteArray(new File("c:/tmp/your_existing.pdf")));
FileOutputStream outStream = new FileOutputStream("c:/tmp/pdf_copy_1.pdf");
PdfContentByte contentByte = null;
int totalPages = 0;
PdfReader pdfReader = null;
PdfStamper pdfStamper = null;
try {
pdfReader = new PdfReader(inputStream);
totalPages = pdfReader.getNumberOfPages();
pdfStamper = new PdfStamper(pdfReader, outStream);
String text = "SAMPLE BARCODE TEST";
String barcodeData = DigestUtils.md5Hex(text); --encoding barcode text...
//Adding barcode to each page in pdf
for (int i = 1; i <= totalPages; i++) {
contentByte = pdfStamper.getOverContent(i);
if(barcodeData != null){
Image img = null;
BarcodeDatamatrix datamatrix = new BarcodeDatamatrix();
datamatrix.setWidth(10); -- BARCODE WIDTH
datamatrix.setHeight(10); -- BARCODE HEIGHT
datamatrix.generate(barcodeData);
img = datamatrix.createImage();
img.setAbsolutePosition(175, 750); --barcode position x,y
contentByte.addImage(img);
}
}
}
catch (Exception e) {
e.printStackTrace();
}
Upvotes: 1
Reputation: 11
You must change
barcode.setOptions(BarcodeDatamatrix.DM_AUTO);
to
barcode.setOptions(BarcodeDatamatrix.DM_B256);
Upvotes: 1
Reputation: 479
For those who need it in C#:
// 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.Options = (BarcodeDatamatrix.DM_AUTO);
// try to generate the barcode, resizing as needed.
for (int generateCount = 0; generateCount < barcodeDimensions.Length; generateCount++)
{
barcode.Width = (barcodeDimensions[generateCount]);
barcode.Height = (barcodeDimensions[generateCount]);
int returnResult = barcode.Generate("1234567");
if (returnResult == BarcodeDatamatrix.DM_NO_ERROR)
{
Image barcodeImg = barcode.CreateImage();
table.AddCell(barcodeImg);
table.AddCell(string.Empty);
}
}
Btw, does anyone know how to make rectangular barcodes with this? Whenever I choose a rectangular format it gives me an Error code of 3 (DM_Error_Invalid_Square).
Upvotes: 3
Reputation: 5105
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
Upvotes: 3
Reputation: 586
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.");
Upvotes: 4