How to Mirror Fields from an Attached PDF Using JavaScript
Question details
The user needs to automatically copy specific form field values, such as a name and title, from a main PDF form into an attached PDF file using JavaScript.

- Product
- PDF Editor
- Device & OS
- not provided
- Scenario
- Automating data entry by synchronizing form fields across a main PDF and its attached secondary PDF.
- Observed behavior
- The user is attempting to set up a script to pass values because the attached document cannot natively pull information from its parent document.
Verify that your PDF viewer supports JavaScript execution and ensure you have noted the exact form field names used in both the main and attached PDF files.
Use a JavaScript Push Action from the Main PDF
Since attached PDFs cannot directly read from parent documents, you must run a script in the main PDF to push data into the attachment using the openDataObject method.
By design, PDF attachments exist in a sandbox and cannot reach out to their parent documents to retrieve data. To synchronize fields, the parent document must explicitly open the attachment as a data object and write the values into its form fields.
This approach requires triggering a script from the main document, usually via a form button or an event action like 'Document Did Save'.
In your main PDF, go to the form editing tools and add a Push Button. Label it 'Sync to Attachment' or a similar clear action.
Double-click the newly created button to open its Properties dialog box, then navigate to the 'Actions' tab.
From the 'Select Action' dropdown, choose 'Run a JavaScript', and then click the 'Add' button to open the JavaScript Editor.
Input a script that uses Doc.openDataObject() to target the attached file. Retrieve the values using this.getField('YourFieldName').value and push them into the corresponding fields of the opened data object.
Save the PDF, exit form editing mode, fill in the main fields, and click your trigger button to verify the data successfully mirrors into the attachment.

Create and Manage Interactive PDF Forms in WPS Office
While advanced JavaScript operations can be complex and dependent on viewer security settings, WPS Office provides an intuitive, user-friendly interface to create, edit, and fill out interactive PDF forms without extensive coding.
- 1. Open Your PDF in WPS: Launch WPS Office and select the PDF file you wish to edit or turn into a form.
- 2. Navigate to the Form Tab: Click on the 'Form' tab located in the top navigation ribbon to reveal the interactive form tools.
- 3. Insert Form Elements: Select elements like 'Text Field', 'Check Box', or 'Dropdown' and click on the document page to place them.
- 4. Customize Field Properties: Double-click any placed field to adjust its name, formatting, and default values easily without complex scripts.

Frequently Asked Questions
Can an attached PDF automatically pull data from the main document?
No. For security and architectural reasons, an attached PDF cannot directly access or read data from its parent document. The data transfer must be initiated by the main document pushing values into the attachment.
Which JavaScript method is used to access an attached PDF?
The 'Doc.openDataObject()' method is used within the main PDF's JavaScript to access an attached file as a hidden data object, allowing you to read or write its form fields.
Why is my PDF JavaScript failing to execute?
Scripts often fail to execute due to strict security settings in the user's PDF viewer that block JavaScript or cross-document data access. Ensure the file is placed in a trusted directory or that JavaScript is enabled in the viewer's preferences.




