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

标签 java pdf itext7

使用此 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]

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

最佳答案

我找到了如何识别特定字段类型。

更新的方法:

    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()) {
        PdfName type = form.getField(key).getFormType();
        String simpleFieldType = getSimpleFieldType(form.getField(key), type, key);
        s += "[Field name: " + key + ", Field type: " + simpleFieldType + "]\n";

    }
    s = (s.substring(0, s.length() - 1));

    return s;
}

private String getSimpleFieldType(PdfFormField field, PdfName type, String key) {

     if (0 == PdfName.Btn.compareTo(type)) {
         if(((PdfButtonFormField)form.getField(key)).isPushButton()){
             return "Push Button";
         } else {
             if(((PdfButtonFormField)form.getField(key)).isRadio()){
                 return "Radio Button";                   
             }else {
                 return "Check Box";
             }
         }
     } else if (0 == PdfName.Ch.compareTo(type)) {
         return "List Box";
     } else if (0 == PdfName.Sig.compareTo(type)) {
         return "Signature";
     } else if (0 == PdfName.Tx.compareTo(type)) {
         return "Text Box";

     }else {
         return "Unknown type";
     }
}

结果现在显示为:

[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: List Box]
[Field name: Gender List Box, Field type: List Box]
[Field name: Height Formatted Field, Field type: Text Box]
[Field name: Driving License Check Box, Field type: Check Box]
[Field name: Language 1 Check Box, Field type: Check Box]
[Field name: Language 2 Check Box, Field type: Check Box]
[Field name: Language 3 Check Box, Field type: Check Box]
[Field name: Language 4 Check Box, Field type: Check Box]
[Field name: Language 5 Check Box, Field type: Check Box]
[Field name: Favourite Colour List Box, Field type: List Box]

关于java - 使用 iText7 识别特定 PDF 字段类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48406731/

相关文章:

asp.net - Crystal 存储库下载 - ExportToHttpResponse 正在下载不带扩展名的文件

c# - 如何使用 itext7 在固定矩形内缩放文本?

java - 如何将查询结果直接转换为 Hibernate 中的数据对象?

java - 从 JDBC 数据库中检索数据到 Jtable

java - JTextField 上的 setBorder 不起作用?或者是吗?

java - 用完整形式替换缩写/俚语

java - IF 语句给出无法解析 PDFDocument 中的符号

php - 使用 TCPDF 将 CMYK 图像变为负片

java - IText 7 如何在页眉中添加div或段落而不与页面内容重叠?

java - itext7 错误