How to Lock and Email a PDF Form Using JavaScript
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.

- 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 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.
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.
Open your PDF form in edit mode, insert a 'Button' field, and right-click it to select 'Properties'.
Navigate to the 'Actions' tab. Choose 'Mouse Up' as the trigger, select 'Run a JavaScript' from the action dropdown, and click 'Add'.
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; } }
Below the loop, add the mailing function: this.mailDoc({bUI: true, cTo: "recipient@example.com", cSubject: "Completed Form Submission"}); then click 'OK' to save.

Flatten Pages and Email (Acrobat Only)
If you are certain that users are operating Adobe Acrobat, you can use the flattenPages method to permanently merge the fields into the document's background.
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. Open and Fill Your Form: Launch WPS Office and open your interactive PDF form to complete the required fields.
- 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. 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.

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.




