How to Use a PDF Calculation Script to Display Fields Based on Codes
Question details
The user wants to map input codes to corresponding source fields so that entering a specific code automatically fills a target data field with the source field's value.

- Product
- Device & OS
- not provided
- Scenario
- Creating interactive PDF forms using JavaScript for dynamic field population based on user input.
- Observed behavior
- Form fields require an automated script to conditionally fetch and display data from predefined source fields when a user types a matching code.
Ensure your PDF editor supports JavaScript for interactive forms and that you have mapped out the exact, case-sensitive names of your source and destination fields.
Apply a Custom Validation Script to the Code Field
Use a custom JavaScript on the input field itself to fetch the value from the requested source field immediately upon data entry.
This method executes a script as soon as the user enters a code and commits the value. It reads the input, looks for a source field with a matching name, and replaces the entered code with the target value.
Open your PDF form in edit mode, right-click the first code input field (e.g., Code1), and select "Properties".
Click on the "Validate" tab and select the option to run a custom validation script.
Click "Edit" and paste the following script: if (event.value) { var source = this.getField(event.value); if (source != null) event.value = source.valueAsString; }
Click "OK" to save the script. Repeat this process for the remaining fields (Code2 through Code12).

Use a Custom Calculation Script in the Destination Data Field
Instead of validating the input, you can place a calculation script in the target field to continuously pull data based on what is typed in the code field.
Edit PDF Forms and Add Scripts Easily with WPS Office
WPS Office provides a robust set of PDF tools, including form creation, interactive field editing, and seamless handling of complex PDF functions for free.
- 1. Download and Install: Download and install WPS Office on your computer, then open your interactive PDF document.
- 2. Access Form Tools: Navigate to the "Form" tab on the top ribbon to access advanced form editing tools.
- 3. Edit Field Properties: Double-click any form field to open its properties menu, where you can easily apply calculations, validations, and custom scripts.

Frequently Asked Questions
Why is my PDF calculation script not working?
Ensure that JavaScript is enabled in your PDF viewer's preferences. Additionally, check for typos in field names within your script, as JavaScript is strictly case-sensitive.
Can I use calculation scripts on a mobile PDF viewer?
Most mobile PDF viewers do not fully support complex JavaScript calculations or custom validations. For the best experience, fill out interactive PDF forms on a desktop application like WPS Office.
How do I hide the source fields in my PDF form?
You can hide source fields by opening the field properties, going to the "General" tab, and changing the "Form Field" visibility setting from "Visible" to "Hidden". The script will still be able to pull data from hidden fields.




