It seems that there is an issue with iText Data Matrix barcodes, even with iText 8.0.4, if AUTO encoding is used. If I force to ASCII encoding, I do not see the issue. What is happening is that under certain circumstances, some of the characters, but not all of them, are flipping case (upper goes to lower, lower goes to upper). I am currently working around by forcing ASCII encoding, but thought this is probably worth investigating and fixing. Below is sample code demonstrating first a correct barcode and then an incorrect barcode.
The correct barcode scans
1nPUrEG_8vbghysdOMTV_FpO67pLVFuhq0nizXJ-Nfzyz4VxR0cLz9581IeQL5LTT0QpVcaaUJI=
while the incorrect scans
1nPUrEG_8vbghysdOMTV_FpO67pLVFuhq0nizXJ-Nfzyz4VxR0clZ9581iEql5ltt0qpVcaaUJI=
package com.vic.barcodetest;
import com.itextpdf.barcodes.BarcodeDataMatrix;
import static com.itextpdf.barcodes.BarcodeDataMatrix.DM_ASCII;
import com.itextpdf.kernel.geom.PageSize;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Image;
import com.itextpdf.layout.element.Paragraph;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
public class BarcodeTest {
public static void main(String[] args) {
try {
File testFile = File.createTempFile("test", ".pdf");
PdfDocument testPdf = new PdfDocument(new PdfWriter(testFile));
Document testDoc = new Document(testPdf, PageSize.LETTER);
final String barcodeData = "1nPUrEG_8vbghysdOMTV_FpO67pLVFuhq0nizXJ-Nfzyz4VxR0cLz9581IeQL5LTT0QpVcaaUJI=";
BarcodeDataMatrix bdm1 = new BarcodeDataMatrix();
bdm1.setOptions(DM_ASCII);
bdm1.setCode(barcodeData);
Image i1 = new Image(bdm1.createFormXObject(testPdf));
i1.scale(5.0F, 5.0F);
testDoc.add(i1);
testDoc.add(new Paragraph("Correct"));
BarcodeDataMatrix bdm2 = new BarcodeDataMatrix();
bdm2.setCode(barcodeData);
Image i2 = new Image(bdm2.createFormXObject(testPdf));
i2.scale(5.0F, 5.0F);
testDoc.add(i2);
testDoc.add(new Paragraph("Incorrect"));
testDoc.add(new Paragraph(barcodeData));
testDoc.close();
testPdf.close();
System.out.println("Wrote " + testFile.getAbsolutePath());
} catch(FileNotFoundException e) {
System.out.println(e.getMessage());
e.printStackTrace();
} catch(IOException e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
}
}
I haven’t had the time to investigate the issue further to find the exact issue.
user25413158 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.