[英]Identifying the specific PDF field type with 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]
如您所见,文本框是正确的,但下拉列表被视为复选框,而复选框被视为按钮。
iText 7返回正确的类型,仅返回您的代码
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
}
错误地解释了信息。
getFormType
根据PDF规范返回表单字段类型名称,并且ISO 32000-2在表226 中所有字段词典共有的条目中描述了字段类型:
该词典描述的字段类型:
Btn按钮(请参见12.7.5.2,“按钮字段”)
Tx文本(请参阅12.7.5.3,“文本字段”)
CH选择(参见12.7.5.4, “选场”)
Sig (PDF 1.3)签名(请参见12.7.5.5,“签名字段”)
因此, PdfName.Ch
指示选择字段,而PdfName.Btn
指示按钮字段的任何PdfName.Btn
; 再次根据ISO 32000-2,这次是12.7.5.2节“ 按钮字段” :
按钮字段 (字段类型Btn )表示屏幕上的交互式控件,用户可以用鼠标对其进行操作。 有三种类型的按钮字段:
- 按钮是一种纯粹的交互式控件,可立即响应用户输入而不会保留永久性值(请参见12.7.5.2.2,“按钮”)。
- 复选框会在打开和关闭两种状态之间切换(请参见12.7.5.2.3,“复选框”)。
- 单选按钮字段包含一组相关的按钮,每个按钮可以打开或关闭。 通常,在任何给定时间,一组中最多只有一个单选按钮处于打开状态,选择任何一个按钮会自动取消选择所有其他按钮。 (如第12.7.5.2.4节“单选按钮”中所述,此规则也有例外)
我发现了如何识别特定的字段类型。
更新的方法:
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]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.