How to Count Words on the Current PDF Page Using JavaScript
Question details
The user needs a JavaScript snippet to count the exact number of words on the currently viewed page of a PDF, rather than the entire document, and display this word count in a pop-up dialog box.

- Product
- PDF Editor
- Device & OS
- not provided
- Scenario
- Developing or running a PDF script in a console to analyze text content dynamically on the active page.
- Observed behavior
- The user wants a script that explicitly retrieves the word count of the current page and formats it into a readable dialog alert.
Ensure you are using a professional PDF editor that supports an interactive JavaScript console (such as Adobe Acrobat Pro) to execute custom document scripts.
Use getPageNumWords with an Alert Dialog
Run a specific JavaScript command in your PDF console to retrieve the word count for the currently viewed page and generate a pop-up alert box.
By utilizing the built-in 'getPageNumWords' function along with 'this.pageNum', you can pinpoint the word count to just the page currently visible on your screen.
The 'app.alert' method is then used to push this data into a user-friendly pop-up window.
Launch your PDF editor and open the JavaScript Debugger or Console from the developer or tools menu.
Type the following code to fetch the word count of the active page: var numWords = this.getPageNumWords(this.pageNum);
On the next line, type the code to trigger the pop-up: app.alert("There are " + numWords + " words on this page.", 3);
Highlight the lines of code and press Ctrl+Enter (or the Execute button) to run the script. A dialog box will instantly display the word count.

Manage and Analyze PDF Text Easily with WPS Office
If you need a simpler way to analyze PDF text without writing complex JavaScript, WPS Office offers powerful built-in PDF tools. You can easily convert PDFs to Word documents to instantly access detailed text statistics and word counts without any coding.
- 1. Open the PDF: Launch WPS Office and open your target PDF document.
- 2. Convert to Word: Navigate to the 'Tools' or 'Home' tab and select the 'PDF to Word' feature.
- 3. Select the Page: Choose the specific page range you want to analyze and click 'Convert'.
- 4. View Word Count: Once converted, the document will open in WPS Writer. Simply look at the status bar at the bottom of the screen to see the exact word count.

Frequently Asked Questions
Can I count words for a specific page number instead of the current page?
Yes. You can replace 'this.pageNum' with a specific zero-based index. For example, to count the words on the very first page of the document, you would use 'this.getPageNumWords(0);'.
Does this JavaScript work in all PDF readers?
No. JavaScript execution is typically limited to advanced PDF editing software. Basic PDF readers or web browser viewers usually do not support interactive JS console commands or the 'app.alert' function.
How do I count words in the entire PDF using JavaScript?
To count words across the whole document, you must create a loop that iterates through all pages from 0 to 'this.numPages - 1', adding the result of 'getPageNumWords(i)' for each page to a total sum variable.




