Copy link to clipboard
Copied
Hi all, I'm using Acrobat DC Pro 2022.001.20112 and this is my first time doing any javascript in Acrobat although I'm otherwise comfortable with ExtendScript from other Adobe apps. I'll explain my set up first and then explain my problem.
I've set up a document with 2 pages and some form elements on each page. Page 1 has a few Checkboxes with no calculations. Page 2 has a bunch of text fields that each have a javascript calculation that calls a document-level javascript function to do a simple calculation. The function itself works fine, but I'll post it here just in case it has a bearing on my problem:
function getBudgetCostDifference() {
console.println(event.target.name);
var index = Number(event.target.name.split('.')[1]),
val1 = Number(this.getField('budget.' + index).value),
val2 = Number(this.getField('cost.' + index).value),
result = 0;
if (val1 === val1 && val2 === val2)
result = val1 - val2;
event.value = result;
console.println(' done!');
}
So there are about 20 fields on page 2 that have the following calculation:
getBudgetCostDifference();
This set up works perfectly and the calculations work fine... except:
When I click a checkbox on page 1 the form on page 2 is re-calculated. Every time. Even though nothing is happening to the form on page 2 I see the console output from the above function. The field names are totally different: checkbox.1 checkbox.2 etc. on page 1 and budget.1 cost.1 difference.1 etc. on page 2.
So anyone know what's going on here? Is this normal behaviour? If the forms are treated as a single form, can I logically separate them? My ideal aim is for the javascript function to only be executed when the form fields values it refers to are modified.
Any help is much appreciated!
- Mark
1. There is only one form, made up of all of the form fields on all of the pages. There is no way to subdivide into logically separate forms.
2. All calculation scripts run after any change. Acrobat has no way to know the dependencies between fields (since scripts can refer to field names dynamically), so it runs them all in calculation order.
3. Except for occasional performance problems, this shouldn't be an issue. Calculation scripts run a LOT so they should be efficient and without side effe
Copy link to clipboard
Copied
1. There is only one form, made up of all of the form fields on all of the pages. There is no way to subdivide into logically separate forms.
2. All calculation scripts run after any change. Acrobat has no way to know the dependencies between fields (since scripts can refer to field names dynamically), so it runs them all in calculation order.
3. Except for occasional performance problems, this shouldn't be an issue. Calculation scripts run a LOT so they should be efficient and without side effects.
Copy link to clipboard
Copied
Thanks @Test Screen Name, excellent clarification! Knowing that, I'll do some actual tests and, if performance seems lacking, I'll explicitly specify each field by name rather than working it out dynamically which will remove a few steps.
Do you know whether there are performance advantages to using "Simplified field notation" over hard-coded javascript calculation?
I realise I may be over-thinking things.
- Mark
Copy link to clipboard
Copied
I've never really checked, but I *think* that Simplified Field Notation is turned into JavaScript behind the scenes, then executed. Your script does not look too expensive - this starts to be an issue if
- the calculations have large overhead
- there are a large number of form fields
- a large number of calculations are triggered as side effects, for example needlessly setting the values of other fields in calculate scripts, which will trigger further calculations, calling all calculate scripts...
Also, do check the calculation order carefully. Don't assume it will just run from top to bottom.
Copy link to clipboard
Copied
Thanks again @Test Screen Name. Yes I appreciate the mention of calculation order—it was definitely something I had to adjust.
- Mark
Copy link to clipboard
Copied
if (val1 === val1 && val2 === val2)
Why does you use this if?
Copy link to clipboard
Copied
This checks for NaN. It's a weird fact that in JavaScript NaN ≠ NaN. So a var containing NaN won't equal itself.
EDIT: See try67's comment below. I didn't realise there was a built-in method. Best to use that. I'll change my line to
if (isNaN(val1) && isNaN(val2))
- Mark
Copy link to clipboard
Copied
Use the built-in isNaN() method for that.
Copy link to clipboard
Copied
Thank you! That is much better! 🙂