gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 02:21:42 26 4
gpt4 key购买 nike

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

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com