How to Hide a Calculated PDF Field Until Input is Provided
Question details
The user wants a calculated PDF form field to remain completely blank until a specific referenced input field receives data, rather than evaluating prematurely.

- Product
- PDF Editor
- Device & OS
- not provided
- Scenario
- Creating an interactive PDF form with dynamic math calculations where dependent fields should appear empty by default.
- Observed behavior
- The calculated field automatically evaluates the formula even when referenced fields are empty, resulting in a zero or an unintended value being displayed.
Ensure you have a PDF editor that supports JavaScript for interactive form fields, and identify the exact field names used in your calculation before proceeding.
Apply a Custom JavaScript Calculation
Use a custom JavaScript condition to verify if the input fields are blank before executing the calculation, outputting an empty string if they are.
By default, PDF form calculators treat empty numeric fields as zero, which triggers the calculation prematurely. Using the valueAsString property in a custom script allows the PDF to verify if text actually exists in the field before doing the math.
Open your PDF in form editing mode, right-click the calculated field (e.g., field B3), and select 'Properties'.
Navigate to the 'Calculate' tab in the Properties dialog box and select the 'Custom calculation script' option.
Click the 'Edit' button and paste the following JavaScript: var s1 = this.getField("B2").valueAsString; var s2 = this.getField("A3").valueAsString; if (s1 && s2) { var v1 = Number(s1); var v2 = Number(s2); event.value = Math.ceil(v1 - v2); } else { event.value = ""; }
Click 'OK' to close the JavaScript editor, then close the Properties window. Switch to the hand/preview tool and test your form to ensure the field stays blank until both B2 and A3 have inputs.

Create Interactive PDF Forms with WPS Office
WPS Office features a powerful PDF editor that allows you to easily design professional forms, manage field properties, and execute custom JavaScript calculations without hassle.
- 1. Open the PDF File: Launch WPS Office and open your PDF document.
- 2. Access Form Tools: Navigate to the 'Form' tab on the top ribbon to access the interactive form creation tools.
- 3. Modify Field Properties: Double-click any text field or right-click and select 'Properties' to access the Calculate tab and input your scripts.

Frequently Asked Questions
Why does my calculated PDF field show a zero when the input is blank?
Standard PDF behavior treats empty numeric fields as zero. When your calculation runs, it calculates using zero, thus outputting a result. Implementing a JavaScript check for an empty string prevents this.
Can I use this script for operations other than subtraction?
Yes. Once the script verifies the fields are not blank, you can replace the math logic (e.g., Math.ceil(v1 - v2)) with any other operation like addition (v1 + v2) or multiplication (v1 * v2).
How do I find the exact field names in my PDF?
When you enter form editing mode in your PDF editor, field names are usually displayed as labels over the text boxes. You can also confirm the exact name under the 'General' tab in the field's Properties menu.




