gpt4 book ai didi

java - iText 7 - 重新生成使用 Adob​​e 创建的条形码字段

转载 作者:行者123 更新时间:2023-12-02 01:58:39 31 4
gpt4 key购买 nike

我来找你是因为我有一个问题!

我使用 Adob​​e Acrobat 在 acroform PDF 中创建了条形码。我可以将新值设置为 barCode 字段,但无法使用新值生成新的“外观”。我怎样才能做到这一点?

这就是我现在所做的,但没有成功......:

PdfFormField field = fields.get("QRCODE");
field.setValue(this.generateXMLDataMatrix(),false);
form = form.setNeedAppearances(true);
form.flattenFields();
pdf.close();

所以现在,我仍然保留在 Adob​​e Acrobat 中创建的默认外观:(

谢谢你的帮助! :)

最佳答案

您必须手动生成字段外观。以下是如何对二维码执行此操作的示例:

PdfFormField field = fields.get("QRCODE");
// Get the annotation. If you might have multiple representations of the same field across the document the just iterate over all widgets
PdfWidgetAnnotation annotation = field.getWidgets().get(0);
// This class will help us draw the barcode
BarcodeQRCode qrCode = new BarcodeQRCode(this.generateXMLDataMatrix());
// Get the number of rectangles constituting the barcode
Rectangle size = qrCode.getBarcodeSize();
// Creating a new FormXObject that will become our apperance. Set the dimension(bbox) of the current appearance
PdfFormXObject newAppearance = new PdfFormXObject(annotation.getAppearanceObject(PdfName.N).getAsRectangle(PdfName.BBox));
// Helper class to draw on FormXObject
PdfCanvas barcodeCanvas = new PdfCanvas(newAppearance, pdfDocument);
// Calculating the side of the smaller rectangle in the barcode
float side = Math.min(annotation.getRectangle().toRectangle().getHeight() / size.getHeight(),
annotation.getRectangle().toRectangle().getWidth() / size.getWidth());
// Draw the barcode on the XObject
qrCode.placeBarcode(barcodeCanvas, ColorConstants.BLACK, side);
// Set appearance to the newly generated one
annotation.setAppearance(PdfName.N, newAppearance.getPdfObject());

关于java - iText 7 - 重新生成使用 Adob​​e 创建的条形码字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57386020/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com