How to Populate a PDF Field from Drop-Down Values Using Acrobat JavaScript
Question details
The user wants to auto-populate a PDF form field based on the combined selections of two drop-down menus by looking up data in an attached tab-delimited file.

- Product
- Adobe Acrobat
- Device & OS
- not provided
- Scenario
- Creating a dynamic PDF form that retrieves course names based on a generated course ID from drop-down selections.
- Observed behavior
- Needs a custom JavaScript solution to concatenate drop-down values, parse an attached text data file, find the matching ID, and output the corresponding result into a designated text field.
Ensure you have Adobe Acrobat Pro installed, as Acrobat Reader does not support creating document-level JavaScript for forms. Also, prepare your tab-delimited data file and attach it to your PDF document.
Implement Document-Level JavaScript for Data Lookup
Create a hidden field to combine drop-down values and use a custom script to read the attached data file, find the matching ID, and populate the target field.
This process involves concatenating the values of your two drop-down menus into a single ID, then using a JavaScript function to access the attached text file. The script reads the file line by line, splits the tab-delimited data, and checks for a match.
Due to Acrobat's security restrictions, ensure the script is executed in a trusted context. For small datasets (e.g., 80 records), performance is usually fine, but test the form thoroughly before distribution.
Add a hidden text field to your PDF. In its properties, use a calculation script to concatenate the values of the two drop-down menus (e.g., event.value = this.getField('Dropdown1').value + '-' + this.getField('Dropdown2').value;).
Go to the Attachments panel in Acrobat (View > Show/Hide > Navigation Panes > Attachments) and attach your tab-delimited data file to the PDF.
Navigate to Tools > JavaScript > Document JavaScripts. Create a new function that uses this.getDataObjectContents('filename.txt') to read the attached file.
In your script, convert the data stream to a string using util.stringFromStream(). Split the string by newline characters to separate the rows, and then split each row by the tab character to isolate the columns.
Iterate through the rows to find a matching ID. Once found, use this.getField('TargetField').value = matchedValue; to populate the third PDF field with the retrieved data.

Easily Create and Fill PDF Forms with WPS Office
While advanced JavaScript parsing requires heavy coding, WPS Office provides an intuitive PDF editor to create, manage, and fill interactive forms without complex scripting. You can easily add text fields, drop-down menus, and checkboxes to your documents.
- 1. Open your PDF in WPS Office: Launch WPS Office and open your PDF document using the built-in PDF editor.
- 2. Access form tools: Navigate to the 'Form' tab on the top ribbon to view available interactive form controls.
- 3. Insert form fields: Click on 'Combo Box' for drop-down menus or 'Text Field' for text entry, and drag them onto your document page.
- 4. Edit properties: Double-click the inserted fields to customize their properties, add drop-down options, or set default values.

Frequently Asked Questions
Can I use Acrobat Reader to read the attached data file using JavaScript?
Generally, reading attached files using document-level JavaScript requires Acrobat Pro. Acrobat Reader has strict security limitations that may block the getDataObjectContents method unless the document is specially rights-enabled.
What is the best format for the attached data file?
A tab-delimited text file (.txt) or comma-separated values (.csv) file works best. They are plain text formats that can be easily parsed using Acrobat's JavaScript string manipulation methods like split().
Is there a limit to the number of records I can search this way?
While there is no hard limit, parsing large text files via JavaScript in a PDF can cause performance issues. For datasets under a few hundred records, the delay is usually unnoticeable. Always test performance before deployment.
How do I handle missing matches in the JavaScript code?
Add conditional logic in your script to check if the loop finishes without finding a matching ID. If no match is found, you should clear the target field or display a default 'Not Found' message to prevent incorrect data entry.




