Now that the data has been acquired it can be used in a calculation. Data should be tested at the time it is acquired, however data testing is outside the scope of this article. Or, in date calculations, an input value must be a legal data value. For example, in the case where a value is used in a division, an input of zero cannot be used as a divisor. It is very often the case the data needs to be tested before it is used in a calculation. The script here is so short it doesn't really matter, but this naming prefix is critical for writing long and complex scripts. This is important for being able to understand how a particular variable fits into a script. Whenever I create variables I always prefix them with a type notation. It begins with the lower case letter "n". Notice that I've chosen a particular notation for the variable name. This entire line of code creates a variable named nA and assigns it the numerical value that is in the InputA field. If two text values are added together, the result would be the concatenation of the two text values, rather than the addition of two numbers. JavaScript is a bit squirrely when it comes to adding numbers and text. So the next piece of code to understand is the Number() function, which converts text into a number. However, the value property is unpredictable in what it returns, the valueAsString property gives us a known type, which is helpful if the field contains a blank.īut of course, what we really need is a number, because we're doing a numeric calculation. We could have also used the code - this.getField("InputA").value, and you'll see this done quite often in Acrobat scripts. You can find out more about field naming on the PDF Form Scripting page. Field naming is a topic all by itself and is very important when dealing with complicated forms and calculations. It is important to note that this part of the script will fail if field names in the script do not exactly match the field names on the form, including upper case letters. When you use it in your own scripts be careful to make sure the name of your particular field is enclosed in quotes. The important part to know here, is that the code - this.getField("InputA").valueAsString, returns the value of the InputA field as a text string. We're not going to get deep into the details of JavaScript coding, but we are going to have to cover a few things. One field is named InputA and the other is named InputB. For this script the data is acquired from two fields. There are other places where data can be acquired, but let's stick with the simplest case. Generally, the values used in a PDF form calculation are acquired from other form fields. However, not only is it instructive to explicitly show the individual parts, but these parts need to be separate for more complicated calculations.Īlso, while the calculation is simple, there is actually a lot going on here. This calculation is so simple that it could easily be done in a single line of code. Var nB = Number(this.getField("InputB").valueAsString) Var nA = Number(this.getField("InputA").valueAsString) Assign the calculation result to the field value.Combine the inputs using mathematical operators.Acquire the input values for the calculation. ![]() Sometimes these parts are combined into a single line of code, but all three parts are still there. Every calculation script has at least the three parts listed below. Now we can get down to writing the actual script.
0 Comments
Leave a Reply. |