How to Make Fillable PDFs Read-Only and Restore with JavaScript
Question details
The user needs to learn how to dynamically toggle the editable state of a fillable PDF form using JavaScript, specifically making it read-only for a submission event and restoring it afterwards.

- Product
- PDF Editor
- Device & OS
- not provided
- Scenario
- Managing fillable PDF form states before and after form submission.
- Observed behavior
- The form needs to be locked (read-only) during the data submission process and unlocked (fillable) dynamically using a JavaScript function.
Ensure you are using a PDF editor that supports JavaScript execution and that you are familiar with adding document-level or button-level action scripts.
Toggle Form Fields to Read-Only or Editable using JavaScript
Use a JavaScript loop to iterate through all form fields in the document and change their boolean readonly status.
In PDF JavaScript, the 'readonly' property is a boolean, meaning it has only two states: true or false. By utilizing a simple 'for' loop, you can quickly change the state of every field in your document at once.
Open your PDF form in your PDF editor. Right-click the button or event (like a 'Submit' button) where you want to add the script, select 'Properties', and navigate to the 'Actions' tab. Choose 'Run a JavaScript' as the action.
To lock the form fields prior to submission, add the following script: for (var i = 0; i < this.numFields; i++) { this.getField(this.getNthFieldName(i)).readonly = true; }
To restore the form to an editable state after submission, use the same loop but change the boolean value to false: for (var i = 0; i < this.numFields; i++) { this.getField(this.getNthFieldName(i)).readonly = false; }

Easily Manage and Secure PDF Forms with WPS Office
WPS PDF provides comprehensive tools for creating, editing, and managing fillable forms. You can easily adjust field properties, set up interactive elements, and secure your documents in a user-friendly interface.
- 1. Open your PDF: Launch WPS Office and open your fillable PDF document.
- 2. Access form tools: Navigate to the 'Form' tab on the top ribbon to view all form creation and editing tools.
- 3. Modify properties: Double-click any form field to open its properties, where you can manually set it to 'Read-Only' or adjust other interactive settings.

Frequently Asked Questions
What does the readonly property do in a PDF form?
The readonly property is a boolean (true/false) setting that determines whether a user can interact with and change the content of a specific form field. When set to true, the field displays its data but cannot be altered.
Can I lock specific PDF fields instead of the whole document?
Yes. Instead of using a loop that iterates through 'this.numFields', you can target specific fields using 'this.getField("FieldName").readonly = true;' to lock only the fields you choose.
Why use JavaScript instead of standard PDF security passwords?
JavaScript allows for dynamic state changes during specific actions. For example, you can lock fields momentarily during a submission process and unlock them automatically afterward, which static security passwords cannot achieve.




