PDF Editing Issues

Delete the Last Two PDF Pages with Acrobat JavaScript

Tauseeq MagsiTauseeq Magsi Nov 18, 2026 868 views

Question details

Delete the last two pages of a PDF using Acrobat JavaScript.

Delete the Last Two PDF Pages with Acrobat JavaScript
Product
Adobe Acrobat
Device & OS
not provided
Scenario
The user wants a script that removes the final two pages regardless of the document's length.
Observed behavior
Acrobat JavaScript uses zero-based page indexes, so the last two pages of a seven-page PDF have indexes 5 and 6.
Before you start

Open the PDF in an Acrobat edition that supports page deletion and the JavaScript console. Check that the document has more than two pages and that its permissions allow page editing.

Solution 1Recommended

Delete the Last Two Pages with a Page-Count Script

Use this script when the PDF may have different page counts.

The script uses numPages to find the final two zero-based indexes. The condition prevents it from running on a document with two or fewer pages.

1
Open the JavaScript console

Open the PDF in Acrobat, then press Ctrl+J on Windows or Command+J on macOS to open the JavaScript console.

2
Enter and run the script

In the console, enter `if (this.numPages > 2) this.deletePages(this.numPages - 2, this.numPages - 1);`. Select the line and press Ctrl+Enter on Windows or Command+Enter on macOS to run it.

3
Check and save the PDF

Check the page thumbnails to confirm the final two pages were removed, then choose File > Save or File > Save As.

Delete the Last Two Pages with a Page-Count Script
Seven-page example: For a seven-page PDF, `this.deletePages(5, 6);` deletes the final two pages. Page indexes start at 0, and the end index is included.
Edit PDF pages in WPS Office

Delete the Last Two PDF Pages in WPS Office

WPS PDF can remove the final two pages through its page management tools, without running Acrobat JavaScript.

  1. 1. Open the PDF: Open WPS Office and use Open to select your PDF.
  2. 2. Find the last pages: Open the page thumbnail panel and identify the final two pages.
  3. 3. Delete the pages: Select the last two thumbnails, then use the Delete Pages command in the page tools or thumbnail context menu.
  4. 4. Save the result: Review the remaining pages and choose Save or Save As.
Delete selected PDF pages without writing a scriptView page thumbnails to identify the final two pagesOpen and edit Microsoft Office formats, including DOCX, XLSX, and PPTXUse a familiar interface for PDF and Office documents
microsoft office alternative - wps office

Frequently Asked Questions

Why does the script use indexes 5 and 6 for a seven-page PDF?

Acrobat JavaScript starts page indexes at 0. In a seven-page PDF, the indexes run from 0 through 6, so `this.deletePages(5, 6);` removes pages 6 and 7.

Will the page-count script work on PDFs of different lengths?

Yes. `this.numPages - 2` and `this.numPages - 1` identify the final two indexes. The supplied condition runs the deletion only when the PDF has more than two pages.

What happens if the PDF has only two pages?

The condition `this.numPages > 2` is false, so the script does not delete any pages.

Why might Acrobat refuse to delete the pages?

Page deletion may be unavailable in your Acrobat edition or restricted by the PDF's permissions. Check the document's security settings and use an edition that supports page editing.