PDF Editing Issues

How to Make PDF Fields Read-Only Using JavaScript by Suffix

Jason MillerJason Miller Nov 3, 2026 869 views

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.

How to Make PDF Fields Read-Only Using JavaScript by Suffix
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.
Before you start

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.

Solution 1Recommended

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.

1
Open the JavaScript Debugger

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.

2
Input the script

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; }

3
Execute the code

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.

4
Save your document

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.

Use JavaScript to Bulk Update PDF Fields
Script Executed: All fields with the 'Duplicate' suffix are now locked and cannot be edited by end-users filling out the form.
Easy PDF Form Management

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. 1. Download WPS Office: Download and install WPS Office from the official website to access the built-in PDF tools.
  2. 2. Open your PDF form: Launch WPS PDF and open your interactive PDF document.
  3. 3. Access form tools: Navigate to the 'Form' tab on the top ribbon to interact with and manage form fields visually.
  4. 4. Edit properties: Right-click on any form field and select 'Properties' to manually adjust its read-only status or appearance without scripting.
Easily view, fill, and manage PDF form fields without needing to write code.Fully compatible with standard PDF formats and Adobe Acrobat forms.Lightweight software with an intuitive, user-friendly interface.Includes comprehensive PDF conversion, merging, and annotation tools.
microsoft office alternative - wps office

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'.