PDF Editing Issues

How to Use JavaScript to Sum Values in a Multiline PDF Field

Bushra ParveenBushra Parveen Oct 28, 2026 869 views

Question details

The user needs a JavaScript calculation to add up every numeric line typed into a single multiline text field in a PDF form.

Product
PDF Forms
Device & OS
not provided
Scenario
Setting up a custom form calculation in a PDF editor to process multiple numbers entered on separate lines within one specific field.
Observed behavior
The original script failed to correctly reference and sum the individual array elements generated by the multiline input.
Before you start

Ensure you are using a PDF editor that supports form field properties and custom calculation scripts, and confirm you know the exact Field Name of your multiline text box.

Solution 1Recommended

Use valueAsString and split() to Calculate the Array

This solution extracts the raw string from the multiline field, splits the lines using a carriage return, and loops through the array to sum the values.

In PDF JavaScript, multiline text fields separate lines using carriage returns rather than standard line feeds. By utilizing the valueAsString property and splitting by "\r", you can accurately isolate each numeric entry.

1
Access Field Properties

Open your PDF in form editing mode, right-click the target field where you want the total to appear, and select 'Properties'.

2
Enable Custom Calculation

Navigate to the 'Calculate' tab, select the 'Custom calculation script' option, and click the 'Edit' button.

3
Insert the JavaScript Code

Enter the following code into the JavaScript editor: var values = this.getField("FieldName").valueAsString.split("\r"); var total = 0; for (var i in values) total += Number(values[i]); event.value = total;

4
Update the Field Name

Replace 'FieldName' in the script with the exact name of your multiline input field, then click 'OK' and close the properties window to test your form.

Use valueAsString and split() to Calculate the Array
Script Formatting Tip: Ensure that the multiline field strictly contains numeric values on each line to avoid 'NaN' (Not a Number) calculation errors.
Advanced PDF Form Management

Edit and Process PDF Forms Smoothly with WPS Office

WPS Office provides a comprehensive PDF editor that allows you to manage interactive form fields, edit text, and handle various document calculation tasks effortlessly.

  1. 1. Open the PDF File: Launch WPS Office and open the PDF form document you wish to edit.
  2. 2. Navigate to Form Tools: Go to the 'Form' tab in the top ribbon to access interactive form field options.
  3. 3. Edit Field Properties: Double-click any text field to modify its properties, input settings, or calculations.
  4. 4. Save Your Changes: Click 'Save' to preserve your form settings and ensure all scripts function properly.
Easily fill, edit, and manage interactive PDF forms.Highly compatible with Adobe Acrobat PDF formats.Lightweight software with an intuitive, user-friendly interface.Supports extensive text editing, page management, and annotations.
microsoft office alternative - wps office

Frequently Asked Questions

Why does splitting by \n fail in a PDF multiline field?

Adobe Acrobat and most PDF standards encode line breaks in multiline text fields as carriage returns (\r) instead of newlines (\n). Using split("\n") will not correctly separate the array items.

How do I enable multiple lines in a PDF text field?

In your PDF form editor, open the Text Field Properties, go to the 'Options' tab, and check the box for 'Multi-line'.

Can I sum form fields without using custom JavaScript?

Yes, but only if your numbers are in separate text fields. You can go to the 'Calculate' tab and choose 'Value is the sum of the following fields', then select the specific fields you want to add together.