Search code examples
javascriptvariablesacrobat

Setting and accessing document-level variables in Acrobat 10 Pro


I have a form that currently functions based on in-field calculations. In order to get some of the calculations to work, I had to create hidden calculated fields and then calculate off of the those hidden fields to get other calculations.

(This is a loan application form, so there are many variables, such as credit score, interest rate based off credit score, loan term, etc.)

I want to move the hidden fields to document-level variables, but can't seem to make the other fields recognize the document-level variables, or calculate based on their (supposed) values.

For example, I have a field that is populated by a series of checkboxes. Right now, the MouseUp Event Action populates the htxtLoanType using the following script:

this.getField("htxt_LoanType").value = "0";

This and 2 other similar functions create the 3 values I need to access an array containing all the possible interest rate combinations, based on credit score, loan term and loan type.

I have tried to enter a variable (outside of a function) into the Document JavaScripts named "Variables" here...

enter image description here

var vLoanType;          // The array value of the current loan type...

I then try to set the value of 'vLoanType' with this script linked to the MouseUp EventScript of the checkbox:

//this.getField("htxt_LoanRequestType").value = "0";
vLoanType = "0";

The commented section works, since it assigns the value directly to the textfield. The vLoanType = "0"; doesn't seem to assign anything to the variable, since I can't get the variable to return a value to a text field.

If I try to enter

event.value = vLoanType;

into a text field's custom calculation script, it does nothing. It doesn't return the variable's value, which should be set to "0", and it doesn't display anything.

What am I missing regarding the setting and returning of document-level variables? I don't code professionally, so any help would be appreciated. Also, let me know if you need more information.


Solution

  • Variables declared outside of a function in a document level script (which is not the default when you create a new document level script) are scoped to the PDF document which means that any field can access them. By default, creating a new document level script via Acrobat Pro will create a stub function for you based on the name you entered in the dialog. Delete the function; it's not necessary.

    To display a variable's value in a text field, use the following code where "foo" is the variable name and "myField" is the name of the field in question.

    this.getField("myField").value = foo;