How to set the export value of a check box?

Tags: formscheck boxiText 5

I need to add a check box to an existing PDF. I also need set the export value of this check box. I saw that I can get the possible values of an existing check box by looking at the appearance dictionary (/AP), but I don't know how to set the export value for a new checkbox.

With export value I mean the value which is marked in the screenshot

Check box export value

Here is my code to create a check box, but the export value is always empty:

PdfFormField checkbox = PdfFormField.createCheckBox(stamper.getWriter());
checkbox.setWidget(fieldVO.rect, PdfAnnotation.HIGHLIGHT_NONE);
checkbox.setFieldName(fieldVO.getNewName());
//TODO set export value
log.info("exportValue is " + fieldVO.getExportValue());
break;
Posted on StackOverflow on Jul 15, 2015 by Al Phaba

First this: you are using PdfFormField to create a check box. That is hard. Why don't you use the RadioCheckField class?

rect = new Rectangle(180, 806, 200, 788);
checkbox = new RadioCheckField(stamper.getWriter(), rect,
     "fieldVO.getNewName()", "on");
field = checkbox.getCheckField();

Then you want to define the appearances. Suppose that onOff is an array of PdfAppearance objects, one with the appearance if the box isn't selected (0) and one if the box is selected (1). Note that the name of the off state should be "Off" as defined in ISO-32000-1. As far as I remember, the standard recommends using "Yes" for the on state, but you can use a custom value, such as "MyCustomValue":

field.setAppearance(
    PdfAnnotation.APPEARANCE_NORMAL, "Off", onOff[0]);
field.setAppearance(
    PdfAnnotation.APPEARANCE_NORMAL, "MyCustomValue", onOff[1]);

In this case, the appearance of the on state will be stored as an entry with key /MyCustomValue. This key is a PDF name object and several restrictions apply. That's why the /Opt key was introduced:

(Taken from ISO-32000-1) Beginning with PDF 1.4, the field dictionary for check boxes and radio buttons may contain an optional Opt entry. If present, the Opt entry shall be an array of text strings representing the export value of each annotation in the field. It may be used for the following purposes:

  • To represent the export values of check box and radio button fields in non-Latin writing systems. Because name objects in the appearance dictionary are limited to PDFDocEncoding, they cannot represent non-Latin text.
  • To allow radio buttons or check boxes to be checked independently, even if they have the same export value.

EXAMPLE: a group of check boxes may be duplicated on more than one page such that the desired behavior is that when a user checks a box, the corresponding boxes on each of the other pages are also checked. In this case, each of the corresponding check boxes is a widget in the Kids array of a check box field.

In your case, you'd need something like:

PdfArray options = new PdfArray();
options.add(new PdfString("My Custom Value"));
field.put(PdfName.OPT, options);

I haven't tested if this works, I'm merely interpreting what ISO-32000-1 says and converting the spec into iText code.

Finally, you can add the field.

stamper.addAnnotation(field, 1);