How to Access a Checkbox in Another PDF Using JavaScript
Question details
The user needs to use JavaScript to detect whether a checkbox in one open PDF is selected in order to control the visibility of fields in a second open PDF.

- Product
- PDF Editors
- Device & OS
- not provided
- Scenario
- Managing dynamic form fields across multiple open PDF documents simultaneously using JavaScript.
- Observed behavior
- A cross-document script is required to link the checkbox state of one document to the form fields of another.
Ensure both PDF documents are saved locally and that your PDF viewer's security settings are configured to allow cross-document JavaScript execution.
Use app.openDoc() to Reference the Second Document
You can achieve cross-document interactions by opening a background reference to the target document and setting its disclosed property to access its fields.
To allow one PDF to read or modify fields in another, you must assign a unique object name to the second PDF using the app.openDoc() method. Additionally, the target document must have its 'disclosed' property set so its fields are accessible to scripts outside of itself.
In your primary PDF script editor, use the code: var doc = app.openDoc('/path/to/second-document.pdf'); to create a reference to the second file.
Ensure the target document is configured to be accessed externally. You may need to set doc.disclosed = true; depending on your script structure and reader version.
Use the getField method on the new document object to retrieve the checkbox state: var checkboxState = doc.getField('YourCheckboxName').value;
Write an if-else statement evaluating checkboxState. If it equals 'Yes' (or your defined export value), set the display property of the fields in your primary PDF to visible or hidden accordingly.

Edit and Manage PDF Forms Easily with WPS PDF
If you need to create, edit, or configure interactive PDF forms without complex scripting, WPS Office provides an intuitive suite of PDF form tools that handle checkboxes, text fields, and buttons effortlessly.
- 1. Open your PDF: Launch WPS Office and open the PDF file you wish to edit.
- 2. Access form tools: Navigate to the 'Form' tab located on the top ribbon menu.
- 3. Add form fields: Click on the 'Check Box' or 'Text Field' icons to place interactive elements directly onto your document.
- 4. Modify properties: Right-click the inserted field and select 'Properties' to adjust names, export values, and display settings.

Frequently Asked Questions
Why does the app.openDoc() method return null or throw an error?
This usually happens if the file path provided is incorrect, or if the PDF reader's security settings prevent cross-document script execution. Ensure the path is exact and both files are stored in a trusted location.
What is the 'disclosed' property in PDF JavaScript?
The 'disclosed' property allows a PDF document's internal objects and fields to be accessed by JavaScript running in a different open PDF document. If it is not set, external scripts cannot read or modify its fields.
How do I check if a checkbox is checked using getField?
You can check the .value property of the field object. Typically, an unchecked box returns 'Off', while a checked box returns the export value you defined in its properties, which is often 'Yes' or 'On'.
Can I hide a field dynamically without using JavaScript?
Basic field visibility can sometimes be toggled using built-in form actions like 'Show/Hide a field' triggered by a mouse click, but linking conditions dynamically across two completely different files strictly requires JavaScript.




