如何通过iText7获取PDF表单域的格式类型?
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();
2. Inspect Format-Related Field Properties
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*_FormatJavaScript 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

