How to Extract and Delete the Last Page of a PDF Using JavaScript
Question details
The user wants to configure a folder-level JavaScript script to dynamically identify, extract, and then delete the final page of any open PDF document.

- Product
- PDF Editor
- Device & OS
- not provided
- Scenario
- Automating the separation and removal of the last page from PDF files using a custom script or add-in.
- Observed behavior
- The user needs the correct JavaScript syntax and parameters to ensure the script accurately targets the last page regardless of the total page count.
Ensure you have the necessary administrative permissions to save .js files into your PDF application's Javascripts directory, and that you know your exact local output folder path.
Modify JavaScript Parameters to Target the Last Page
Use the this.numPages property to dynamically calculate and target the final page for extraction and deletion.
In PDF JavaScript APIs, page arrays are zero-indexed. This means the first page is 0, and the final page is always the total number of pages minus one. By setting both the start and end parameters to 'this.numPages - 1', you can reliably target the last page.
In your JavaScript code, modify the extractPages function so the parameters read: nStart: this.numPages-1, nEnd: this.numPages-1. Be sure to specify the correct output path (cPath) for the extracted file.
Immediately following the extraction command, add the deletePages function using the same parameters: this.deletePages({nStart: this.numPages-1, nEnd: this.numPages-1}); to remove the page from the original file.
Wrap your script in app.beginPriv() and app.endPriv() if you are executing this as a trusted function from a folder-level script, allowing it to save the output files automatically.
Save your complete script as a .js file inside the Acrobat Javascripts directory to load it as a custom tool in your Edit menu.

Extract and Delete PDF Pages Visually with WPS Office
If you prefer not to deal with complex JavaScript coding and folder directories, WPS Office provides an intuitive visual interface to effortlessly extract, delete, and manage your PDF pages in just a few clicks.
- 1. Open your PDF: Launch WPS Office and open the PDF document you wish to edit.
- 2. Go to the Page Tab: Navigate to the 'Page' tab located on the top toolbar to view all pages as thumbnails.
- 3. Extract the Last Page: Select the last page, click on 'Extract Page', choose your output location, and save it as a new PDF.
- 4. Delete the Page: With the last page still selected in the original document, click 'Delete Page' to remove it.
- 5. Save Changes: Click 'Save' or press Ctrl+S to keep the changes to your original document.

Frequently Asked Questions
Why is the last page represented as 'this.numPages - 1'?
Acrobat JavaScript relies on zero-based indexing for page numbers. The first page is recognized as page 0, which means the index of the final page is always exactly one less than the total page count.
Where should I save folder-level JavaScript files?
Folder-level scripts should be saved as .js files in the application's specific Javascripts directory. This allows the PDF software to load the script automatically as a trusted Add-In when the application launches.
Can I extract PDF pages without deleting them?
Yes. To keep the original document intact, simply use the this.extractPages function in your script and omit the this.deletePages command.
How do I format local file paths in my JavaScript?
File paths (cPath) must be formatted using forward slashes. On macOS, paths typically begin with the hard drive name (e.g., /Macintosh HD/Users/...). On Windows, they start with the drive letter (e.g., /C/Users/...).




