How to Sum Only Positive Form Values in PDF with Acrobat Script
Question details
The user needs to calculate a subtotal in a PDF form by summing only positive field values while ignoring any negative numbers.

- Product
- Device & OS
- not provided
- Scenario
- Calculating a taxable product subtotal on a PDF quote form where negative rebate values should not reduce the total sum.
- Observed behavior
- Standard built-in sum functions automatically include negative values, improperly reducing the total instead of treating them as zero.
Ensure you are using a PDF editor that supports form field editing and custom JavaScript, such as Adobe Acrobat Pro. Take note of the exact field names you wish to include in your sum calculation.
Apply a Custom JavaScript Calculation to the Total Field
Use a custom calculation script that iterates through your specific target fields, converting them to numbers and adding them to the total only if they are greater than zero.
By default, built-in PDF calculation tools perform straight mathematical sums. To apply conditional logic (like ignoring negative values), you must use JavaScript.
Open your PDF file in your advanced PDF editor and navigate to the 'Prepare Form' or 'Form Editing' tool area.
Right-click the text field where you want the final subtotal to appear and select 'Properties' from the context menu.
Navigate to the 'Calculate' tab in the Properties window. Select the option for 'Custom calculation script' and click the 'Edit' button.
In the JavaScript editor, paste the following code: var fieldsToAdd=["Text1","Text2","Text3"]; var total=0; for(var i=0;i<fieldsToAdd.length;i++){var v=Number(this.getField(fieldsToAdd[i]).valueAsString); if(v>0) total+=v;} event.value=total;
Replace "Text1", "Text2", and "Text3" in the script with your actual form field names. Click 'OK' to save the script, then close the Properties window and test your form.

Handle PDF Forms and Documents with WPS PDF
WPS Office includes a powerful PDF editor that allows you to effortlessly view, fill, and manage PDF forms. With comprehensive document tools, you can handle complex office tasks efficiently.
- 1. Open WPS Office: Launch the WPS Office application and click on the 'PDF Edit' module from the home screen.
- 2. Load your document: Open your PDF form or document within the application to start viewing or editing.
- 3. Fill the form: Use the interactive PDF tools to fill out text fields, select checkboxes, or apply secure digital signatures.
- 4. Save and export: Once your form is completely filled and calculated, click 'Save' or export it directly as a flattened PDF file.

Frequently Asked Questions
Why does standard PDF sum calculation include negative values?
The built-in sum function in PDF editors simply adds all raw mathematical values from the selected fields. To conditionally ignore negative numbers, you must override this behavior with a custom JavaScript snippet.
How do I find the exact field names in my PDF form?
Enter the 'Prepare Form' or form editing mode in your PDF software. The internal field names will be displayed as labels over each text box. You must type these exact names into your script array.
Will this JavaScript calculation work in web browsers or mobile PDF viewers?
Many basic web browser PDF viewers and default mobile apps do not support executing custom JavaScript in forms. For best results, users should fill the form using a dedicated desktop PDF reader.




