PDF Editing Issues

How to Calculate a Percentage Between Two PDF Form Fields

Huda QurayshiHuda Qurayshi Oct 28, 2026 868 views

Question details

The user needs to calculate the percentage difference between two PDF form fields without triggering a format error when the denominator field is empty or zero.

How to Calculate a Percentage Between Two PDF Form Fields
Product
PDF Editor
Device & OS
not provided
Scenario
Creating or editing an interactive PDF form that requires automated percentage calculations based on user input.
Observed behavior
The calculation produces a "Format does not match" warning when the second field is empty or zero because division by zero is not allowed.
Before you start

Ensure you are using a PDF editor that supports JavaScript for custom field calculations, and note the exact names of your source form fields (e.g., 'amount1' and 'amount2').

Solution 1Recommended

Use a Custom JavaScript Calculation to Prevent Division by Zero

Apply a custom JavaScript code to the result field that checks if the denominator is zero before performing the percentage calculation.

A standard simple calculation in a PDF form cannot handle conditional logic, which leads to errors when users leave a denominator field blank. By writing a document-level or field-level calculation script, you can convert input values to numbers and safely handle empty fields.

1
Access Field Properties

Open your PDF form in edit mode. Right-click the target field where the percentage result should appear and select 'Properties'.

2
Navigate to the Calculate Tab

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

3
Enter the Custom Script

Select 'Custom calculation script' and click 'Edit'. Input the following script: `var a1 = Number(this.getField("amount1").valueAsString); var a2 = Number(this.getField("amount2").valueAsString); if (!a2) event.value = ""; else event.value = 100 * (a2 - a1) / a2;`

4
Set the Number Format

Go to the 'Format' tab, change the format category to 'Number', and choose your preferred decimal places. Click 'Close' to save the properties.

Use a Custom JavaScript Calculation to Prevent Division by Zero
Match Form Field Names: You must replace 'amount1' and 'amount2' in the script with the exact, case-sensitive names of the fields in your actual PDF document.
Advanced PDF Form Editing

Easily Create and Calculate PDF Forms with WPS Office

WPS PDF provides robust tools for creating interactive PDF forms, including complete support for custom JavaScript calculation scripts. You can quickly set up complex formulas and prevent format errors seamlessly.

  1. 1. Open PDF Form Editor: Launch WPS Office, open your PDF document, and navigate to the 'Form' tab.
  2. 2. Insert Form Fields: Add text fields for your initial amounts and a designated text field for the percentage result.
  3. 3. Apply Custom Script: Right-click the result field, select 'Properties', go to the 'Calculate' tab, and paste your custom JavaScript to prevent division by zero.
  4. 4. Save and Test: Save your PDF document and test the form by entering numbers to ensure the percentage calculates properly without warnings.
Full support for advanced JavaScript in PDF form calculationsHighly compatible with Adobe Acrobat PDF forms and formatsUser-friendly interface for designing interactive form fieldsComprehensive PDF editing, merging, and conversion capabilities
microsoft office alternative - wps office

Frequently Asked Questions

Why do I get a "Format does not match" warning in my PDF form?

This warning occurs when a calculation formula attempts to divide by zero or process an empty field, resulting in an invalid number format. Using a custom JavaScript to check for zero or empty values prevents this error.

Can I format the calculation result with a percentage sign?

Yes. In the Field Properties under the 'Format' tab, select 'Percentage' instead of 'Number'. Note that the Percentage format automatically multiplies the value by 100, so you should remove the `100 *` multiplier from your custom JavaScript.

Are JavaScript calculations in PDFs supported on mobile devices?

Advanced PDF JavaScript calculations, including custom field scripts, are primarily supported on desktop PDF editors like WPS Office or Adobe Acrobat. Mobile PDF viewers may not execute these scripts correctly.