PDF Editing Issues

Populate an Acrobat Form Field With Another PDF’s Page Count

Chanuka GeekiyanageChanuka Geekiyanage Nov 19, 2026 868 views

Question details

Automatically fill a blank Acrobat form field with the page count of a related PDF without overwriting existing values.

Populate an Acrobat Form Field With Another PDF’s Page Count
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.
Before you start

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.

Solution 1Recommended

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.

1
Enable disclosure in the source PDF

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.

2
Check the form field name

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.

3
Add the SpecSheet script

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(); } }`

4
Test and save the result

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.

Use Acrobat document-level JavaScript to fill a blank page-count field
If Acrobat blocks access: Check that JavaScript is enabled and the filenames match. Review errors in Acrobat’s JavaScript console with an administrator; security settings may require a trusted function or prevent automatic access. Browser PDF viewers and restricted viewers may not support this workflow.
Free Microsoft Office alternative

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.

Open related PDFs to check page counts for manual form entryCompatible with Microsoft Word, Excel, and PowerPoint formatsFamiliar interface for working across documentsFree and lightweight office suite
microsoft office alternative - wps office

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.