是否可以使用 iText 以编程方式将 'Open a web link action' 操作添加到 acroform 字段?

Is it possible to add 'Open a web link action' action to acroform field programmatically using iText?

在 Adob​​e Acrobat 中,可以将 'Open a web link action' 添加到 acroform。是否可以使用 iText 使用已经存在的 acroform 来做到这一点?

我无法在 iText 文档中找到关于它的任何提及,因此尝试以编程方式创建新的 acrofield 并向其中添加此操作,但没有成功。这是我的代码:

PdfReader pdfReader = new PdfReader(templateStream);
        PdfStamper stamper = new PdfStamper(pdfReader, new FileOutputStream("delivery.pdf"));
        stamper.setFormFlattening(true);
        stamper.getAcroFields().setField("package", packages);
        stamper.getAcroFields().setField("purchase_id", purchaseId);
        stamper.getAcroFields().setField("activation_code", activationCode);
        if (partner != "") {
            PdfFormField field = PdfFormField.createTextField(stamper.getWriter(), false,
                    false, 100);
            field.setFieldName("partner");
            PdfAction action = new PdfAction(partner);
            field.setAction(action);
            field.setColor(new BaseColor(0,0,255));
            PdfAppearance appearance = stamper.getUnderContent(1).
                    createAppearance(200, 20);
            appearance.setFontAndSize(BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED), 12f);
            appearance.setColorFill(BaseColor.BLUE);
            field.setAppearance(PdfAnnotation.APPEARANCE_DOWN, appearance);
            field.setDefaultAppearanceString(appearance);
            stamper.getAcroFields().setField("partner", "Click here to show partner's web site");
        }

显示的 PDF 文档没有合作伙伴字段。请指出一些文档或我的代码有误。

您正在尝试向表单添加交互性。但是,您也通过使用此行放弃了所有交互性:

stamper.setFormFlattening(true);

您还声称要添加一个额外的字段。据我所知,这种说法是错误的。您创建了一个字段名称 field(并且您 以困难的方式创建了它 ;我希望您改用 TextField class。但是,我没有看到您在任何地方添加该字段。我错过了以下行:

stamper.addAnnotation(field, 1);

请注意,此行没有意义:

stamper.getAcroFields().setField("partner", "Click here to show partner's web site");

为什么要先创建一个字段(并可能添加标题)然后立即更改它?为什么不从一开始就正确创建字段?

最后,您似乎想创建一个可以被人们点击的按钮。那你为什么要创建一个文本字段?创建一个按钮不是更有意义吗?

这是机器会响应的问题示例:错误太多...也许您应该考虑在尝试修复您的问题之前阅读 the documentation代码。