1

我需要生成一个 PDF417 条形码并将其填充到我使用 ITEXT API 为其归档标识符的 PDF 字段中。

我使用了几种方法,例如

方式一

      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);

这里的问题是条形码尺寸不正确,并且没有放置在正确的位置。

方式2。

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

这里的问题也是一样的,条形码尺寸不正确,并且没有放置在正确的位置。

我知道需要专门放置条形码的字段的 IDENTIFIER 字段。有没有一种方法可以让我获取单元格并生成该单元格精确大小的条形码图像并将其放入?

谢谢大家的帮助!

4

1 回答 1

1

我终于可以实现这个了。我的代码是

        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);
于 2017-05-07T07:42:05.803 回答