Populate an Acrobat Form Field With Another PDF’s Page Count
Question details
Automatically fill a blank Acrobat form field with the page count of a related PDF without overwriting existing values.

- Product
- Adobe Acrobat
- Device & OS
- not provided
- Scenario
- A SpecSheet PDF and its related source PDF are in the same folder and share a filename prefix.
- Observed behavior
- The Pages field should receive the source PDF’s page count only when it is blank; saved or manually edited values should remain unchanged.
Place the PDFs in the same folder and use matching names such as sample.pdf and sample_SpecSheet.pdf. Confirm the exact name of the Pages form field and test in desktop Acrobat with JavaScript enabled.
Use Acrobat document-level JavaScript to fill a blank page-count field
Use this approach when you can edit both PDFs and Acrobat permits the required cross-document access.
The source PDF must allow disclosure, and the SpecSheet script must be able to open it. Acrobat security restrictions may block the openDoc call; a trusted function configured by an administrator may be required.
The example expects a form field named nbPages and a SpecSheet filename ending in _SpecSheet.pdf. It leaves any nonblank field value unchanged.
Open sample.pdf in desktop Acrobat. Go to All tools > Use JavaScript > Document JavaScripts, add a script containing `this.disclosed = true;`, and save the PDF. In older versions, look under Tools > JavaScript > Document JavaScripts.
Open sample_SpecSheet.pdf and choose Prepare a form. Select the Pages field and check its properties; either name it `nbPages` or replace `nbPages` in the script with its actual name.
In the SpecSheet, open Document JavaScripts and add this script: `var field = this.getField("nbPages"); var suffix = "_SpecSheet.pdf"; var name = this.documentFileName; if (field && field.valueAsString === "" && name.slice(-suffix.length) === suffix) { var sourceName = name.slice(0, -suffix.length) + ".pdf"; var sourcePath = this.path.slice(0, -name.length) + sourceName; var otherDoc = null; try { otherDoc = app.openDoc(sourcePath); field.value = otherDoc.numPages; } catch (e) { console.println("Could not read the source PDF: " + e); } finally { if (otherDoc) otherDoc.closeDoc(); } }`
Save and reopen the SpecSheet with the source PDF closed. Check that a blank field is filled, then save the SpecSheet and reopen it to confirm the value remains. Enter a different value and reopen it again to verify that the script does not overwrite it.

Enter the source PDF’s page count manually
Use this fallback if Acrobat cannot open the source PDF through JavaScript.
Work with PDF and Office files in WPS Office
WPS Office is a free, lightweight alternative with a familiar interface for working with PDF and Office documents. It makes moving between common file formats straightforward, but it does not replace the Acrobat JavaScript workflow described above.

Frequently Asked Questions
Will the script overwrite a manually entered page count?
No. The script checks whether the nbPages field is blank before opening the source PDF or writing a value. Save the SpecSheet after entering or updating the field.
What should I do if my field is not named nbPages?
In Acrobat, open Prepare a form and check the field’s properties. Replace `nbPages` in the SpecSheet script with the exact field name, including capitalization.
Why does the source PDF need a disclosure script?
The document-level statement `this.disclosed = true;` permits other documents to access its exposed properties, subject to Acrobat’s security restrictions. Add it through Document JavaScripts in the source PDF and save the file.
Why does the page count remain blank when I reopen the SpecSheet?
Check that the source PDF is in the same folder, the names follow the expected pattern, and Acrobat JavaScript is enabled. Inspect the JavaScript console for errors; Acrobat may also block app.openDoc without an appropriately configured trusted function.




