How to Automatically Lock All PDF Comments Using JavaScript
Question details
The user wants to use Acrobat JavaScript to automatically lock and apply read-only properties to all comments and annotations in a PDF, avoiding the need to edit them individually.

- Product
- Device & OS
- not provided
- Scenario
- Securing a reviewed PDF document by locking all annotations in bulk before distributing or finalizing the file.
- Observed behavior
- Currently seeking a script to apply bulk properties to annotations rather than manually clicking and changing the properties of each individual comment.
Before running any automated scripts, make sure to save a backup copy of your original PDF document, as bulk property changes to annotations can be tedious to manually revert.
Execute a Document-Level JavaScript to Lock Annotations
By running a short JavaScript code block in your PDF editor's console, you can iterate through all annotations in the document and set their lock and read-only attributes to true instantly.
This method requires a PDF editor that supports a JavaScript console, such as Adobe Acrobat. The script scans the document for all annotation objects and updates their properties in a single action.
Launch your PDF editor and open the document. Navigate to the JavaScript debugging console (in Acrobat, this is found under Tools > JavaScript > Document JavaScripts or Debugger).
Copy and paste the following script into the console: this.syncAnnotScan(); var annots = this.getAnnots(); if (annots != null) { for (var i = 0; i < annots.length; i++) { annots[i].setProps({lock: true, readOnly: true}); } }
Run the script from the console. This will scan all pages of your PDF and apply the locked and read-only states to every annotation found.
Close the console and save the PDF document. Right-click any comment or annotation in the document to verify that the properties are now locked and cannot be edited.
Secure and Manage PDF Comments Easily with WPS Office
If you are looking for an intuitive way to manage, review, and secure PDF documents without writing complex JavaScript code, WPS PDF offers a user-friendly interface. You can easily protect your documents and manage permissions with just a few clicks.
- 1. Open your PDF: Launch WPS Office and open your PDF document.
- 2. Navigate to the Protect Tab: Click on the 'Protect' tab located on the top ribbon.
- 3. Encrypt the Document: Select 'Encrypt' to set a permission password. This allows you to restrict editing, commenting, and formatting changes across the entire document securely.
- 4. Save Your Settings: Confirm your password and save the file to ensure all comments and contents remain protected.

Frequently Asked Questions
Can I unlock the PDF comments later using JavaScript?
Yes. You can run the exact same script but change the properties in the loop to {lock: false, readOnly: false} to unlock the comments, provided the document itself is not restricted by a master security password.
Will this script work in all PDF viewers?
The script must be executed in a PDF editor that supports JavaScript execution. However, once the comments are locked and the file is saved, the read-only properties will be respected by nearly all standard PDF viewers and browsers.
Why did the script fail to lock my comments?
The script might fail if the PDF is already password-protected against modifications, or if your PDF editor has JavaScript execution disabled in its security preferences. Ensure JavaScript is enabled and that you have full editing permissions for the document.




