PDF Editing Issues

How to Access Object Properties from a PDF Form Field using JavaScript

Rana GarciaRana Garcia Nov 5, 2026 869 views

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.

How to Access Object Properties from a PDF Form Field using JavaScript
Product
PDF
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.
Before you start

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.

Solution 1Recommended

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.

1
Retrieve the string value

Use the command this.getField("test").valueAsString to fetch the raw text data stored in the target PDF form field.

2
Evaluate the string into an object

Parse the string into a variable. You can use the eval() function by structuring your code like: eval("var fruit = " + this.getField("test").valueAsString);

3
Access the properties

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;

Security Warning: Using eval() can execute arbitrary code. Only use this method if the source data is completely trusted. If possible, format your field data as standard JSON and use JSON.parse() instead for a safer implementation.
Manage PDF Forms Easily

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. 1. Open your PDF file: Launch WPS Office and open your interactive PDF document containing form fields.
  2. 2. Access the Form tools: Navigate to the 'Form' tab on the top navigation ribbon to highlight and manage all interactive fields.
  3. 3. Edit field properties: Double-click or right-click on specific form fields and select 'Properties' to adjust data formats, appearances, or calculation actions.
Easily fill out and manage interactive PDF form fieldsHigh compatibility with Adobe Acrobat and standard PDF formatsLightweight application with an intuitive, clutter-free user interfaceCross-platform support allowing seamless editing on Windows, Mac, and mobile
microsoft office alternative - wps office

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);