PDF Editing Issues

How to Combine Non-Empty PDF Form Fields Using JavaScript

Natalie TaylorNatalie Taylor Nov 3, 2026 869 views

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.

How to Combine Non-Empty PDF Form Fields Using JavaScript
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.
Before you start

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).

Solution 1Recommended

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.

1
Open Form Field Properties

Right-click the summary text field where you want the combined text to appear and select 'Properties'.

2
Navigate to the Calculate Tab

In the Properties dialog box, click on the 'Calculate' tab.

3
Enter Custom Calculation Script

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;

4
Adjust Field Names

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.

5
Save and Test

Click 'OK' to save the script, close the Properties dialog, and test the form by entering data into the comment fields.

Use a Custom JavaScript Calculation Script
Field Naming Convention: This script requires your input fields to be named with a consistent prefix followed by a sequential number (e.g., Comment-1, Comment-2).
Edit PDF Forms Easily

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. 1. Open PDF in WPS Office: Launch WPS Office and open your PDF document.
  2. 2. Access Form Tools: Navigate to the 'Form' tab in the top ribbon to access form creation and editing tools.
  3. 3. Add or Edit Fields: Click on 'Text Field' to add new fields or double-click existing fields to modify their properties and calculations.
Supports creating and editing interactive PDF formsHighly compatible with Adobe Acrobat PDF formatsLightweight and fast performanceUser-friendly interface for managing form field properties
microsoft office alternative - wps office

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.