itextbarcodepdf417

Fill Barcode PDF-417 in PDF using ITEXT for a field identifier


I need to generate a PDF417 barcode and fill it in a PDF field for which I have the filed identifier using ITEXT API.

I have used several ways such as

Way 1

      PdfContentByte cb = stamper.getUnderContent(1);
      BarcodePDF417 pf = new BarcodePDF417();
      pf.setText("Some text value for encoding");
      Rectangle size = pf.getBarcodeSize();

      PdfTemplate template = cb.createTemplate(size.getWidth(), size.getHeight());
      pf.placeBarcode(template, BaseColor.BLACK, 5, 5);
      Image image = Image.getInstance(template);
      image.setAbsolutePosition(35f, 40f);
      cb.addImage(image);

The issue here is the barcode is not of right size and is not placed at correct position.

Way 2.

  BarcodePDF417 barcode = new BarcodePDF417();
  barcode.setText("Some text value for encoding");
  barcode.placeBarcode(cb, BaseColor.BLACK, 5, 5);

Here too the issue is same, the barcode is not of right size and is not placed at correct position.

I know the field IDENTIFIER for the filed where I need to place the barcode specifically. Is there a way with which I can get the cell and generate the barcode image of exact size of this cell and place it in?

Thank you for all the help!


Solution

  • I could finally implement this. My code is

            PdfContentByte cb = stamper.getUnderContent(i);// i being the pdf page number.
            BarcodePDF417 barcode = new BarcodePDF417();
            barcode.setErrorLevel(5);
            barcode.setCodeColumns(30);
            barcode.setCodeRows(5);
            barcode.setOptions(BarcodePDF417.PDF417_FIXED_RECTANGLE);
            barcode.setText(<Byte[] - aka byte array of the text that needs to be encoded.>);
            Rectangle rect = barcode.getBarcodeSize();
    
            PdfTemplate template = cb.createTemplate(2 + rect.getWidth(), 2 + rect.getHeight());
    
            barcode.placeBarcode(template, BaseColor.BLACK, 1, 1);
    
            Image image = Image.getInstance(template);
            image.scaleAbsolute(535, 135);
            image.setAbsolutePosition(40f, 40f);
            cb.addImage(image);