How to Create a Dynamic Submit Form Message Body using Acrobat JavaScript
Question details
Form developers need a method to build a dynamic email message body by collecting data only from populated PDF form fields, omitting any empty lines.

- Product
- Adobe Acrobat / PDF Forms
- Device & OS
- not provided
- Scenario
- Setting up a PDF form email submission button that automatically formats a custom email body based on user input.
- Observed behavior
- The form currently sends raw or blank data, and the user wants to ensure only fields containing actual data are compiled and appended to the submission email body.
Ensure you have access to a PDF editor with form editing and JavaScript execution capabilities, and verify that your target form fields use a strict sequential naming convention (e.g., 'DIU Item 1', 'DIU Item 2').
Use a JavaScript Loop to Construct the Message Body
Add a custom JavaScript action to your form's submit button that iterates through the sequentially named fields, skips empty ones, and builds a clean message string.
By utilizing a 'for' loop in Acrobat JavaScript, you can dynamically retrieve field values. The `valueAsString` property is crucial here to safely check if a field has been filled by the user. If the primary field contains data, the script retrieves related fields using the current loop index and appends them to a variable.
Once the string is completely constructed, it is passed into the `this.submitForm` function as the `cMsg` parameter.
Enter Form Editing mode in your PDF application. Right-click your intended Submit button and select 'Properties'.
Navigate to the 'Actions' tab. Select 'Mouse Up' as the trigger, choose 'Run a JavaScript' as the action, and click 'Add'.
In the JavaScript Editor, initialize an empty string variable (e.g., `var msgBody = "";`). Create a `for` loop that iterates through the total number of your sequential fields (e.g., `for (var i=1; i<=20; i++)`).
Inside the loop, use an `if` statement to check if the field is empty: `if (this.getField("DIU Item " + i).valueAsString != "")`.
If the condition is met, append the text and field values to your variable: `msgBody += "Item: " + this.getField("DIU Item " + i).valueAsString + " Qty: " + this.getField("DIU Item " + i + " Qty").valueAsString + "\n";`
After the loop finishes, pass the variable to the submit function: `this.submitForm({cURL: "mailto:your@email.com", cMsg: msgBody, cSubmitAs: "PDF"});`

Manage and Fill PDF Forms Seamlessly with WPS Office
While complex JavaScript logic is excellent for advanced form submissions, having a highly compatible and intuitive PDF editor simplifies everyday form handling. WPS PDF provides a comprehensive toolset to view, edit, fill, and manage standard interactive PDF forms effortlessly.
- 1. Download and Install: Download WPS Office for free from the official website and install it on your device.
- 2. Open Your PDF Form: Launch WPS PDF and open the interactive PDF form you need to manage or test.
- 3. Interact with Fields: Click into the highlighted text fields, checkboxes, and dropdowns to fill the form intuitively.
- 4. Save and Submit: Once completed, use the integrated save and share features to email your form securely.

Frequently Asked Questions
How do I hide empty fields when emailing a PDF form?
You can hide empty fields by using a JavaScript loop triggered upon submission. By verifying if a field's `valueAsString` is not empty (`!= ""`) before appending it to the email body string, blank inputs are effectively skipped.
What function is used to submit a PDF form via email in JavaScript?
The `this.submitForm()` method is used. You can pass an object with parameters such as `cURL` for the 'mailto:' address, and `cMsg` to define your dynamically generated custom message body.
Can I retrieve multiple related field values in a single loop iteration?
Yes. If your related fields share a common sequential prefix (e.g., 'Item 1', 'Item 1 Qty Used'), you can retrieve and concatenate all of them within the same loop iteration by appending the specific suffix to the current loop index number.
Why is my PDF JavaScript submit button not doing anything?
This usually happens if JavaScript execution is disabled in your PDF viewer's security preferences, or if there is a syntax error in your code. Check the application's JavaScript console for specific error messages or misspelled field names.




