PDF Editing Issues

How to Find a Sentence and Delete Its PDF Page Using JavaScript

Amos GikundaAmos Gikunda Nov 3, 2026 869 views

Question details

The user wants to automatically locate a specific sentence within a PDF document and delete the entire page that contains it using an automated script.

How to Find a Sentence and Delete Its PDF Page Using JavaScript
Product
PDF
Device & OS
not provided
Scenario
Automating the removal of specific PDF pages based on their text content.
Observed behavior
Standard PDF actions cannot find whole sentences directly to trigger page deletion; custom JavaScript using successive word matching is required.
Before you start

Ensure you have a backup of your original PDF document and a basic understanding of JavaScript syntax before running custom scripts.

Solution 1Recommended

Using Custom JavaScript to Extract Words and Delete Pages

Write a custom script to loop through words, piece together the target sentence, and remove the corresponding page.

Since PDF scripting engines typically use methods like 'getPageNthWord' which return only individual words or tokens, your script must extract consecutive words and concatenate them to check against your target sentence.

Once a complete match is detected, the script can call the 'deletePages' method for that specific page index. Be aware that deleting pages dynamically alters the total page count, so it is best to loop through the document in reverse.

1
Access the JavaScript Console

Open your advanced PDF editor and navigate to the Action Wizard or JavaScript Debugger tool.

2
Set up a reverse loop

Write a 'for' loop that iterates through 'this.numPages' backwards (from the last page to the first page) to prevent index shifting when a page is deleted.

3
Extract and match words

Inside the loop, retrieve the word count for the page. Use 'this.getPageNthWord(pageIndex, wordIndex)' to extract consecutive words, ignoring punctuation, to see if they form your target sentence.

4
Execute the delete command

If the extracted sequence of words matches your target sentence, execute 'this.deletePages(pageIndex)'.

Using Custom JavaScript to Extract Words and Delete Pages
Script Complexity: This requires advanced JavaScript coding skills. Depending on the PDF's text formatting, words may be extracted with unexpected spaces or punctuation, which you will need to clean using Regular Expressions.

Easily Search and Delete PDF Pages with WPS Office

Instead of spending hours writing and debugging complex JavaScript code, you can quickly find any sentence and delete the containing pages using WPS PDF's intuitive interface.

  1. 1. Open your document: Launch WPS Office and open your PDF file using the WPS PDF tool.
  2. 2. Search for the sentence: Press 'Ctrl + F' to open the Search panel, then type the exact sentence you want to locate.
  3. 3. Identify the page number: Look at the search results to note the specific page number where your target sentence is highlighted.
  4. 4. Access Page Management: Navigate to the 'Page' tab on the top ribbon and click on 'Delete Pages'.
  5. 5. Delete the page: Enter the identified page number into the prompt and click confirm to instantly remove it from your document.
No coding or JavaScript knowledge requiredFully compatible with standard PDF documents created by Microsoft Office or AdobeUser-friendly interface for finding text and visually managing pagesFree and lightweight alternative to heavy PDF editors
microsoft office alternative - wps office

Frequently Asked Questions

Can I use a standard Action Wizard to delete pages based on text?

No, standard Action Wizards do not have a built-in feature to delete pages conditionally based on text content. You must implement custom JavaScript to achieve this functionality.

Why doesn't the getPageNthWord method find whole sentences?

The getPageNthWord function is specifically designed to extract single words or tokens from the PDF's underlying text stream. To find a full sentence, your script must extract an array of consecutive words and join them together for comparison.

What happens to the page numbering after deleting a page via script?

The page numbering dynamically shifts immediately after a page is deleted. If you are looping forward through pages to delete them, the index will break. Always loop backwards (from the highest page number to 0) when deleting pages programmatically.

Does WPS Office support custom JavaScript for PDFs?

WPS PDF focuses on user-friendly, UI-based tools for editing and managing PDFs. While it does not process advanced Acrobat-specific JavaScript console actions, it offers robust manual search and page management features that bypass the need for coding.