PDF Editing Issues

Hide Previously Spawned Acrobat PDF Templates with JavaScript

Steve KSteve K Nov 5, 2026 868 views

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.

Hide Previously Spawned Acrobat PDF Templates with JavaScript
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.
Before you start

Ensure you have basic knowledge of Acrobat JavaScript and that your PDF document has the necessary hidden templates and form fields properly configured.

Solution 1Recommended

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.

1
Define a tracking variable

Create a document-level JavaScript variable (e.g., `var spawnedPageNums = [];`) to store the page indexes of any newly spawned template pages.

2
Add deletion logic to the radio button

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.

3
Spawn the new template

After the deletion script runs, call `getTemplate("YourTemplateName").spawn();` to create the new pages based on the user's selection.

4
Update the tracking array

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.

Track and Delete Spawned Pages via JavaScript
Scripting Best Practice: By explicitly managing page deletion, your dynamic PDF form will remain clean and only display the template relevant to the currently selected radio button.
Free Microsoft Office alternative

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. 1. Download and Install: Visit the official WPS Office website and download the free version for your operating system.
  2. 2. Open your PDF: Launch WPS Office and drag-and-drop your PDF file into the workspace to start viewing or editing.
  3. 3. Utilize PDF Tools: Navigate to the 'PDF' tab to access tools for annotating, form filling, merging, and converting your documents.
Seamlessly open, view, and edit PDF documents with an intuitive user interface.Highly compatible with Microsoft Office formats including DOCX, XLSX, and PPTX.Lightweight application that runs smoothly on Windows, Mac, Linux, and mobile devices.Built-in robust PDF conversion tools to easily switch between document formats.
microsoft office alternative - wps office

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.