Fix an Acrobat PDF Calculation Script After Reopening a Form
Question details
Fix a custom calculation in an Acrobat PDF form that stops working after the file is reopened or the form is cleared.

- Product
- Adobe Acrobat
- Device & OS
- not provided
- Scenario
- A PDF form divides one field value by another using a custom JavaScript calculation.
- Observed behavior
- The calculation initially works but fails after the PDF is reopened or the form is cleared. The script does not handle empty, invalid, or zero values.
Open the PDF in Acrobat and confirm the exact names of the numerator and divisor fields in Prepare a Form. Check whether commas in the field values are thousands separators or decimal separators before changing the script.
Validate the field values in the Acrobat calculation script
Use this method to diagnose JavaScript errors and prevent invalid values or a zero divisor from breaking the calculation.
The example below removes commas as thousands separators. Do not use that conversion unchanged if your form uses commas as decimal separators.
Open the PDF in Acrobat and press Ctrl+J to open the JavaScript console. Reproduce the problem by clearing or reopening the form, then check the console for errors.
Open Prepare a Form, select the calculated result field, and open its Properties. On the Calculate tab, select Custom calculation script and click Edit.
In the script editor, replace the existing calculation with the following. Change the two field names to match your form exactly: var numeratorField = this.getField("Remaining Balance 3"); var divisorField = this.getField("Divide by 2"); var numeratorText = numeratorField ? numeratorField.valueAsString.replace(/,/g, "") : ""; var divisorText = divisorField ? divisorField.valueAsString.replace(/,/g, "") : ""; var numerator = Number(numeratorText); var divisor = Number(divisorText); if (numeratorText.replace(/\s/g, "") !== "" && divisorText.replace(/\s/g, "") !== "" && isFinite(numerator) && isFinite(divisor) && divisor !== 0) { event.value = numerator / divisor; } else { event.value = ""; }
Confirm the script and field properties, then use File > Save. Test valid values, an empty field, a zero divisor, and a value with a thousands separator; clear the form and reopen the PDF to test again.

Work with PDFs and office files in WPS Office
WPS Office is a free, lightweight alternative with a familiar interface for working with PDFs and Microsoft Office files. Its format support helps make moving everyday documents between office suites straightforward. Use Acrobat to diagnose and edit the custom PDF JavaScript described above.

Frequently Asked Questions
Where can I find errors in an Acrobat calculation script?
Press Ctrl+J in Acrobat to open the JavaScript console, then reproduce the problem and inspect the reported errors.
Why does clearing the form affect the calculation?
Clearing the form can leave the numerator or divisor empty. The script should check both values before dividing and clear the result when the inputs are invalid.
Can a comma in a number cause a calculation error?
Yes. JavaScript's Number conversion does not automatically interpret a value such as 1,000 as a number. Remove commas only when they represent thousands separators; decimal-comma formats need different handling.
Where do I edit a custom calculation in Acrobat?
Open Prepare a Form, open the calculated field's Properties, and choose Calculate > Custom calculation script > Edit.




