PDF Editing Issues

Use openDoc() to Insert Pages into the Correct PDF in Acrobat JavaScript

Adam DavisAdam Davis Nov 19, 2026 868 views

Question details

The user needs an Acrobat Action Wizard script to insert pages into each newly opened PDF and save the result.

Use openDoc() to Insert Pages into the Correct PDF in Acrobat JavaScript
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().
Before you start

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.

Solution 1Recommended

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.

1
Open the Action Wizard script

In Acrobat, open the relevant Action Wizard action and edit its Execute JavaScript step.

2
Store the opened document

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});

3
Insert pages into the target

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.

4
Save and close the target

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.

5
Test the batch

Run the action on a small set of PDFs and open the saved output files to verify that each received the expected pages.

Use the Document Returned by openDoc() for Page Insertion
Check save permissions: Action Wizard does not automatically save or close the document opened by your script. Acrobat security restrictions may require a permitted or privileged context for file operations.
Free Microsoft Office alternative

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.

Organize PDF pages without writing an Acrobat scriptOpen and edit Microsoft Office formats including DOCX, XLSX, and PPTXUse a familiar interface for an easier move from Microsoft OfficeWork with documents in a lightweight application with a free version
microsoft office alternative - wps office

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.