PDF Editing Issues

How to Run a PDF Calculation Script Based on a Radio Button

Muhammad TalhaMuhammad Talha Nov 5, 2026 869 views

Question details

Calculate an employee's departure date by adding a specific number of days (14 or 16) to an arrival date, depending on which radio button is selected in a PDF form.

How to Run a PDF Calculation Script Based on a Radio Button
Product
Adobe Acrobat / PDF Forms
Device & OS
not provided
Scenario
Creating an interactive PDF form that requires conditional date calculations based on user input.
Observed behavior
The calculated date field dynamically updates to show the correct departure date when the user selects either the 14-day or 16-day radio button.
Before you start

Ensure your PDF editor supports JavaScript for forms and that you have the exact field names for both your date input and radio buttons.

Solution 1Recommended

Use JavaScript and Radio Button Export Values for Date Calculation

Assign specific numerical export values to your radio buttons and use a custom calculation script to add those days to the base date.

To make this work, the radio button group needs to pass a numerical value to your calculation script. By setting the export values of the buttons to 14 and 16, the script can simply retrieve the value of the selected button and add it to the parsed arrival date.

1
Set Radio Button Export Values

Open the properties of your radio button group (e.g., 'num_days_rb'). In the Options tab, set the Export Value of the first button to 14, and the second button to 16.

2
Identify Field Names

Note the exact names of your fields. For this example, assume the base date field is 'Leave End Date Field' and the radio button group is 'num_days_rb'.

3
Add Custom Calculation Script

Open the properties of the field where the calculated date should appear. Go to the Calculate tab, select 'Custom calculation script', and click Edit.

4
Insert the JavaScript Code

Paste the calculation script to parse the date, add the radio button's value, and format the output. Make sure to account for empty fields or an 'Off' state when no button is selected.

Use JavaScript and Radio Button Export Values for Date Calculation
JavaScript Code Snippet: var date = util.scand('dd-mmm-yyyy', getField('Leave End Date Field').value); if (getField('Leave End Date Field').valueAsString === '') { event.value = ''; } else { var num_days = getField('num_days_rb').value; if (num_days !== 'Off') { date.setDate(date.getDate() + Number(num_days)); event.value = util.printd('dd-mmm-yyyy', date); } else { event.value = ''; } }
Advanced PDF Editing

Create Interactive PDF Forms with WPS Office

WPS Office includes a robust PDF editor capable of handling interactive forms, inserting radio buttons, and managing field properties efficiently.

  1. 1. Open your PDF: Launch WPS Office and open your existing PDF document or create a new one.
  2. 2. Access Form Controls: Navigate to the Form tab on the top ribbon to view all interactive controls.
  3. 3. Insert Radio Buttons: Select the Radio Button tool and click on the document to place your interactive options.
  4. 4. Configure Field Properties: Right-click the inserted field to adjust its export values, names, and custom formatting.
Create and edit interactive PDF forms easilyFully compatible with standard Adobe Acrobat PDF formatsLightweight all-in-one suite for PDF, Word, Excel, and PowerPoint
microsoft office alternative - wps office

Frequently Asked Questions

Why is my PDF calculation script returning an error or blank value?

This usually happens if the field names in your JavaScript do not exactly match the field names in your PDF form. Field names are case-sensitive. Check for typos in your 'getField' function.

How do I handle a scenario where no radio button is selected?

When no radio button is selected, its value defaults to 'Off'. You should add a conditional 'if' statement in your JavaScript to check if the value is 'Off', and if so, prevent the script from attempting to calculate the date.

Can I use this calculation script for different date formats?

Yes. You can modify the 'dd-mmm-yyyy' string in both the util.scand (which reads the date) and util.printd (which outputs the date) functions to match your desired format, such as 'mm/dd/yyyy'.