How to Find a Sentence and Delete Its PDF Page Using JavaScript
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.

- Product
- 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.
Ensure you have a backup of your original PDF document and a basic understanding of JavaScript syntax before running custom scripts.
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.
Open your advanced PDF editor and navigate to the Action Wizard or JavaScript Debugger tool.
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.
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.
If the extracted sequence of words matches your target sentence, execute 'this.deletePages(pageIndex)'.

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. Open your document: Launch WPS Office and open your PDF file using the WPS PDF tool.
- 2. Search for the sentence: Press 'Ctrl + F' to open the Search panel, then type the exact sentence you want to locate.
- 3. Identify the page number: Look at the search results to note the specific page number where your target sentence is highlighted.
- 4. Access Page Management: Navigate to the 'Page' tab on the top ribbon and click on 'Delete Pages'.
- 5. Delete the page: Enter the identified page number into the prompt and click confirm to instantly remove it from your document.

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.




