How to Insert a Same-Name PDF Cover Page Using Acrobat JavaScript
Question details
User needs to automate the insertion of cover-page PDFs into source PDFs by matching their file names using JavaScript.

- Product
- Adobe Acrobat Pro 2020
- Device & OS
- not provided
- Scenario
- Processing multiple PDF files to prepend matching cover pages located in a different directory.
- Observed behavior
- Requires a working JavaScript snippet to locate the correct cover file, insert it at the beginning of the document, and save the updated PDF with a new name.
Verify that all your cover page PDFs have the exact same file names as your source PDFs and always test scripts on duplicate files before processing your entire folder.
Use Acrobat JavaScript to Automate Cover Page Insertion
Apply a custom JavaScript snippet via the Action Wizard to automatically fetch and insert matching cover pages from a designated folder.
This method utilizes Acrobat Pro's JavaScript API to match the current document's filename with a cover page in a separate directory. It inserts the cover at the very beginning (page 0) and saves a new copy to avoid overwriting your original files immediately.
Place all your source PDFs in one folder and the corresponding cover pages (with identical filenames) in a separate folder, such as 'C:/Covers/'.
Open Acrobat Pro and navigate to Tools > Action Wizard > New Action.
Under 'Files to be Processed', add the folder containing your source PDFs. Under 'Choose tools to add', select 'More Tools', choose 'Execute JavaScript', and click the plus icon to add it to your action steps.
Uncheck the 'Prompt user' box, click 'Specify Settings', and paste the following script: var coverDirectory = "C:/Covers/"; this.insertPages({nPage: -1, cPath: coverDirectory + this.documentFileName}); this.saveAs({cPath: this.path.replace(/\.pdf$/i, " (with cover).pdf"), bPromptToOverwrite: true});
Save the Action with a recognizable name, then click on it in the Action Wizard pane to execute it. The script will process each file and save the newly merged PDFs in your source directory.

Insert Cover Pages Easily with WPS PDF
If you prefer a visual, code-free way to manage your PDF documents, WPS PDF allows you to easily insert cover pages or merge multiple files in just a few clicks.
- 1. Open your source PDF: Launch WPS Office and open your main PDF document in the WPS PDF viewer.
- 2. Navigate to the Page tools: Click on the 'Page' tab located in the top ribbon menu, then select the 'Insert Pages' option.
- 3. Insert the cover page: Choose the cover page PDF file from your computer folder and set the insertion position to 'First Page' or 'Before Page 1'.
- 4. Save the merged document: Click 'OK' to merge the pages, then go to Menu > Save As to save your newly updated document.

Frequently Asked Questions
Why is my Acrobat JavaScript failing to find the cover page?
This typically occurs if the file path uses backslashes (\) instead of forward slashes (/), or if the file names in the source and cover directories do not match exactly, including case sensitivity.
Can I use this script to insert non-PDF cover pages like Word or image files?
No, the JavaScript 'insertPages' method is specifically designed for merging PDF documents. Any Word documents or image files must be converted to PDF format before running the script.
How do I run this JavaScript across multiple files at once?
You can automate it by using the Action Wizard in Acrobat Pro. Create a new Action, add the 'Execute JavaScript' tool with your script, select your entire folder of source PDFs as the target, and run the Action.




