首页 文章

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

提问于
浏览
0

我试图通过 java 中的 iText 将 javascript 验证添加到现有的 Acroform 字段中。到目前为止,我已编写了以下代码,但没有为表单字段分配任何操作。有什么我想念的吗?

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

1 回答

  • 2

    当注释字典中没有AA条目时,您正在创建一个新字典。但是你没有将新词典添加到注释词典中。

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

相关问题