How to Spawn a PDF Template at a Specific Page Using Acrobat JavaScript
Question details
The user wants to insert a spawned PDF template at a specific page index rather than at the end of the document, and needs to resolve an error caused by incorrect movePage arguments.

- Product
- Adobe Acrobat
- Device & OS
- not provided
- Scenario
- Automating PDF document assembly or dynamic form generation using Acrobat JavaScript templates based on user interactions like checkbox selections.
- Observed behavior
- Using the this.movePage method with incorrect arguments results in a 'RaiseError: Expected an array object' error, and the spawned page fails to appear at the correct location.
Ensure you have Adobe Acrobat Pro with permissions to execute document-level JavaScript, and verify that the target PDF already contains the hidden template page you intend to spawn.
Use the Template Object's Spawn Method with a Target Page Parameter
Directly specify the insertion page index within the spawn method to place the template exactly where you want it, completely avoiding the problematic movePage step.
The most reliable way to place a template page at a specific location is to use the nPage parameter inside the spawn method. This approaches prevents API errors associated with moving pages after they have been instantiated at the end of the document.
Use 'this.getTemplate("YourTemplateName")' to reference the template you want to spawn.
Instead of a basic spawn call, pass an object containing the 'nPage' property. For example: 'var t = this.getTemplate("Testimonials"); t.spawn({nPage: 3, bRename: true, bOverlay: false});'
Delete the 'this.movePage(this.numPages - 1, 3);' line from your script, as the spawn method has already placed the page at the correct index.

Easily Organize and Edit PDF Pages with WPS Office
If you don't want to deal with complex JavaScript programming just to insert or rearrange pages, WPS PDF provides a visual, drag-and-drop interface. As a robust Office alternative, it allows you to easily manage PDF documents alongside Word, Excel, and PowerPoint files.
- 1. Open your PDF: Launch WPS Office and open the PDF file you wish to modify.
- 2. Access page management: Click on the 'Page' tab located in the top navigation ribbon.
- 3. Insert or arrange pages: Use the 'Insert Page' button to add blank pages or pages from other files, and simply drag and drop thumbnails to rearrange them.

Frequently Asked Questions
Why does the Acrobat JavaScript movePage method throw an 'Expected an array object' error?
This error generally occurs when the parameters passed to the movePage method do not match the expected API signature for your specific Acrobat version. Passing comma-separated integers when an array or a specific properties object is expected will trigger this script failure.
Can I spawn a PDF template as an overlay on an existing page?
Yes. When using the spawn method, you can set the 'bOverlay' parameter to true. This will overlay the template contents onto the page specified by the 'nPage' parameter instead of inserting it as a brand new page.
How do I hide or unhide a template before spawning it in JavaScript?
You can control the visibility of a template by setting its hidden property, e.g., 'this.getTemplate("Testimonials").hidden = true;'. However, templates designed for spawning are usually kept hidden by default, and spawning them automatically generates a visible instance.




