Use openDoc() to Insert Pages into the Correct PDF in Acrobat JavaScript
Question details
The user needs an Acrobat Action Wizard script to insert pages into each newly opened PDF and save the result.

- Product
- Adobe Acrobat Action Wizard and Acrobat JavaScript
- Device & OS
- not provided
- Scenario
- A batch action processes multiple similarly named PDFs by opening a second document and inserting pages into it.
- Observed behavior
- The script inserts pages into the original document because it uses the original document context rather than the object returned by openDoc().
Identify the target, source, and output paths for each PDF, and check that the PDFs permit page insertion. Run the action in an Acrobat context with permission to open and save files.
Use the Document Returned by openDoc() for Page Insertion
Use an explicit document reference so the script modifies the intended PDF regardless of which document has focus.
In Acrobat, open the relevant Action Wizard action and edit its Execute JavaScript step.
For each file, set targetPath, sourcePath, and outputPath to the appropriate paths, then assign the opened PDF to a variable: var target = app.openDoc({cPath: targetPath});
Call insertPages on target rather than this. For example, append the first page of the source PDF with: target.insertPages({nPage: target.numPages - 1, cPath: sourcePath, nStart: 0, nEnd: 0}); Set nEnd to the last zero-based source page index when inserting a range.
After a successful insertion, explicitly save and close the opened document: target.saveAs({cPath: outputPath}); target.closeDoc(true); Repeat these operations for each target PDF.
Run the action on a small set of PDFs and open the saved output files to verify that each received the expected pages.

Manage PDF Pages and Office Files with WPS Office
WPS Office is a free, lightweight alternative with a familiar interface for working with PDFs and Office files. It can help with manual PDF page organization, but it does not run Acrobat Action Wizard scripts or fix Acrobat JavaScript. PDF editing features may depend on your version or plan.

Frequently Asked Questions
Why does insertPages modify the original PDF instead of the newly opened one?
In the action's JavaScript context, this can still refer to the original document. Assign app.openDoc() to a variable and call insertPages on that variable.
How do I append more than one source page?
In target.insertPages(), set nStart and nEnd to the first and last zero-based source page indexes. Set nPage to target.numPages - 1 to insert after the target's last page.
Does Action Wizard save a PDF opened by my script automatically?
No. Call saveAs() on the returned document object and then call closeDoc() after the save succeeds. Ensure the action has the required file-operation permissions.
Can WPS Office run this Acrobat JavaScript batch action?
No. The script uses Acrobat-specific APIs and Action Wizard. WPS Office can be used for manual PDF page organization, subject to feature availability.




