PDF Security & Access Issues

How to Lock and Email a PDF Form Using JavaScript

Anum MushtaqAnum Mushtaq Nov 2, 2026 869 views

Question details

The user wants to execute a JavaScript code to either make a completed PDF form read-only or flatten it, and then automatically attach the finalized PDF to an email with a predefined subject and recipient.

How to Lock and Email a PDF Form Using JavaScript
Product
Adobe Acrobat
Device & OS
not provided
Scenario
Automating the form submission process so that users cannot modify the answers after clicking a submit button, which triggers the email attachment.
Observed behavior
The user needs the exact JavaScript methods to achieve form locking/flattening and emailing, while accounting for the differences in permissions between Adobe Acrobat and Acrobat Reader.
Before you start

Before writing your script, determine whether your end-users will be completing the form in Adobe Acrobat or the free Adobe Reader, as true page flattening is restricted in Reader without special usage rights.

Solution 1Recommended

Make Fields Read-Only and Email the Document

Use this script to lock the form by setting individual fields to read-only before triggering the email prompt. This method is highly recommended as it works smoothly in both Adobe Acrobat and Adobe Reader.

Instead of flattening the entire document, this approach uses a loop to find every interactive field in the PDF and changes its property to read-only. Once locked, the mailDoc function is called to generate an email draft with the secure form attached.

1
Add a Submission Button

Open your PDF form in edit mode, insert a 'Button' field, and right-click it to select 'Properties'.

2
Set the JavaScript Action

Navigate to the 'Actions' tab. Choose 'Mouse Up' as the trigger, select 'Run a JavaScript' from the action dropdown, and click 'Add'.

3
Insert the Read-Only Loop Script

In the JavaScript Editor, enter a loop to lock the fields: for (var i = 0; i < this.numFields; i++) { var f = this.getField(this.getNthFieldName(i)); if (f != null) { f.readonly = true; } }

4
Add the Email Script

Below the loop, add the mailing function: this.mailDoc({bUI: true, cTo: "recipient@example.com", cSubject: "Completed Form Submission"}); then click 'OK' to save.

Make Fields Read-Only and Email the Document
Selective Locking: If you only want to lock specific fields, you can add an 'if' condition inside your loop to check the field name before applying the readonly property.
No-Code PDF Management

Lock and Share PDF Forms Easily with WPS Office

If you want to avoid writing complex JavaScript to secure your documents, WPS Office provides built-in tools to easily fill, password-protect, flatten, and email PDF forms in just a few clicks.

  1. 1. Open and Fill Your Form: Launch WPS Office and open your interactive PDF form to complete the required fields.
  2. 2. Restrict Editing: Go to the 'Protect' tab and select 'Encrypt'. Set a permissions password and disable editing to instantly lock all form data.
  3. 3. Email the Secured PDF: Click the 'Share' or 'Email' icon on the top toolbar to automatically attach the locked PDF to a new email draft.
Easily restrict form editing with robust password protection.Flatten PDF forms quickly using the native 'Print to PDF' feature.Share secured PDFs directly via your default email client with one click.Highly compatible with all standard Adobe PDF formats and form fields.
microsoft office alternative - wps office

Frequently Asked Questions

Why is the flattenPages script not working for some users?

The flattenPages() JavaScript method requires Adobe Acrobat. If your users open the PDF in the free Adobe Reader, the script will not execute because Reader lacks the necessary document modification permissions.

Can I lock only specific fields instead of the whole form?

Yes. When writing your JavaScript loop to set the readonly property, you can include an 'if' statement to verify the field name. This allows you to skip certain fields, like the signature or date field, keeping them editable while locking the rest.

How do I specify multiple email recipients in the mailDoc script?

You can add multiple email addresses in the 'cTo' parameter by separating them with a semicolon (e.g., cTo: "user1@example.com; user2@example.com"). You can also use 'cCc' and 'cBcc' parameters for carbon copies.