PDF Editing Issues

How to Automatically Delete PDF Pages by Label Name Using JavaScript

Bushra ParveenBushra Parveen Nov 24, 2026 868 views

Question details

The user needs a method to automate the deletion of specific pages in a large PDF document based on custom page labels or prefix names.

How to Automatically Delete PDF Pages by Label Name Using JavaScript
Product
Adobe Acrobat
Device & OS
not provided
Scenario
Managing large PDF documents where specific pages marked with certain text labels or prefixes need to be identified and removed in bulk.
Observed behavior
Successfully identifying and programmatically removing the target pages using JavaScript without causing index shifting or deletion errors.
Before you start

Ensure you have Adobe Acrobat Pro installed, as the free Adobe Reader does not support executing custom page manipulation scripts. It is highly recommended to make a backup copy of your PDF file before running any automated deletion scripts.

Solution 1Recommended

Delete PDF Pages by Label Using Acrobat JavaScript

Utilize a reverse loop in Adobe Acrobat's JavaScript Console to safely identify page labels and delete matching pages without breaking the document's page index.

When deleting pages programmatically via JavaScript, the page indices shift dynamically after every deletion. To prevent script errors and ensure all target pages are deleted correctly, you must loop through the document in reverse order (from the last page to the first page).

1
Open the JavaScript Console

Launch Adobe Acrobat Pro, open your target PDF document, and press 'Ctrl + J' (Windows) or 'Cmd + J' (Mac) to open the JavaScript Debugger console.

2
Set Up the Reverse Loop

In the console, write a for-loop that starts at the last page and decrements to zero: 'for (var i = this.numPages - 1; i >= 0; i--) { ... }'.

3
Retrieve the Page Label

Inside the loop, use the getPageLabel method to fetch the name of the current page: 'var pageLabel = this.getPageLabel(i);'.

4
Add Matching and Deletion Logic

Check if the retrieved label matches your specific criteria (e.g., 'if (pageLabel.indexOf("DeleteMe") !== -1)'). If it matches, delete the page by calling 'this.deletePages(i);'.

5
Execute the Script

Highlight all the code you have typed in the console and press 'Ctrl + Enter' (Windows) or 'Cmd + Enter' (Mac) on your numeric keypad to run the script. The matching pages will be removed instantly.

Delete PDF Pages by Label Using Acrobat JavaScript
Preventing Index Shifts: By looping backward from the highest page number down to zero, the index of the pages you have yet to check remains unaffected by the pages you have already deleted.

Easily Manage and Delete PDF Pages Visually with WPS Office

While writing custom JavaScript requires expensive tools like Acrobat Pro and coding knowledge, WPS Office offers an intuitive, user-friendly PDF Editor that lets you visually organize, extract, and delete specific PDF pages in just a few clicks.

  1. 1. Open your PDF in WPS Office: Launch WPS Office and open the PDF document you want to edit.
  2. 2. Access the Page Manager: Click on the 'Page' tab located in the top navigation ribbon to open the visual page management dashboard.
  3. 3. Select Pages to Remove: Hold down the 'Ctrl' key and click the thumbnails of the specific pages you wish to remove.
  4. 4. Delete Selected Pages: Click the 'Delete Pages' button on the toolbar or simply press the 'Delete' key on your keyboard to instantly remove them.
Select and delete multiple PDF pages visually without needing to write codeFully compatible with all standard PDF documents and formatsLightweight, fast, and easy to navigate for beginners and professionalsComprehensive built-in PDF tools for merging, splitting, and editing
microsoft office alternative - wps office

Frequently Asked Questions

Why do I need to loop backwards when deleting PDF pages with JavaScript?

When you delete a page, the index of all subsequent pages decreases by one. If you loop forwards, you will skip pages and encounter errors because the indices have shifted. Looping backwards from the last page ensures the remaining pages keep their original indices until they are evaluated.

Where do I find the page label using Acrobat JavaScript?

You can retrieve the page label by using the 'this.getPageLabel(pageIndex)' method in your JavaScript code, where 'pageIndex' is the zero-based index of the page you are checking.

Can I use Adobe Acrobat Reader to run this JavaScript?

No, Adobe Acrobat Reader does not have the necessary permissions or the JavaScript Debugger console to execute document-level page deletion scripts. You must use Adobe Acrobat Pro or Standard to run automation scripts.

What happens if a custom page label does not exist?

If a custom page label has not been set, the 'getPageLabel()' method typically returns the physical page number as a string (e.g., '1', '2'). Your script should account for these default values if you are specifically searching for text prefixes.