How to Insert PDF Pages Based on Form Values Using Acrobat JavaScript
Question details
The user wants to automatically insert a specific PDF page when a form field contains a selected value or when a checkbox is selected.

- Product
- Adobe Acrobat PDF
- Device & OS
- not provided
- Scenario
- Creating dynamic PDF forms where additional pages need to be added conditionally based on what the user selects in the form fields.
- Observed behavior
- Requires a script to trigger page insertion dynamically, utilizing either the insertPages method for external files or the spawn method for hidden page templates.
Determine whether your end users will be using Adobe Acrobat Pro or Adobe Reader, as the insertPages JavaScript method does not work natively in the free Reader without specific setup.
Use Page Spawning with Hidden Templates (Works in Adobe Reader)
This is the best method if your end users use Adobe Reader. It involves embedding the predetermined pages as hidden templates inside the document and using JavaScript to spawn them.
Since external page insertion is restricted in Adobe Reader, you must pre-load the dynamic pages into your PDF file. By hiding them as templates, they remain invisible until the user clicks a button that triggers the JavaScript spawn method based on their form selection.
Add all the pages you might want to insert into your document. Open the 'Organize Pages' tab, select the first page to insert, open the 'More' menu, and select 'Page Templates'. Name the page template (e.g., 'PageA'), select 'Add', and then uncheck the visibility box to hide it. Repeat this for each conditional page.
Importing data won't usually trigger the action automatically. Add a 'Button' form field to your PDF (e.g., named 'Update Document') to initiate the action.
Open the button's properties, go to the Actions tab, and set the action to 'Run a JavaScript'. Enter code to retrieve the templates and spawn them based on your field's value. For example: `var a = this.getTemplate('PageA'); var resp = this.getField('Input').valueAsString; if (resp == 'x') { a.spawn({nPage:this.numPages-1, bRename:false, bOverlay:false}); }`

Use the insertPages Method (Requires Adobe Acrobat)
This method pulls external PDF files into your current document dynamically but requires the user to have Adobe Acrobat and access to the referenced local file paths.
Manage and Organize PDF Pages Effortlessly with WPS Office
While advanced conditional JavaScript requires extensive setup, WPS Office provides an incredibly intuitive interface for manually inserting, extracting, and organizing PDF pages. Perfect for users who need to compile documents quickly without writing complex code.
- 1. Open Your PDF: Launch WPS Office and open your main PDF form or document.
- 2. Navigate to the Page Tools: Click on the 'Page' tab located in the top ribbon to access the page management tools.
- 3. Insert New Pages: Click on 'Insert' to add blank pages, or select 'Insert from PDF' to instantly append pages from an external document without needing any scripts.
- 4. Organize Your Document: Drag and drop the thumbnails to easily reorder your pages exactly how you need them.

Frequently Asked Questions
Will the insertPages JavaScript function work in Adobe Reader?
No, the insertPages method is restricted in the free Adobe Reader due to security limitations. It only works in Adobe Acrobat. If your users have Reader, use the hidden page template spawning method instead.
Can I trigger the page insertion automatically without a button?
While it is technically possible to tie scripts to field calculation or formatting events, importing data or typing typically won't trigger the action reliably. It is highly recommended to use a button (like a 'Submit' or 'Add Page' button) with a 'Mouse Up' event to initiate the script.
How do I hide a page template in my PDF?
First, add your page to the document. Open the 'Organize Pages' tab, select the page, click the 'More' dropdown, and choose 'Page Templates'. Give the template a name, click 'Add', and then uncheck the visibility box next to the template name to hide it from the normal view.




