使用iText7识别特定的PDF字段类型

问题描述 投票:0回答:2

使用此PDF表单示例:http://foersom.com/net/HowTo/data/OoPdfFormExample.pdf

这段代码:

public String getPdfFieldNames() throws IOException {
    if (pdf == null || pdf.isClosed()) {
        throwPdfNotOpenException();
    }
    if (getPdfFormType().equals("XFA")) {
        throwXfaNotSupportedException();
    }

    String s = "";
    Map<String, PdfFormField> map = form.getFormFields();
    for (String key : map.keySet()) {
        String simpleFieldType = getSimpleFieldType(form.getField(key));
        s += "[Field name: " + key + ", Field type: " + simpleFieldType + "]\n";
    }
    s = (s.substring(0, s.length() - 1));

    return s;
}
private String getSimpleFieldType(PdfFormField field) {
    if (field.getFormType() == PdfName.Tx) {
        return "text box";
    } else if (field.getFormType() == PdfName.Ch) {
        return "check box";
    } else if (field.getFormType() == PdfName.Btn) {
        return "button";
    } else {
        return field.getFormType().toString();
    }
    // also do radio button
}

产生以下结果:

    [Field name: Given Name Text Box, Field type: text box]
    [Field name: Family Name Text Box, Field type: text box]
    [Field name: Address 1 Text Box, Field type: text box]
    [Field name: House nr Text Box, Field type: text box]
    [Field name: Address 2 Text Box, Field type: text box]
    [Field name: Postcode Text Box, Field type: text box]
    [Field name: City Text Box, Field type: text box]
    [Field name: Country Combo Box, Field type: check box]
    [Field name: Gender List Box, Field type: check box]
    [Field name: Height Formatted Field, Field type: text box]
    [Field name: Driving License Check Box, Field type: button]
    [Field name: Language 1 Check Box, Field type: button]
    [Field name: Language 2 Check Box, Field type: button]
    [Field name: Language 3 Check Box, Field type: button]
    [Field name: Language 4 Check Box, Field type: button]
    [Field name: Language 5 Check Box, Field type: button]
    [Field name: Favourite Colour List Box, Field type: check box]

正如您所看到的文本框是正确的,但正在考虑下拉列表复选框和复选框被视为按钮。

java pdf itext7
2个回答
© www.soinside.com 2019 - 2024. All rights reserved.