Help with adding the word count for multiple sections on form to one total word count field
Copy link to clipboard
Copied
Hello,
I have a form where I have multiple fields (awords, bwords, cwords, dwords, ewords). I need the word counts from each of the 5 sections to add up into one total word count field. I don't need to limit the words, I just need the total number of words from each of the sections to add up into the total words field.
Since my IT department claims they don't really do javascript, I am on my own.
My team and I spending hours adding up each individual section to get total word count for form submitted to us. Having the word total added up automatically would be a total game changer. Thanks for any help!
Michelle
Copy link to clipboard
Copied
The word count is found by splitting the field value on white space. Then the number of elements is the words.
Here's a script for a single field. You can run it in the Acrobat Console window.
You can find a tutorial here:
https://www.pdfscripting.com/public/Free_Videos.cfm#JSIntro
nWrdCnt = this.getField("awords").valueAsString().replace(/(^\s+)|(\s+$)/g,"").split(/\s+/mg).length;
This needs to be done for each field.
If you need this number inside the form, you can add a "WordCount" field and make the script a calculation.
If you need this number outside the form, then it should be an automation tool or a Console window script.
Use the Acrobat JavaScript Reference early and often
Copy link to clipboard
Copied
Use the code Thom provided to calcluate the number of words for each field, and then add up those numbers, either directly in your code or by placing them in (hidden) text fields and then adding those up.
Copy link to clipboard
Copied
There's a tab labeled calculation in the field properties where you have an option to calculate the sum of selected fields. Once you follow @Thom_Parker code to get the word count into the awords, bwords ... etc fields, just create a field for example "total-word-counts" and on the properties select the option I mentioned earlier.

