简体   繁体   English

使用iText将javascript验证添加到pdf中的现有acroform字段

[英]Adding javascript validation to existing acroform field in pdf with iText

I am trying to add javascript validation to an existing Acroform field via iText in java. 我正在尝试通过Java中的iText将javascript验证添加到现有的Acroform字段中。 I have written the following code so far, but no action is assigned to the form field. 到目前为止,我已经编写了以下代码,但是没有将任何操作分配给表单字段。 Is there something I am missing? 我有什么想念的吗?

    PdfReader reader = new PdfReader(uri);
    PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
    PdfWriter writer = stamper.getWriter();

    AcroFields acroFields = reader.getAcroFields();
    AcroFields.Item dateField = acroFields.getFieldItem("SignDateField");
    PdfAction pdfAction = PdfAction.javaScript("app.alert('hello');", writer);

    PdfDictionary widgetRefDict = (PdfDictionary) PdfReader.getPdfObject(dateField.getWidgetRef(0));
    PdfDictionary actionDict = widgetRefDict.getAsDict(PdfName.AA);
    if (actionDict == null) {
        actionDict = new PdfDictionary();
    }
    actionDict.put(PdfName.V, pdfAction);

    stamper.close();
    reader.close();

When there's no AA entry in the annotation dictionary, you're creating a new dictionary. 当注释词典中没有AA条目时,您将在创建一个新词典。 But you're not adding that new dictionary to the annotation dictionary. 但是您没有将新字典添加到注释字典中。

// ...
if (actionDict == null) {
    actionDict = new PdfDictionary();
    // add the newly created action dict
    widgetRefDict.put(PdfName.AA, actionDict);
}
// ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM