Use JavaScript to Find and Replace Text in Acrobat PDF Form Fields
Question details
The user wants to find and replace specific characters within the value of a PDF form text field using JavaScript, such as an Acrobat regular-format script or a keystroke script.

- Product
- Adobe Acrobat PDF
- Device & OS
- not provided
- Scenario
- Modifying the actual underlying data of a PDF form field by targeting and replacing multiple occurrences of specific text characters upon user input.
- Observed behavior
- The user needs to make permanent text replacements in the field value, but applying a standard format script incorrectly only alters the visible display without updating the underlying field value.
Before editing form field scripts, ensure you have a backup of your original PDF document and possess basic familiarity with JavaScript Regular Expressions (RegExp).
Use a Validation or Keystroke Script with RegExp Replace
Apply a custom JavaScript using the RegExp replace method during field commitment to permanently change the underlying field data rather than just its visual formatting.
Avoid using a Format script to replace text data. Format scripts are designed to change only the displayed text on the screen, leaving the actual underlying field value unchanged.
Instead, use a Validation or Keystroke script that executes when the 'event.willCommit' property evaluates to true. This ensures the data is permanently modified right as the input is submitted.
Open your PDF document in Adobe Acrobat, navigate to the Tools menu, and select 'Prepare Form' to access form editing capabilities.
Right-click the specific text field you want to modify and select 'Properties' from the context menu.
Navigate to the 'Validate' or 'Keystroke' tab. Choose 'Run custom validation script' and click the 'Edit' button to open the JavaScript editor.
Input your script using the JavaScript RegExp object. For example, use 'event.value = event.value.replace(/targetText/g, "newText");' to replace all occurrences. Click 'OK' to save and apply the changes.

Manage and Fill PDF Forms Easily with WPS Office
If you need an intuitive way to manage, fill, and edit PDF forms without dealing with complex JavaScript coding, WPS PDF provides a user-friendly and reliable solution.
- 1. Download and Install: Visit the official WPS website to download and install WPS Office on your device.
- 2. Open Your PDF: Launch WPS Office and open your interactive PDF form document.
- 3. Fill and Edit: Click on the form fields to directly type and manage your data with intuitive text tools.
- 4. Save the Document: Once completed, click 'Save' or export your filled PDF securely.

Frequently Asked Questions
Why isn't my format script saving the replaced text in Acrobat?
A format script is only designed to change the visual appearance of the text on the screen for the user. To permanently alter the actual underlying field value, you must use a validation or keystroke script that triggers upon data commitment.
How do I ensure every instance of a word is replaced in my PDF form field?
You need to use the global flag ('g') within your JavaScript Regular Expression, such as /searchString/g. If this flag is omitted, the replace method will stop after finding and changing the first match.
Can I use standard JavaScript syntax inside Acrobat PDF form fields?
Yes, Adobe Acrobat utilizes a specific implementation called the Acrobat JavaScript API. Standard string manipulation methods, including the RegExp object and replace methods, function similarly to standard web-based JavaScript.




