How to Auto-Populate a PDF Field Using JavaScript
Question details
The user needs to automatically populate a numeric PDF field (like a cost field) with a specific value, but only if another designated text field (like a name field) is not empty.

- Product
- PDF Editor
- Device & OS
- not provided
- Scenario
- Creating or editing an interactive PDF form where certain field values dynamically depend on user input in other specific fields.
- Observed behavior
- The target field should mirror another field's value when the associated text field is filled, and remain blank if the text field is empty.
Ensure your PDF editing software supports interactive form fields and custom JavaScript execution. Verify the exact names of your form fields before applying the script, as JavaScript is case-sensitive.
Add a Custom JavaScript Calculation to the Target Field
Use JavaScript within the field's calculation properties to conditionally retrieve and display values from other fields.
By utilizing custom calculation scripts, you can make PDF forms dynamic and responsive. This method involves extracting the string value of the condition field and the numeric value of the source field, then applying an IF statement to determine the target field's output.
Open your interactive PDF document in your preferred PDF editor and navigate to the Form Editing or Prepare Form tools.
Right-click on the target field you want to auto-populate (e.g., 'TRVL 2 Trans Cost') and select 'Properties' from the context menu.
In the Field Properties dialog box, click on the 'Calculate' tab to view calculation options.
Select the 'Custom calculation script' option and click the 'Edit' button. Enter the following script, making sure to replace the field names with your actual field names: var Tname = this.getField("TRVL 2 Name").valueAsString; var Tcost = this.getField("TRVL 1 Trans Cost").value; if(Tname != "") event.value = Tcost; else event.value = "";
Click 'OK' to close the JavaScript editor, then close the Properties window. Exit form editing mode and test the form by typing into the name field to see if the cost field populates correctly.

Easily Create and Manage Interactive PDF Forms in WPS Office
WPS Office provides comprehensive PDF editing tools, allowing you to seamlessly create interactive forms, edit text, and manage form fields. It offers an intuitive interface for formatting your PDF documents without the complexity of heavier software.
- 1. Open PDF in WPS Office: Launch WPS Office and open your PDF document.
- 2. Access Form Tools: Navigate to the 'Form' tab on the top ribbon to view all form creation and editing tools.
- 3. Edit Form Fields: Click on 'Text Field', 'Check Box', or other form controls to insert them, and double-click existing fields to modify their properties.
- 4. Save Document: Once your form is configured, click 'Save' to preserve the interactive elements for your users.

Frequently Asked Questions
Why is my JavaScript calculation not updating automatically in the PDF?
Check if your PDF viewer has JavaScript enabled. In many PDF readers, JavaScript execution is disabled by default for security reasons. Additionally, ensure the field calculation order is correct if multiple calculated fields depend on one another.
How do I find the exact name of a PDF form field?
Enter Form Editing mode in your PDF editor, right-click the specific field, and select 'Properties'. The exact field name will be displayed in the 'General' tab. You must use this exact name in your script, as JavaScript is strictly case-sensitive.
Can I use this script to perform mathematical calculations like addition or multiplication?
Yes. You can modify the script to define multiple variables representing different fields, and apply mathematical operators (such as +, -, *, /) to those variables before assigning the final calculated result to 'event.value'.




