How to Combine Non-Empty PDF Form Fields Using JavaScript
Question details
The user wants to create a summary field in a PDF form that combines user-entered comments from multiple fields while ignoring any empty fields or default placeholder values.

- Product
- PDF Editor
- Device & OS
- not provided
- Scenario
- Creating a summary field in a PDF form to aggregate user input dynamically.
- Observed behavior
- Need a custom calculation script to iterate through form fields and concatenate only those containing text.
Ensure you have a PDF editor that supports JavaScript for form fields, and verify that your target input fields are named sequentially (e.g., Comment-1, Comment-2).
Use a Custom JavaScript Calculation Script
Apply a custom JavaScript code to the summary field to loop through the target fields and combine their values if they are not empty.
This script uses a loop to check a series of sequentially named fields. It retrieves the value of each field as a string and appends it to the summary text only if the string is not empty.
Right-click the summary text field where you want the combined text to appear and select 'Properties'.
In the Properties dialog box, click on the 'Calculate' tab.
Select 'Custom calculation script' and click the 'Edit' button. Paste the following code: var text = ""; for (var i = 1; i <= 30; i++) { var s = this.getField("Comment-" + i).valueAsString; if (s != "") text += "Comments-" + i + ": " + s + "\r"; } event.value = text;
Modify the script to match your actual field names (e.g., change 'Comment-' to your field prefix) and adjust the loop limit (e.g., '30') to match the total number of fields you are checking.
Click 'OK' to save the script, close the Properties dialog, and test the form by entering data into the comment fields.

Create and Manage Interactive PDF Forms with WPS Office
WPS Office provides a comprehensive PDF editor that allows you to create interactive forms, add text fields, and manage form properties efficiently. It's a lightweight and powerful alternative for handling your daily PDF tasks.
- 1. Open PDF in WPS Office: Launch WPS Office and open your PDF document.
- 2. Access Form Tools: Navigate to the 'Form' tab in the top ribbon to access form creation and editing tools.
- 3. Add or Edit Fields: Click on 'Text Field' to add new fields or double-click existing fields to modify their properties and calculations.

Frequently Asked Questions
Why is my JavaScript calculation not updating automatically?
Ensure that the calculation order is set correctly in your PDF editor. If the summary field calculates before the input fields are updated, it may not reflect the latest changes. You can adjust the calculation order in the form editing preferences.
Can I use this script for fields with different names?
The provided script relies on a sequential naming pattern (e.g., Comment-1, Comment-2). If your fields have completely different names, you will need to manually retrieve each field using `this.getField("ExactFieldName").valueAsString` instead of using a `for` loop.
How do I add a separator between the combined values?
In the script, the `\r` adds a carriage return (new line) after each entry. You can change this to a comma and space by replacing `"\r"` with `", "` in the script.




