Thomas Harre
2015-02-06 09:23:35 UTC
Hi Everybody
I'm struggeling to find a way to find out whether a checkbox is checked
or not and would appreciate a little help.
As far as I could collect from googeling the matter I've figured out
that I'm supposed to compare the value of the AcroField to the
available appearanceStates.
My problem however is that I don't know which one of the
appearanceStates is the one representing the "checked" state.
Is there any way to find that out without looking at the document
manually?
In the attachment there's a PDF document that hast 16 checkboxes with
several combinations of checked/unchecked, differet
export values (1, true, Ja, x) and differences in whether they are
checked by default or not.
With the following java code I extracted the values and available
values
String pdfPath =
getClass().getResource("DemoCheckbox.pdf").getPath();
PdfReader pdfReader = new PdfReader(pdfPath);
AcroFields acroFields = pdfReader.getAcroFields();
for (Map.Entry<String, AcroFields.Item> entry :
acroFields.getFields().entrySet()) {
String fieldName = entry.getKey();
String value = acroFields.getField(fieldName);
if (acroFields.getFieldType(fieldName) ==
AcroFields.FIELD_TYPE_CHECKBOX) {
String[] appearanceStates =
acroFields.getAppearanceStates(fieldName);
System.out.println("Checkbox: " + fieldName + " Value:" + value
+
" AvailableValues:" + StringUtil.concatWithDelimiter(",",
appearanceStates));
}
}
Checkbox: box_1_clear Value: AvailableValues:Off,1
Checkbox: box_1_ticked Value:1 AvailableValues:Off,1
Checkbox: box_1_bydefault_cleared Value:Off AvailableValues:Off,1
Checkbox: box_1_bydefault_ticked Value:1 AvailableValues:Off,1
Checkbox: box_true_clear Value: AvailableValues:true,Off
Checkbox: box_true_ticked Value:true AvailableValues:true,Off
Checkbox: box_true_bydefault_clear Value:Off AvailableValues:true,Off
Checkbox: box_true_bydefault_ticked Value:true
AvailableValues:true,Off
Checkbox: box_ja_clear Value: AvailableValues:Ja,Off
Checkbox: box_ja_ticked Value:Ja AvailableValues:Ja,Off
Checkbox: box_ja_bydefault_clear Value:Off AvailableValues:Ja,Off
Checkbox: box_ja_bydefault_ticked Value:Ja AvailableValues:Ja,Off
Checkbox: star_x_clear Value: AvailableValues:Off,x
Checkbox: star_x_ticked Value:x AvailableValues:Off,x
Checkbox: star_x_bydefault_clear Value:Off AvailableValues:Off,x
Checkbox: star_x_bydefault_ticked Value:x AvailableValues:Off,x
Clearely I cannot rely on the order of the available values.
So from what I can gather the checkbox is always checked if the value
is not 'Off'.
But is this garanteed?
Or is there another possibillity to figure out if the checkbox is
actually checked or not?
Thanks in advance for your help and kind regards
Thomas
I'm struggeling to find a way to find out whether a checkbox is checked
or not and would appreciate a little help.
As far as I could collect from googeling the matter I've figured out
that I'm supposed to compare the value of the AcroField to the
available appearanceStates.
My problem however is that I don't know which one of the
appearanceStates is the one representing the "checked" state.
Is there any way to find that out without looking at the document
manually?
In the attachment there's a PDF document that hast 16 checkboxes with
several combinations of checked/unchecked, differet
export values (1, true, Ja, x) and differences in whether they are
checked by default or not.
With the following java code I extracted the values and available
values
String pdfPath =
getClass().getResource("DemoCheckbox.pdf").getPath();
PdfReader pdfReader = new PdfReader(pdfPath);
AcroFields acroFields = pdfReader.getAcroFields();
for (Map.Entry<String, AcroFields.Item> entry :
acroFields.getFields().entrySet()) {
String fieldName = entry.getKey();
String value = acroFields.getField(fieldName);
if (acroFields.getFieldType(fieldName) ==
AcroFields.FIELD_TYPE_CHECKBOX) {
String[] appearanceStates =
acroFields.getAppearanceStates(fieldName);
System.out.println("Checkbox: " + fieldName + " Value:" + value
+
" AvailableValues:" + StringUtil.concatWithDelimiter(",",
appearanceStates));
}
}
Checkbox: box_1_clear Value: AvailableValues:Off,1
Checkbox: box_1_ticked Value:1 AvailableValues:Off,1
Checkbox: box_1_bydefault_cleared Value:Off AvailableValues:Off,1
Checkbox: box_1_bydefault_ticked Value:1 AvailableValues:Off,1
Checkbox: box_true_clear Value: AvailableValues:true,Off
Checkbox: box_true_ticked Value:true AvailableValues:true,Off
Checkbox: box_true_bydefault_clear Value:Off AvailableValues:true,Off
Checkbox: box_true_bydefault_ticked Value:true
AvailableValues:true,Off
Checkbox: box_ja_clear Value: AvailableValues:Ja,Off
Checkbox: box_ja_ticked Value:Ja AvailableValues:Ja,Off
Checkbox: box_ja_bydefault_clear Value:Off AvailableValues:Ja,Off
Checkbox: box_ja_bydefault_ticked Value:Ja AvailableValues:Ja,Off
Checkbox: star_x_clear Value: AvailableValues:Off,x
Checkbox: star_x_ticked Value:x AvailableValues:Off,x
Checkbox: star_x_bydefault_clear Value:Off AvailableValues:Off,x
Checkbox: star_x_bydefault_ticked Value:x AvailableValues:Off,x
Clearely I cannot rely on the order of the available values.
So from what I can gather the checkbox is always checked if the value
is not 'Off'.
But is this garanteed?
Or is there another possibillity to figure out if the checkbox is
actually checked or not?
Thanks in advance for your help and kind regards
Thomas