You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过iText7获取PDF表单域的格式类型?

Retrieving Form Field Format Types with iText7

Absolutely! You can absolutely retrieve the format types (like Number, Date, Percentage) for text form fields in iText7—you just need to dig into the underlying PDF dictionary properties and additional actions associated with each field. Here's a practical, step-by-step breakdown:

1. Access the AcroForm and Iterate Through Fields

First, you'll need to load your PDF document, grab the AcroForm container, and loop through each form field:

PdfDocument pdfDoc = new PdfDocument(new PdfReader("your-document.pdf"));
PdfAcroForm form = PdfAcroForm.getAcroForm(pdfDoc, false);

if (form != null) {
    Map<String, PdfFormField> fields = form.getFormFields();
    for (Map.Entry<String, PdfFormField> entry : fields.entrySet()) {
        PdfFormField field = entry.getValue();
        String fieldName = entry.getKey();
        
        // Focus only on text form fields (since Number/Date/Percentage are text subtypes)
        if (field instanceof PdfTextFormField textField) {
            analyzeTextFieldFormat(textField, fieldName);
        }
    }
}

pdfDoc.close();

Create a helper method to check the key properties that define the field's format. Most formatted fields use JavaScript actions to enforce rules, so we'll start there:

private static void analyzeTextFieldFormat(PdfTextFormField textField, String fieldName) {
    PdfDictionary fieldDict = textField.getPdfObject();
    
    // Check for Adobe's standard formatting/validation actions
    PdfDictionary additionalActions = fieldDict.getAsDictionary(PdfName.AA);
    if (additionalActions != null) {
        // Check the Format action (/F) which handles display formatting
        checkActionScript(additionalActions.get(PdfName.F), fieldName);
        // Check the Validate action (/K) which enforces input rules
        checkActionScript(additionalActions.get(PdfName.K), fieldName);
    }
    
    // Use default value (/DV) as a hint for format pattern
    PdfString defaultValue = fieldDict.getAsString(PdfName.DV);
    if (defaultValue != null) {
        System.out.printf("Field '%s' default value format hint: %s%n", fieldName, defaultValue.toUnicodeString());
    }
    
    // Fixed-size fields (FLAG_DO_NOT_SCROLL) often indicate date/number types
    int fieldFlags = textField.getFieldFlags();
    if ((fieldFlags & PdfFormField.FLAG_DO_NOT_SCROLL) != 0) {
        System.out.printf("Field '%s' is fixed-size (common for dates/numbers)%n", fieldName);
    }
}

3. Parse JavaScript to Identify Format Type

Most PDF editors (like Adobe Acrobat) use Adobe's built-in JavaScript functions to format Number, Date, and Percentage fields. We can parse these scripts to detect the format:

private static void checkActionScript(PdfObject actionObj, String fieldName) {
    if (actionObj instanceof PdfDictionary actionDict) {
        PdfString jsScript = actionDict.getAsString(PdfName.JS);
        if (jsScript != null) {
            String script = jsScript.toUnicodeString();
            if (script.contains("AFNumber_Format")) {
                if (script.contains("percent")) {
                    System.out.printf("✅ Field '%s' is a **Percentage Field**%n", fieldName);
                } else {
                    System.out.printf("✅ Field '%s' is a **Number Field**%n", fieldName);
                }
            } else if (script.contains("AFDate_Format")) {
                System.out.printf("✅ Field '%s' is a **Date Field**%n", fieldName);
            } else if (script.contains("AFTime_Format")) {
                System.out.printf("✅ Field '%s' is a **Time Field**%n", fieldName);
            } else {
                // Handle custom formatting scripts
                System.out.printf("⚠️ Field '%s' uses custom formatting: %s%n", fieldName, script);
            }
        }
    }
}

Key Notes

  • Adobe's Built-in Functions: This approach works seamlessly with standard fields created in Adobe Acrobat, as they rely on the AF*_Format JavaScript functions.
  • Custom Formats: For fields with custom formatting logic, you'll need to extend the script parsing to match the specific rules used.
  • Raw PDF Access: If you need to check rare or custom properties, always use textField.getPdfObject() to access the underlying PDF dictionary directly.

内容的提问来源于stack exchange,提问作者Muddassir Awan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.11 09:17:49