PDF Editing Issues

How to Extract the Last Page from Multiple PDFs with Acrobat JavaScript

Steve KSteve K Nov 5, 2026 869 views

Question details

The user needs an Acrobat Action script to automatically extract the final page from multiple PDF documents that have varying page counts.

How to Extract the Last Page from Multiple PDFs with Acrobat JavaScript
Product
Adobe Acrobat
Device & OS
not provided
Scenario
Automating page extraction for multiple PDF files using JavaScript where the last page index must be calculated dynamically.
Observed behavior
Attempting to use static indices or this.extractPage(0) only extracts the first page, and using -1 fails to identify the last page correctly.
Before you start

Ensure you have Adobe Acrobat Pro installed, as the Action Wizard and advanced document-level JavaScript execution are not available in the standard Acrobat Reader.

Solution 1Recommended

Use Acrobat JavaScript to Calculate and Extract the Last Page

Use the extractPages method along with the numPages property to dynamically calculate and extract the last page of any PDF document.

In Adobe Acrobat JavaScript, page numbering starts at zero. This means that if a document has 10 pages, the first page is index 0 and the last page is index 9. Therefore, the last page of any document can be represented as this.numPages - 1.

It is also critical to use the extractPages method instead of extractPage, as extractPages is the correct API call for this action and accepts a parameter object.

1
Open the Action Wizard

Launch Adobe Acrobat Pro, navigate to the Tools menu, select Action Wizard, and click on 'New Action'.

2
Add the Execute JavaScript tool

In the 'Choose tools to add' panel, expand the 'More Tools' section. Select 'Execute JavaScript' and click the plus (+) icon to add it to your Action steps on the right.

3
Enter the JavaScript code

Uncheck the 'Prompt user' box, click 'Specify Settings', and paste the following code: this.extractPages({nStart: this.numPages - 1}); Click OK to save the script.

4
Select target files and run

Under 'Files to be Processed', click 'Add Folder' to select the directory containing your multiple PDF files. Save the Action, give it a name like 'Extract Last Page', and run it to process all files.

Use Acrobat JavaScript to Calculate and Extract the Last Page
Dynamic File Saving: To save the extracted pages to a specific folder automatically, you can expand the script by passing the cPath parameter within the extractPages object, just like in your robust first-page script.
Easy PDF Page Extraction

Extract PDF Pages Intuitively with WPS Office

Skip the complex JavaScript coding. WPS PDF provides a user-friendly visual interface that allows you to quickly select, extract, and manage specific pages or page ranges from your PDF documents without writing a single line of code.

  1. 1. Open your PDF in WPS Office: Launch WPS Office and open the PDF file from which you want to extract the last page.
  2. 2. Navigate to the Page tools: Click on the 'Page' tab located in the top navigation ribbon to reveal page management options.
  3. 3. Select Extract Page: Click on the 'Extract Page' button to open the extraction settings dialogue.
  4. 4. Extract the last page: Visually select the last page thumbnail or enter the final page number in the range field, then click 'Extract' to save it as a new PDF.
Visually select and extract the last page of any PDF without requiring scripts.Fully compatible with standard Adobe PDF formats.Built-in page management tools to extract, split, and merge PDFs effortlessly.Lightweight, fast, and available across Windows, Mac, and mobile platforms.
QA img-9

Frequently Asked Questions

Why does this.extractPage(0) only extract the first page?

Acrobat JavaScript uses a zero-based index for its page numbers. This means the first page is at index 0, the second is at index 1, and so on. Passing 0 to the method will always target the very first page of the document.

What is the difference between extractPage and extractPages in Acrobat JS?

The extractPages method is the official, supported API for extracting document pages in Acrobat JavaScript. It accepts an object with parameters like nStart, nEnd, and cPath. Using extractPage is generally incorrect syntax and may result in script errors.

Can I automatically save the extracted last page to a specific folder?

Yes. You can add the cPath parameter to the extractPages method. By dynamically building the file path string (e.g., appending '_LastPage.pdf' to the original file name), the script will automatically save the new PDF to your desired location.

How do I apply this script to hundreds of PDFs at once?

You can use the Action Wizard in Adobe Acrobat Pro. Create a new Action, add the 'Execute JavaScript' tool with your extraction script, and set 'Files to be Processed' to target the entire folder containing your hundreds of PDFs.