AcroForm の抽出 - Java で PDF からフォームデータの抽出

使用する Form は、単純なフィールド名からフィールド値への抽出フローが必要な場合のファサードです。

すべての AcroForm フィールドからの値の抽出

  1. Form ファサードを使用して PDF フォームドキュメントを開いてください。
  2. フィールド名を取得元から反復処理し、各現在のフィールド値を Form ファサードからマップに格納してください。
public static Map<String, String> getValuesFromAllFields(Path inputFile) {
    Form form = new Form(inputFile.toString());
    try {
        Map<String, String> formValues = new LinkedHashMap<>();
        for (String fieldName : form.getFieldNames()) {
            formValues.put(fieldName, form.getField(fieldName));
        }

        System.out.println(formValues);
        return formValues;
    } finally {
        form.close();
    }
}