PDF Editing Issues

How to Automatically Lock All PDF Comments Using JavaScript

Muhammad TalhaMuhammad Talha Nov 5, 2026 868 views

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.

How to Automatically Lock All PDF Comments Using JavaScript
Product
PDF
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 you start

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.

Solution 1Recommended

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.

1
Open the JavaScript Console

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

2
Input the JavaScript Code

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

3
Execute the Script

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.

4
Save and Verify

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.

Annotations Successfully Locked: Once saved, the annotations will retain their read-only status across all standard PDF viewers.
Advanced PDF Management

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. 1. Open your PDF: Launch WPS Office and open your PDF document.
  2. 2. Navigate to the Protect Tab: Click on the 'Protect' tab located on the top ribbon.
  3. 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. 4. Save Your Settings: Confirm your password and save the file to ensure all comments and contents remain protected.
Easily view, manage, and navigate all PDF comments from a centralized panelApply document-level encryption and restriction to prevent unauthorized editingFully compatible with Adobe Acrobat PDF formats and standard annotationsLightweight, fast, and feature-rich interface for seamless document workflows
microsoft office alternative - wps office

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.