How to Access Object Properties from a PDF Form Field using JavaScript
Question details
The user needs to extract and manipulate object properties (such as name, color, and weight) stored within a PDF form field via JavaScript, but the data is being returned as plain text.

- Product
- Device & OS
- not provided
- Scenario
- Writing JavaScript within a PDF document to read data stored in one form field and apply its internal object properties to other fields.
- Observed behavior
- The valueAsString method returns a literal text string instead of a callable JavaScript object, causing direct property access (e.g., object.name) to fail.
Ensure you have access to the PDF document's JavaScript console or form field calculation properties, and verify that the text stored in the target field is a correctly formatted object literal.
Convert the String Value to a JavaScript Object
Since valueAsString retrieves plain text, you must programmatically parse the string into a valid JavaScript object before accessing its individual properties.
The PDF JavaScript API retrieves field values precisely as they are entered. If the field contains a JavaScript object literal or JSON data, the API still processes it as a standard text string. Attempting to query properties like '.name' directly on a string data type will not work.
Use the command this.getField("test").valueAsString to fetch the raw text data stored in the target PDF form field.
Parse the string into a variable. You can use the eval() function by structuring your code like: eval("var fruit = " + this.getField("test").valueAsString);
Once evaluated, use standard dot notation to retrieve the specific property and assign it to another field. For example: this.getField("Name").value = fruit.name;
Use Individual Fields for Each Property
Modify the document structure to store individual properties in separate, dedicated PDF form fields rather than as a single text object.
Edit and Manage PDF Forms Seamlessly with WPS Office
WPS PDF provides powerful, intuitive tools for creating, filling, and editing interactive PDF forms. Whether you need to input data, adjust field properties, or format text, WPS Office streamlines your document workflow.
- 1. Open your PDF file: Launch WPS Office and open your interactive PDF document containing form fields.
- 2. Access the Form tools: Navigate to the 'Form' tab on the top navigation ribbon to highlight and manage all interactive fields.
- 3. Edit field properties: Double-click or right-click on specific form fields and select 'Properties' to adjust data formats, appearances, or calculation actions.

Frequently Asked Questions
Why does getField return a string instead of a JavaScript object?
The PDF JavaScript API retrieves user inputs from fields as primitive data types (typically strings or numbers). Even if you type an object literal or JSON syntax into a field, the PDF engine processes it purely as standard text until it is explicitly parsed via script.
Is it safe to use eval() within a PDF form?
Using eval() can execute arbitrary JavaScript code, which poses a security risk if the field's input is not strictly controlled. It is highly recommended to store data as valid JSON and use JSON.parse() when possible to parse data securely.
How can I check the exact data type returned by a PDF field?
You can utilize the PDF JavaScript console. Use the 'typeof' operator combined with 'console.println()' to print out the data type of your getField return. For example: console.println(typeof this.getField("myField").valueAsString);




