iText 7-重新生成使用Adobe创建的条形码字段

时间:2019-08-07 01:55:19

标签: java itext itext7

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

我用Adobe Acrobat在acroform PDF中创建了条形码。我可以将新值设置为barCode字段,但是无法使用新值生成新的“外观”。我该怎么办?

这是我目前没有成功的方法...:

=IIF((Fields!PrimaryUserName.Value = "John Cena") OR (Fields!PrimaryUserName.Value = "Brad Pitt"), "Black", "Yellow")

因此,到目前为止,我仍然具有在Adobe Acrobat中创建的默认外观:(

谢谢您的帮助! :)

2 个答案:

答案 0 :(得分:0)

您将必须手动生成字段外观。这是一个如何对一个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 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());

答案 1 :(得分:0)

已解决!我终于做到了:

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