How to Rename PDF Files Based on Form Answers Using Acrobat JavaScript
Question details
The user wants to use Acrobat JavaScript to automatically generate a PDF filename that includes the date, the user's name, and specific indicators if any of the nine Yes/No radio buttons are marked 'Yes'.

- Product
- Adobe Acrobat
- Device & OS
- not provided
- Scenario
- Automating the saving process of a PDF form where the output filename dynamically reflects specific user responses, such as symptom indicators from radio buttons.
- Observed behavior
- The user needs a script to iterate through multiple form fields, check for 'Yes' responses, build a custom filename suffix, and safely pass it to an existing save function while ensuring field validation and user privacy.
Ensure you have Adobe Acrobat Pro or a PDF editor that supports document-level JavaScript execution. Before testing new scripts, create a backup of your original PDF form to avoid accidental data loss or irreversible formatting errors.
Implement JavaScript to Loop Through Radio Buttons and Save
Use a custom JavaScript function to check required fields, extract 'Yes' responses from radio buttons, and dynamically construct the PDF filename.
This script iterates through form fields 1 through 9. It checks if the required fields are filled and highlights missing fields in red. If all conditions are met, it builds a filename suffix using the export values of the 'Yes' responses and triggers the save function.
Privacy Note: If the PDF form is used on a public or shared device (like a kiosk), avoid using explicitly descriptive export values (such as actual symptoms) in the filename, as this information is visible in the application's title bar to subsequent users.
In Adobe Acrobat, go to 'Prepare Form', right-click your custom submit or save button, select 'Properties', navigate to the 'Actions' tab, choose 'Run a JavaScript' from the drop-down menu, and click 'Add'.
Input a loop (e.g., for (var i = 1; i < 10; i++)) to iterate through your 9 radio button fields. Add logic to check if this.getField(i).value is 'Off'. If it is, prompt an app.alert asking the user to fill out required fields, change the field's strokeColor to color.red, and break the loop.
In the else block, initialize an empty string for the suffix. Add a condition that if this.getField(i).value is not 'No', append this.getField(i).valueAsString to the suffix string. This extracts the export value of the selected radio button.
Combine the Date field, Name field, and your newly created suffix into a single string. Pass this combined string to your trusted save function (e.g., mySaveAs(this, directory, filename + '.pdf')).
To remove the red validation outlines once the user corrects their input, add an 'On Focus' action to each radio button with the following code: event.target.strokeColor = color.black;.

Easily Manage PDF Forms and Documents with WPS Office
While complex Acrobat JavaScript automation requires specific environment setups, WPS Office offers a powerful, lightweight, and completely free alternative for managing, editing, and converting PDFs alongside Word, Excel, and PowerPoint files. Enjoy a seamless user interface and robust format compatibility.

Frequently Asked Questions
Why is my PDF JavaScript failing to save the document dynamically?
Your script might be failing due to PDF reader security restrictions. Functions like saveAs or custom trusted save functions typically require folder-level JavaScript privileges or a trusted context to execute properly, preventing malicious scripts from saving files silently.
How can I protect user privacy when generating PDF filenames from form data?
If your form is used on a shared device, do not use raw sensitive data (like medical symptoms or financial numbers) as the export values for your radio buttons. Instead, use coded values (e.g., 'Code1', 'Code2') so the resulting filename does not expose private information in the application's title bar.
How do I validate PDF form fields using JavaScript before saving?
You can iterate through your required fields using a for loop. Check if the field's value matches the default unselected state (like 'Off' or 'No'). If it does, trigger an app.alert(), change the field's strokeColor to red to visually alert the user, and use a break statement to stop the save process.
What is an export value in a PDF radio button?
The export value is the specific data string that the PDF form registers when a user selects a particular radio button. In JavaScript, extracting this value allows you to use the user's specific choice in calculations or dynamically generate a filename suffix.




