猿问

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

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


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


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


PdfFormField field = fields.get("QRCODE");

field.setValue(this.generateXMLDataMatrix(),false);

form = form.setNeedAppearances(true);

form.flattenFields();

pdf.close();    

所以现在,我仍然拥有在 Adobe Acrobat 中创建的默认外观 :(


谢谢你的帮助 !:)


千万里不及你
浏览 182回答 2
2回答

慕桂英3389331

您将必须手动生成字段外观。这是一个如何为 QR 码执行此操作的示例: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 widgetsPdfWidgetAnnotation annotation = field.getWidgets().get(0);// This class will help us draw the barcodeBarcodeQRCode qrCode = new BarcodeQRCode(this.generateXMLDataMatrix());// Get the number of rectangles constituting the barcodeRectangle size = qrCode.getBarcodeSize();// Creating a new FormXObject that will become our apperance. Set the dimension(bbox) of the current appearancePdfFormXObject newAppearance = new PdfFormXObject(annotation.getAppearanceObject(PdfName.N).getAsRectangle(PdfName.BBox));// Helper class to draw on FormXObjectPdfCanvas barcodeCanvas = new PdfCanvas(newAppearance, pdfDocument);// Calculating the side of the smaller rectangle in the barcodefloat side = Math.min(annotation.getRectangle().toRectangle().getHeight() / size.getHeight(),        annotation.getRectangle().toRectangle().getWidth() / size.getWidth());// Draw the barcode on the XObjectqrCode.placeBarcode(barcodeCanvas, ColorConstants.BLACK, side);// Set appearance to the newly generated oneannotation.setAppearance(PdfName.N, newAppearance.getPdfObject());

12345678_0001

解决了!这就是我最终做到的:    //Generate BarCodedataMatrix    BarcodeDataMatrix dataMatrix = new BarcodeDataMatrix();    //Add code to the BarCodedataMatrix    dataMatrix.setCode(generateXMLDataMatrix());    dataMatrix.setOptions(BarcodeDataMatrix.DM_AUTO);    //Add dataMatrix to PDF    //Create a canvas for the first page    PdfCanvas canvas = new PdfCanvas(pdf.getFirstPage());    //Create the PdfFormXObject based on the dataMatrix    PdfFormXObject object = dataMatrix.createFormXObject(pdf);    //Add the pdfFormXObject to the canvas at X,Y position    canvas.addXObject(object,50,660);
随时随地看视频慕课网APP

相关分类

Java
我要回答