使用 iText7 识别特定的 PDF 字段类型
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 returns 正确的类型,只是你的代码
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
returns 根据PDF规范的表单字段类型名称,ISO 32000-2描述字段类型在Table 226 项共同所有字段字典:
The type of field that this dictionary describes:
Btn Button (see 12.7.5.2, "Button fields")
Tx Text (see 12.7.5.3, "Text fields")
Ch Choice (see 12.7.5.4, "Choice fields")
Sig (PDF 1.3) Signature (see 12.7.5.5, "Signature fields")
因此 PdfName.Ch
表示选择字段,PdfName.Btn
表示按钮字段的任何风格;再次根据 ISO 32000-2,这次是第 12.7.5.2 节 Button fields:
A button field (field type Btn) represents an interactive control on the screen that the user can manipulate with the mouse. There are three types of button fields:
- A push-button is a purely interactive control that responds immediately to user input without retaining a permanent value (see 12.7.5.2.2, "Push-buttons").
- A check box toggles between two states, on and off (see 12.7.5.2.3, "Check boxes").
- Radio button fields contain a set of related buttons that can each be on or off. Typically, at most one radio button in a set may be on at any given time, and selecting any one of the buttons automatically deselects all the others. (There are exceptions to this rule, as noted in 12.7.5.2.4, "Radio buttons")
我找到了如何识别特定字段类型。
更新的方法:
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]
使用此 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 returns 正确的类型,只是你的代码
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
returns 根据PDF规范的表单字段类型名称,ISO 32000-2描述字段类型在Table 226 项共同所有字段字典:
The type of field that this dictionary describes:
Btn Button (see 12.7.5.2, "Button fields")
Tx Text (see 12.7.5.3, "Text fields")
Ch Choice (see 12.7.5.4, "Choice fields")
Sig (PDF 1.3) Signature (see 12.7.5.5, "Signature fields")
因此 PdfName.Ch
表示选择字段,PdfName.Btn
表示按钮字段的任何风格;再次根据 ISO 32000-2,这次是第 12.7.5.2 节 Button fields:
A button field (field type Btn) represents an interactive control on the screen that the user can manipulate with the mouse. There are three types of button fields:
- A push-button is a purely interactive control that responds immediately to user input without retaining a permanent value (see 12.7.5.2.2, "Push-buttons").
- A check box toggles between two states, on and off (see 12.7.5.2.3, "Check boxes").
- Radio button fields contain a set of related buttons that can each be on or off. Typically, at most one radio button in a set may be on at any given time, and selecting any one of the buttons automatically deselects all the others. (There are exceptions to this rule, as noted in 12.7.5.2.4, "Radio buttons")
我找到了如何识别特定字段类型。
更新的方法:
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]