Add PDF Form Field Value to Email Subject Using JavaScript
Question details
The user wants to dynamically populate an email subject line with a value selected from a PDF drop-down form field using JavaScript.

- Product
- Device & OS
- not provided
- Scenario
- Creating an interactive PDF form that sends an email upon clicking a button, where the subject line reflects the user's input.
- Observed behavior
- The desired behavior is that clicking the email submit button reads the value from a specific drop-down field (e.g., "LocationD") and correctly concatenates it into the subject parameter of the email draft.
Ensure your PDF editing software supports JavaScript for interactive forms and that you know the exact name of the form field you want to reference.
Use getField and mailDoc in PDF JavaScript
Retrieve the specific form field value as a string and insert it into the mailDoc function's subject parameter.
You can use built-in JavaScript functions in advanced PDF software to read user input dynamically. By fetching the field value and combining it with your standard subject text, you can create customized email subjects for each submission.
Right-click the submit button in your interactive PDF form and select 'Properties'. Navigate to the 'Actions' tab.
Under 'Select Action', choose 'Run a JavaScript' from the drop-down menu, then click the 'Add' button to open the JavaScript editor.
Use the getField method to read the value and the mailDoc method to trigger the email. For example: var location = this.getField("LocationD").valueAsString; var subjectLine = "Location: " + location; this.mailDoc({cTo: "target@email.com", cSubject: subjectLine, cMsg: "Thank you"});
Click 'OK' to save the script, close the properties dialog, and exit form editing mode. Click the button to test if your default email client opens a draft with the correct subject.
Edit and Manage PDF Forms Easily with WPS Office
WPS Office provides robust PDF editing tools, allowing you to create interactive forms, manage text fields, and handle document workflows efficiently without relying on complicated or expensive software.
- 1. Open your PDF: Launch WPS Office and open your interactive PDF document.
- 2. Access Form Tools: Navigate to the 'Form' tab on the top ribbon to view and edit form fields, drop-downs, and buttons.
- 3. Edit Properties: Double-click any form field to easily adjust its properties, names, and associated actions.

Frequently Asked Questions
Why is my getField("FieldName").value returning undefined?
This typically happens if the field name is misspelled or if the case does not exactly match. Check the exact field name in your PDF's form properties. Using .valueAsString instead of .value is also highly recommended to ensure the output is properly treated as text.
Can I include multiple form fields in the email subject?
Yes, you can declare multiple variables for different fields (e.g., var field1 = this.getField("Field1").valueAsString; var field2 = this.getField("Field2").valueAsString;) and concatenate them all into the subject line variable using the + operator.
Does the mailDoc function automatically send the email in the background?
No. For security and anti-spam reasons, the mailDoc function only opens the user's default desktop email client (like Outlook or Apple Mail) with a new draft populated with your parameters. The user must manually review and click 'Send'.
What if the user relies on a web-based email client?
PDF JavaScript's mailDoc relies on the default mail handler configured on the user's operating system. If they do not have a default desktop client configured, the submit button may fail to open an email draft.




