How to Make PDF Fields Read-Only Using JavaScript by Suffix
Question details
The user needs to automatically set numerous PDF form fields to read-only if their names end with a specific suffix, without manually editing each field.

- Product
- PDF Editor
- Device & OS
- not provided
- Scenario
- Managing a large PDF document with approximately 1,500 text fields where specific duplicate fields need to be locked simultaneously.
- Observed behavior
- Locking thousands of fields individually is too time-consuming, necessitating an automated script-based approach to change field properties in bulk.
Ensure you have a PDF editor that supports document-level JavaScript execution, such as Adobe Acrobat Pro. Back up your PDF file before running any scripts to prevent accidental data loss in case of syntax errors.
Use JavaScript to Bulk Update PDF Fields
Run a custom JavaScript code snippet in your PDF editor's console to loop through all fields and apply the read-only property to those matching the suffix.
This method utilizes a regular expression to identify field names ending with the word 'Duplicate' and instantly changes their status to read-only across the entire document.
Open your PDF document in a script-compatible PDF editor like Adobe Acrobat. Navigate to 'Tools' > 'JavaScript' > 'Document JavaScripts', or press 'Ctrl + J' (Windows) / 'Cmd + J' (Mac) to open the interactive JavaScript console.
In the console window, type or paste the following code: for (var i = 0; i < this.numFields; i++) { var f = this.getField(this.getNthFieldName(i)); if (f && /Duplicate$/.test(f.name)) f.readonly = true; }
Highlight the entire block of code you just pasted, then press 'Ctrl + Enter' (Windows) or 'Cmd + Enter' (Mac) to run the script. The console will execute the loop instantly.
Close the JavaScript console and go to 'File' > 'Save' to ensure all the newly applied read-only properties are permanently saved to your PDF form.

Manage PDF Forms Effortlessly with WPS Office
While executing complex JavaScript requires advanced and often expensive tools, WPS Office provides a highly intuitive PDF editor to manage and fill PDF forms effortlessly. It is a lightweight, cost-effective solution for everyday PDF tasks.
- 1. Download WPS Office: Download and install WPS Office from the official website to access the built-in PDF tools.
- 2. Open your PDF form: Launch WPS PDF and open your interactive PDF document.
- 3. Access form tools: Navigate to the 'Form' tab on the top ribbon to interact with and manage form fields visually.
- 4. Edit properties: Right-click on any form field and select 'Properties' to manually adjust its read-only status or appearance without scripting.

Frequently Asked Questions
Can I use JavaScript in any PDF viewer to make fields read-only?
No, most basic PDF viewers (like web browsers or free reader apps) do not support executing document-level JavaScript. You need an advanced PDF editor like Adobe Acrobat Pro to run these console scripts.
How do I change the suffix from 'Duplicate' to something else?
In the JavaScript code provided, locate the regular expression `/Duplicate$/`. Replace the word 'Duplicate' with your desired suffix. For example, use `/Copy$/` to target fields ending in 'Copy'.
Is there a way to undo the JavaScript execution if I make a mistake?
You cannot undo a script execution via the standard 'Undo' button. If you make a mistake, you must either run a reverse script (setting f.readonly = false) or reload your previously saved backup of the unedited PDF file.
Can I make fields read-only based on a prefix instead of a suffix?
Yes, you can modify the regular expression to look for prefixes. To match a prefix, use the caret symbol '^'. For example, `/^Duplicate/` will target any form fields that start with 'Duplicate'.




