简体   繁体   中英

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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