Reputation: 31
I just started working on iText to generate PDF document. I need to generate GS1 standard DataMatrix barcodes. Can anybody share sample code snippet? Thank you so much in advance.
Upvotes: 3
Views: 476
Reputation: 1037
I have gone through the iText documentation com.itextpdf.barcodes.BarcodeDataMatrix
but seems that none of options are capable of generating a standard GS1 DataMatrix.
So I have instead used another library OkapiBarcode to generate a GS1 DataMatrix image and import that image into my document:
DataMatrix dataMatrix = new DataMatrix();
dataMatrix.setDataType(Symbol.DataType.GS1);
// Remember to use '[' and ']'
String targetDataMatrixCode = "[01]03453120000011[17]191125[10]ABCD1234";
// Preferred Size indicates the Data Matrix Size
dataMatrix.setPreferredSize(5);
dataMatrix.setContent(targetDataMatrixCode);
// Generate BufferedImage
BufferedImage dataMatrixImage = new BufferedImage(dataMatrix.getWidth(), dataMatrix.getHeight(), BufferedImage.TYPE_BYTE_GRAY);
Graphics2D g2d = dataMatrixImage.createGraphics();
Java2DRenderer renderer = new Java2DRenderer(g2d, 1, Color.WHITE, Color.BLACK);
renderer.render(dataMatrix);
// Then you can continue processing display with dataMatrixImage
...
So points to notice for the above code:
[
and ]
first for GS1 parentheses as the library will help you convert them into (
and )
and do further processingsetPreferredSize(int)
. You should explicitly call this function if you want to fix your Data Matrix to a certain sizeUpvotes: 1