How can I add javascript to a PDF document to count the number of checked boxes and enter the value into a textbox area?

大城市里の小女人 提交于 2019-12-04 16:44:26

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!