PDF Editing Issues

How to Extract and Delete the Last Page of a PDF Using JavaScript

Huda QurayshiHuda Qurayshi Nov 24, 2026 869 views

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.

How to Extract and Delete the Last Page of a PDF Using JavaScript
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.
Before you start

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.

Solution 1Recommended

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.

1
Update the Extraction Script

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.

2
Update the Deletion Script

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.

3
Set Up Privileged Execution

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.

4
Save as an Add-In

Save your complete script as a .js file inside the Acrobat Javascripts directory to load it as a custom tool in your Edit menu.

Modify JavaScript Parameters to Target the Last Page
Save Your Original File: Don't forget to include app.execMenuItem("Save"); in your script after the deletion command to ensure the original PDF is updated.

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. 1. Open your PDF: Launch WPS Office and open the PDF document you wish to edit.
  2. 2. Go to the Page Tab: Navigate to the 'Page' tab located on the top toolbar to view all pages as thumbnails.
  3. 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. 4. Delete the Page: With the last page still selected in the original document, click 'Delete Page' to remove it.
  5. 5. Save Changes: Click 'Save' or press Ctrl+S to keep the changes to your original document.
No coding or scripts required to manage PDF pagesIntuitive visual page manager for quick extractionsFully compatible with standard Adobe PDF formatsLightweight, fast, and user-friendly interface
microsoft office alternative - wps office

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/...).