Hide Previously Spawned Acrobat PDF Templates with JavaScript
Question details
The user needs to remove or hide previously spawned PDF template pages using JavaScript when a new template is selected via radio buttons.

- Product
- Adobe Acrobat
- Device & OS
- not provided
- Scenario
- Managing dynamic PDF forms where different radio buttons trigger the spawning of different PDF templates.
- Observed behavior
- Newly selected templates spawn correctly, but the previously spawned pages remain visible instead of being removed or hidden automatically.
Ensure you have basic knowledge of Acrobat JavaScript and that your PDF document has the necessary hidden templates and form fields properly configured.
Track and Delete Spawned Pages via JavaScript
Use a document-level variable to track the page numbers of spawned templates, then delete them before spawning a new template.
Acrobat templates do not automatically replace one another. To prevent multiple templates from stacking up in your document, your radio button script must explicitly delete the old pages before executing the `spawn()` method for the newly selected template.
Create a document-level JavaScript variable (e.g., `var spawnedPageNums = [];`) to store the page indexes of any newly spawned template pages.
In your radio button's Action script, add a loop that reads the `spawnedPageNums` array and uses `this.deletePages({nStart: pageNum, nEnd: pageNum});` to explicitly remove them from the document.
After the deletion script runs, call `getTemplate("YourTemplateName").spawn();` to create the new pages based on the user's selection.
Record the newly created page numbers returned by the `spawn()` method back into your `spawnedPageNums` variable so they can be deleted on the next selection.

Try WPS Office for Comprehensive Document & PDF Management
While advanced Acrobat JavaScript is specific to Adobe, WPS Office provides a lightweight, highly compatible alternative for everyday PDF editing, document conversion, and office tasks. It seamlessly integrates word processing, spreadsheets, presentations, and PDF tools in one suite.
- 1. Download and Install: Visit the official WPS Office website and download the free version for your operating system.
- 2. Open your PDF: Launch WPS Office and drag-and-drop your PDF file into the workspace to start viewing or editing.
- 3. Utilize PDF Tools: Navigate to the 'PDF' tab to access tools for annotating, form filling, merging, and converting your documents.

Frequently Asked Questions
Why do my spawned PDF templates overlap instead of replacing each other?
Adobe Acrobat templates do not possess an inherent 'replace' function. When you spawn a template, it appends or inserts a new page. You must use JavaScript to explicitly delete the old pages first.
Can I hide a spawned page instead of deleting it?
In Acrobat, you cannot technically 'hide' a standard PDF page once it is spawned. You must delete the spawned pages and re-spawn them from the hidden master template when they are needed again.
Where do I add the JavaScript for the radio buttons?
Right-click the radio button while in form editing mode, select Properties, navigate to the Actions tab, select 'Run a JavaScript' as the action, and paste your page deletion and spawning code.
How do I know which page number to delete in Acrobat JavaScript?
The `spawn()` method returns the page object or number it created. You should capture this return value in a document-level variable so your script knows exactly which page index to pass to the `deletePages()` function.




