Gopi
Gopi

Reputation: 31

Can iText support GS1 DataMatrix

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

Answers (1)

Enowneb
Enowneb

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:

  • You have to use [ and ] first for GS1 parentheses as the library will help you convert them into ( and ) and do further processing
  • The conversion of preferred size to Data Matrix size is included as Javadoc comments. You can check for the function setPreferredSize(int). You should explicitly call this function if you want to fix your Data Matrix to a certain size

Upvotes: 1

Related Questions