How to Replace All Even or Odd Pages in a PDF Automatically
Question details
The user needs an automated method to replace every even or odd page within a PDF document with a specified replacement page.

- Product
- Device & OS
- not provided
- Scenario
- Updating repeating elements, placeholders, or alternating templates in a large PDF document without manually replacing pages one by one.
- Observed behavior
- A manual replacement process is too time-consuming, necessitating an automated script to loop through the document and replace pages based on their parity (even or odd).
Before running a page replacement script, check that your source PDF and the replacement page share the same page dimensions and orientation to prevent alignment issues in the final document.
Use Adobe Acrobat Console JavaScript
Run a custom JavaScript loop within Adobe Acrobat to automatically identify and replace alternate pages based on their index number.
Adobe Acrobat Professional supports document-level JavaScript execution. By utilizing the replacePages() method in a loop, you can target specific page indices (even or odd) and swap them out seamlessly.
Open your target PDF in Adobe Acrobat. Go to 'Tools' > 'JavaScript' and select 'JavaScript Debugger' (or press Ctrl+J / Cmd+J).
In the console, write a loop that iterates through the document's pages. Use the modulo operator (i % 2 === 0 for odd pages, since index starts at 0, or i % 2 !== 0 for even pages) to determine parity.
Within the loop, use the 'this.replacePages()' command, specifying the current page index as the target and the file path of your replacement PDF as the source.
Highlight the script in the console and press Enter (on the numeric keypad) or click 'Execute'. Once the pages are updated, save your document.

Use an External Python Script (PyPDF2)
Use Python and a PDF library like PyPDF2 to programmatically build a new PDF featuring your desired alternating page replacements.
Easily Manage and Replace PDF Pages with WPS Office
While scripting is great for automated parity replacements, WPS Office provides an incredibly intuitive, visual Page Management interface to quickly replace, extract, or insert multiple pages without needing to write any code.
- 1. Open your PDF: Launch WPS Office and open the PDF document you need to edit.
- 2. Navigate to the Page tools: Click on the 'Page' tab located in the top navigation ribbon to open the page management workspace.
- 3. Select the Replace option: Click the 'Replace' button. A dialog box will appear allowing you to select the replacement PDF file from your computer.
- 4. Set the page range: Specify the exact pages or the page range in your current document that you want to replace, and map them to the corresponding pages of the new file.
- 5. Confirm and save: Click 'OK' to instantly replace the selected pages. Review your updated document and click 'Save'.

Frequently Asked Questions
Does replacing a PDF page remove the original page's links?
Yes. When you replace a page, any hyperlinks, buttons, or form fields that were explicitly placed on the original page are discarded and replaced by the interactive elements (if any) present on the new page.
Can I replace multiple specific pages at once without a script?
Yes, using advanced PDF editors like WPS Office, you can select the 'Replace' tool and input a custom page range (e.g., 2, 4, 6, 8) to swap out multiple specified pages in one batch action.
Why did my Acrobat JavaScript fail to replace the pages?
Common reasons for script failure include incorrect file path syntax for the replacement file, syntax errors in your loop (such as zero-based index confusion), or lacking the necessary security permissions to modify the active PDF document.




