Other PDF Issues

How to Add a PDF File Name and User Name to an Email Subject Using JavaScript

John WilsonJohn Wilson Oct 28, 2026 868 views

Question details

The user wants to dynamically formulate an email subject that combines the current PDF file name and a user name obtained from a specific form field using JavaScript.

How to Add a PDF File Name and User Name to an Email Subject
Product
PDF Form Editor
Device & OS
not provided
Scenario
Creating an interactive 'Submit by Email' button or script within a PDF form to streamline the document collection process.
Observed behavior
By default, email subjects generated from PDFs are static; the goal is to execute a script that dynamically pulls file properties and active form field data to create a custom subject line.
Before you start

Ensure your PDF document has interactive form fields enabled and verify the exact internal name of the text field (e.g., 'Text4') that captures the user name, as JavaScript is case-sensitive.

Solution 1Recommended

Use the mailDoc JavaScript Method

Implement a custom JavaScript action to dynamically fetch both the file name and the form field value, passing them into the email subject parameter.

The mailDoc method allows you to trigger the system's default email client and pre-fill the To, Subject, and Body fields. By concatenating document properties and form values, you can create a highly specific subject line that updates automatically based on the user's input.

1
Open PDF in Form Editing Mode

Open your PDF in an advanced editor that supports form field editing and JavaScript actions (such as Adobe Acrobat or WPS PDF Pro).

2
Access Button Properties

Select the form button element that will trigger the email action, right-click it, and choose 'Properties'.

3
Add a JavaScript Action

Navigate to the 'Actions' tab, select 'Run a JavaScript' from the 'Select Action' dropdown menu, and click the 'Add' button.

4
Insert the Script

In the JavaScript Editor window, enter the following code: this.mailDoc({cTo: 'your_email_address@server.com', cSubject: this.documentFileName + ' from ' + this.getField('Text4').valueAsString, cMsg: 'Enter the email message body here'});

5
Customize and Save

Replace 'Text4' with the actual field name of your user name text box, click 'OK', and save the PDF file to apply the changes.

Use the mailDoc JavaScript Method
Verify Field Names: Make sure the field name placed inside getField() exactly matches the actual form field name in your document. Any discrepancies will result in a script error or a null value.
Advanced PDF Form Management

Manage PDF Forms and Share Documents Easily with WPS Office

WPS Office provides robust PDF capabilities, allowing you to fill forms, read JavaScript-enabled documents, and seamlessly share files without complicated setups.

  1. 1. Install WPS Office: Download and install the free version of WPS Office from the official website.
  2. 2. Open the PDF File: Launch WPS PDF and open your interactive PDF form.
  3. 3. Fill Out the Form: Type the required user information directly into the designated text fields.
  4. 4. Share via Email: Click the 'Share' or 'Email' icon in the top toolbar to quickly attach the document to a new email message via your default mail client.
Easily interact with and fill complex PDF form fieldsFully compatible with standard Adobe Acrobat PDF formats and scriptsBuilt-in seamless document sharing and email integrationLightweight, fast, and features a user-friendly interface
microsoft office alternative - wps office

Frequently Asked Questions

Can I add CC and BCC addresses using the mailDoc script?

Yes, you can include 'cCc' and 'cBcc' parameters within the mailDoc object. For example, add cCc: 'cc_address@server.com' separated by a comma within the curly braces.

Why does the getField script return null or an error?

This usually happens if the field name specified in the script does not exactly match the field's actual name in the PDF form. Double-check for typos, spaces, and ensure correct capitalization.

Will this JavaScript action work on all PDF viewers?

No, JavaScript execution depends entirely on the PDF viewer. While it works reliably in robust desktop editors like Adobe Acrobat and WPS PDF Pro, some web browsers and basic mobile PDF readers may restrict or ignore mailDoc actions for security reasons.

How do I add a line break in the email message body?

You can use the newline escape sequence '\n' within the 'cMsg' parameter string to create line breaks in the generated email body.