views:

141

answers:

2

Using PDF, is it possible to create a single form element with multiple fields of which several can be selected? For example, in HTML, one can create a set of checkboxes associated with the same field name:

<div>Select one for Member of the School Board</div>
<input type="checkbox" name="field(school)" value="vote1">
<span class="label">Libby T. Garvey</span><br/>
<input type="checkbox" name="field(school)" value="vote2">
<span class="label">Emma N. Violand-Sanchez</span><br/>

In this case, the field name is "field(school)", and when the form is submitted, "field(school)" can be supplied 0, 1, or 2 times.

Is there an equivalent construct in PDF where a single field can have multiple values. So far in my investigation, it appears that if fields are assigned the same name, it is only possible to select one field. If it is possible to implement this in PDF, what is this construct called and how can it be implemented?

Edit: To clarify, I am aware that a PDF can contain multiple form fields with different field names, and those can be selected independently, but then the grouping is implicit and not explicit as with the HTML form. I would like to use a construct that makes the grouping of options explicit, and preferably allows for restrictions (e.g. at least one required, no more than 2 allowed, etc).

Edit: If someone can find an authoritative opinion that this is not possible, that would also be a desirable answer.

+1  A: 

Yes it is possible. In Adobe PDFs you have the checkbox concept and the radio button concept. While each checkbox and radio button can have its own name, however, they can also be grouped through a subtier via the GroupName.subobj.

Adobe describes it as follows:

The field name. This may include hierarchical syntax in order to facilitate logical groupings. For example, the name myGroup.firstField implies that the form field firstField belongs to a group of fields called myGroup. The advantage of creating logical hierarchies is that you can enforce consistency among the properties of related form fields by setting the properties of the group, which automatically propagate to all form fields within the group.

When the fields are set via a hierarchy you can then get the value of myGroup in this case, and return the selected value of the group. Similarly in the case of RadioButtons you would make sure that all fields in a group have the same name.

This approach to creating form fields is applicable to all fields, but it should be noted that radio buttons require special treatment. Since a set of radio buttons represents a set of mutually exclusive choices, they belong to the same group. Because of this, the names of all radio buttons in the same group must be identical. In addition, the export values of the

set of radio buttons must be set with a single statement, in which an array of values are assigned by the exportValues property of the Field object. For example, suppose we would like to create a set of three radio buttons, each 12 points wide and 12 points high, all named myRadio. We will place them on page 5 of the document, and their export values will be Yes, No, and Cancel. They can be created as shown in the code given below:

var name = "myRadio";
var type = "radiobutton";
var page = 5;
var rb = this.addField(name, type, page, [400, 442, 412, 430]);
this.addField(name, type, page, [400, 427, 412, 415]);
this.addField(name, type, page, [400, 412, 412, 400]);
rb.exportValues=["Yes", "No", "Cancel"];
asnyder
I tried this in Acrobat 9, and it's not working for me. I created four checkbox fields, named school.a, school.b, ..., and then I checked two of the boxes. I then queried this.getField('school').value in the JS debugger, but it returns nothing (no error, no output).
Jason R. Coombs
Hmm, that's suppose to work according to their documentation. Is there a way to get all fields and iterate to see what each one is?
asnyder
It is possible to iterate over all fields (though it appears you can't iterate over just the fields of a group). The group construct seems useful mostly in manipulating widgets, but not the values (and especially the collective value) of the group. Unfortunately, the group construct lends very little to assisting in getting the aggregate value for a set of widgets... the programmer still has to know in advance which widgets are grouped and do any aggregation manually. The programmer can infer groups by name, but this construct is more a convention than a protocol.
Jason R. Coombs
+1  A: 

asnyder's response led me to the conclusion that there is no automatic way to handle multiple values within a single field (as one can with HTML). asnyder's examples come from Developing Acrobat Applications Using JavaScript, available from the Acrobat Javascript Developer Center. This document provides some examples of how to manipulate checkboxes, combo boxes, and radio buttons. All of the examples shed some light on the problem and ultimately led me to the conclusion that any system that is using PDF forms will have any multi-selectable groups implicitly defined.

Using the construct of groupName.fieldName appears to be useful to manipulate the widgets as a group (in Acrobat Javascript), but the fields of a group cannot be enumerated (without enumerating all fields and filtering for the groupName), and the collective value of that group cannot be determined without programatically inspecting the values.

In other words, a multi-selectable value is not an intrinsic feature of Acrobat nor of PDF in any substantial way, though it is possible to implement such a form through programming.

Jason R. Coombs