2

So I have a PDF doc that has 25 check boxes called "cb1" through "cb25". I would like to be able to count the number of boxes that are checked and put that count into a text box area called "Points".

I'm not very familiar with either JS or PDF form creation but from what I've been able to dig up I think I'm close to getting it to work.

I have added the following code to the document level:

function CountCheckBoxes(aFieldsNames) {
    // count field names that have been selected
    var count = 0;
    // loop through array of field names
    for (i = 0; i < aFieldNames.length; i++) {
      // for field names with a value of not Off increment counter
      if (this.getField(aFieldNames[i]).value != "Off") count++;
    } // end loop of field names
    // return count
    return count;
  } // end CountCheckBoxes

I've tried adding the following code text box properties to execute JS on mouse up and as a calculated value, neither of which seem to work to populate the text box with a count of checked boxes.

// var define field names to be tested
var aFields = new Array('cb1', 'cb2', 'cb3', 'cb4', 'cb5', 'cb6', 'cb7', 'cb8', 'cb9', 'cb10', 'cb11', 'cb12', 'cb13', 'cb14', 'cb14', 'cb15', 'cb16', 'cb17', 'cb18', 'cb19', 'cb20', 'cb21', 'cb22', 'cb23', 'cb24', 'cb25');
// count field names that have been selected
event.value = CountCheckBoxes(aFields);
digital_alchemy
  • 663
  • 4
  • 9
  • 19
  • Note that event.value is the value going into the field from which the script is called; if it is a button, the result goes nowhere, because the Button does not have a value property; you'd have to add the code to the text field where the sum should appear. Then it would work. – Max Wyss Jan 08 '16 at 00:17

1 Answers1

2

The code below should be added to the text field that keeps count of the boxes. To do so, right click on the form field then Properties -> Calculate -> Custom Calculation Script -> "Edit...".

var sum = 0;
for ( i = 1; i < 26; i++ ) {
        f = "cb" + i;
        field = getField(f);
        if (field.isBoxChecked(0)) {
            sum = sum + 1;
        }
    }
event.value = sum;

This is tested and working in an actual document. Here are some details about the code:

There is a loop that goes over all 25 fields, and creates a string for each one of their names. The string values are "cb1", "cb2" etc. Then gets the field by name. The isBoxChecked(0) field method, will return true if the box is checked. If a box is checked, the code will bump up the sum of all checked fields. When it's all done, the sum is assigned to the current text field.

Here is a link to the JS for Acrobat reference. It's quite useful when putting together samples like the one above.

Vel Genov
  • 10,513
  • 2
  • 16
  • 19
  • Thanks. This works but but with one minor issue... with no boxes checked it is starting the count at 1 and always is 1 higher than the actual number of checked boxes. I can always just subtract one from the event.value but I don't see in the code why the count would be off. – digital_alchemy Jan 06 '16 at 21:46
  • Try setting the default value of the field to "0". It's in the "Options" tab. I tested it with 5 boxes and it come out correct. – Vel Genov Jan 06 '16 at 21:53
  • NVM... There must have been a checked box that wasn't visible because I reset the form and it's accurate now. – digital_alchemy Jan 06 '16 at 22:21
  • A little warning about the isBoxChecked(): It works reliably only when there is exactly one widget of the checkbox; if there are multiple widgets (checkboxes with same name but different return values, or radiobuttons), the numbering of the widgets may be erratic, and in such a case, you better use the field.value approach. – Max Wyss Jan 08 '16 at 00:19