How to Multiply the Sum of Two PDF Form Fields by 2
Question details
The user needs to calculate the sum of two specific PDF form fields and multiply that total by a fixed value (2) using a custom script, while avoiding formatting errors when inputs are blank.

- Product
- PDF Editors
- Device & OS
- not provided
- Scenario
- Creating an interactive PDF form that requires automatic mathematical calculations based on user input.
- Observed behavior
- The user successfully implemented a calculation script but encountered 'NaN' (Not a Number) or formatting mismatch errors when the source fields were left blank prior to form completion.
Ensure you know the exact names of the source form fields you intend to add together, as you will need to reference them identically in your calculation script.
Apply a Custom JavaScript for PDF Field Calculation
Use a custom calculation script within the result field's properties to add two source fields, multiply their sum by 2, and automatically output the result.
PDF form calculations rely on JavaScript to fetch values from other fields. By converting the field values to numbers, you can perform standard arithmetic operations reliably.
Enter form editing mode in your PDF editor. Right-click the result field where you want the final number to appear, and select 'Properties' from the context menu.
In the Properties dialog box, navigate to the 'Calculate' tab. Choose the 'Custom calculation script' option and click the 'Edit' button.
Paste the following script into the editor: var v1 = +getField("Text1").value; var v2 = +getField("Text2").value; event.value = 2 * (v1 + v2); Replace "Text1" and "Text2" with the actual names of your input fields.
Navigate to the 'General' tab in the Properties window and check the 'Read-Only' box. This ensures that users cannot accidentally overwrite the automated calculation.

Prevent 'NaN' and Formatting Errors on Blank Fields
Modify your calculation approach to handle empty input fields gracefully, preventing annoying pop-up errors indicating format mismatches.
Manage Interactive PDF Forms with WPS PDF
WPS PDF offers a comprehensive suite of tools for viewing, filling, and editing interactive PDF forms. Whether you need to input data or manage document structures, WPS PDF provides a lightweight and highly compatible solution.
- 1. Open Your PDF Form: Launch WPS Office and open your interactive PDF document using the WPS PDF module.
- 2. Access Form Tools: Navigate to the 'Form' tab located on the top ribbon to view all available form fields and editing options.
- 3. Fill or Modify Fields: Click on any highlighted text field, checkbox, or dropdown menu to enter data, or use the editing tools to modify the document layout.
- 4. Save and Export: Once your form is complete, click 'Save' or 'Export to PDF' to secure your inputs in a universally compatible format.

Frequently Asked Questions
How do I find the exact name of a PDF form field?
In form editing mode, right-click the specific field and select 'Properties'. Under the 'General' tab, you will see the exact 'Name' of the field. This is the exact name you must use in the getField("FieldName") part of your script.
Why does my calculated field show 'NaN' or trigger an error message?
This usually happens when the script attempts to calculate a mathematical formula using empty fields or text strings instead of numbers. To fix it, ensure your script checks if the input fields are blank before performing the calculation.
Do I need to know advanced JavaScript to calculate PDF forms?
No, standard calculations like addition or multiplication only require basic JavaScript snippets. For simple sums or products, some PDF editors even offer built-in simplified field calculation options under the 'Calculate' tab without needing custom code.




