How to Combine PDF Form JavaScript to Hide Fields and Send Email
Question details
The user needs to create a unified JavaScript function for a PDF form that conditionally hides or shows an email field based on a checkbox, composes a custom email using form data, and sends the PDF while hiding specific interface buttons.

- Product
- PDF Form
- Device & OS
- not provided
- Scenario
- Configuring interactive PDF form buttons to process form data conditionally and automate the email submission process without UI glitches.
- Observed behavior
- Using separate button actions for visibility and email submission causes logic conflicts; the actions must be combined into a single logical JavaScript workflow to function correctly.
Before modifying your PDF form, ensure your PDF editor supports JavaScript execution for form fields and verify the exact names of your target form fields to prevent script errors.
Combine Conditional Logic and the mailDoc Function
Use a single unified JavaScript block on your submit button's 'Run a JavaScript' action to handle field visibility, dynamic data extraction, and email sending simultaneously.
Instead of assigning multiple separate actions to a button, combining them into one script prevents execution order issues. By checking if a specific box is checked first, you can dynamically display the email field or execute the mailDoc() method securely in one pass.
Open your PDF form in Edit Mode, right-click the submit button you want to configure, and select Properties from the context menu.
Navigate to the Actions tab. Select 'Run a JavaScript' from the 'Select Action' dropdown menu, and click the Add button to open the JavaScript Editor.
Enter your script by first defining the variable for your checkbox: `var visible = this.getField("HiddenCheck");`. Add an if-statement to check its status: `if (visible.isBoxChecked(0) == false) { this.getField("EmailAddress").display = display.visible; }`.
In the `else` block, set your email parameters by extracting field values. For example: `var cToAddr = this.getField("EmailAddress").value;` and `var cSubLine = "Agreement for " + this.getField("OtherPartyName").value;`.
Hide the email field using `this.getField("EmailAddress").display = display.hidden;`, then trigger the email window using `this.mailDoc({bUI: true, cTo: cToAddr, cFromAddr: "your@email.com", cSubject: cSubLine, cMsg: "Your message"});`.
Click OK to close the JavaScript Editor and close the Properties dialog. Preview the PDF and test the button to ensure it hides the required fields and opens your default email client.

Easily Edit PDF Forms and Manage Documents with WPS Office
WPS PDF provides robust tools for filling, editing, and managing interactive PDF forms. Whether you need to process form data, add digital signatures, or convert PDFs to other file formats, WPS Office offers a comprehensive and user-friendly experience.
- 1. Download WPS Office: Download and install WPS Office from the official website to access the comprehensive WPS PDF tool.
- 2. Open Your PDF File: Launch WPS PDF and open your interactive PDF form document.
- 3. Fill and Manage Forms: Navigate to the Form tab to interact with form fields, checkboxes, and buttons seamlessly.
- 4. Save and Share: Once your form is filled, save your document or use the built-in sharing tools to distribute it securely.

Frequently Asked Questions
Why isn't my mailDoc() JavaScript triggering the email client?
This usually happens if your default email client is not configured correctly on your operating system, or if security restrictions within your PDF viewer are blocking JavaScript execution. Ensure your default mail app is set up and that the PDF reader has permissions to execute scripts.
Can I hide the submit button immediately after a user clicks it?
Yes, you can hide the button by including `this.getField("ButtonName").display = display.hidden;` in your JavaScript. Place this line before the `mailDoc()` call so the button disappears from the document before the email is generated.
How do I reference another field's value in my PDF email subject line?
You can retrieve another field's value using the `getField` method in your JavaScript. For example, `var subject = "Agreement for " + this.getField("ClientName").value;` will seamlessly append the text entered in the 'ClientName' field to your email subject line.




