These forums are now Read Only. If you have an Acrobat question, ask questions and get help from one of our experts.

Word count a field

Anonymous
Answered

Hi everybody,

Is it possible to get a word count to appear in a separate text field.

For example, I have this text field that people will type their advert text into, I would really like a new field to calculate the number of words and enter it into the new field. Is this possible using Javascript?
Any help would be muchly appreciated.

Thanx

My Product Information:
Acrobat Pro 9.1.1, Macintosh
gkaiseril
Online
Expert
Registered: Feb 23 2006
Posts: 4307
Yes, but you will need to write a custom script to perform this task. A simple script was presented in [url=http://www.acrobatusers.com/forums/aucbb/viewtopic.php?id=17726]AcroForm Word Count Validation[/url]. A simple function to provide this information and adjust for the posability of numeric entry could be:
// document level function that counts wordsfunction WordCount(cString) {// force string to character stringcString = cString.toString();// first set to length of stringvar fCount = cString.length;// if length is greater than zero count wordsif(fCount > 0){// split string at spacevar aWords = cString.split(" ");fCount = aWords.length} // end length > 0return fCount} // end WordCount

You did not specify if you wanted the count as the characters are entered.

George Kaiser

smitchell15 (not verified)
Thanx for replying,

Sorry if this sounds a bit simple as not really a Javascript expert yet! But where do you enter the script you gave me. Would I enter this in the field NOT containing the advert text and would it be in the validation/calculate tab?

Ideally, i would like the text field NOT containing the advert text to just have the number of words that the other text field has?

I used the link you gave me and manage to manipulate it to this. This gave me an exact word count of the field which was good but I would still really like that number to appear in the text field?


var f=this.getField("1a").value;
var aWords = f.split(" ");
var nNumWords = aWords.length;

if (nNumWords > 10) {
app.alert("There is " + nNumWords + " words.\n");
}


Thanx again
gkaiseril
Online
Expert
Registered: Feb 23 2006
Posts: 4307
If you want this to happen while the text is being entered, one would use the 'custom keystroke scirpt'. If one wanted this check performed on comitting the entry, one would use the 'custom validataion script'.

George Kaiser

smitchell15 (not verified)
Thats brilliant thankyou, finally managed to crack it with this code

var f=this.getField("1a").value;
var aWords = f.split(" ");
var nNumWords = aWords.length;
event.value=nNumWords;

Thankyou, this has been bugging me all week
jpsb
Registered: Jul 24 2009
Posts: 1
I am having trouble implementing the same functionality.

I have been able to count the words in a text field and display them in a message box using the script below:

var wordCnt = new Array();
var str = this.rawValue;
wordCnt = str.split(" ");
xfa.host.messageBox("There are " + wordCnt.length + " words.");

But I want the number of words to be displayed in a separate numeric or text field directly onto the page, preferably as the words are typed.

I have to admit I am a bit of a n00b, so any help is appreciated.
nixopax
Registered: Feb 6 2009
Posts: 105
Rather than use messageBox(), make a text field named 'wordcount' and then use this:

var wordCnt = new Array();var str = this.rawValue;wordCnt = str.split(" ");xfa.resolveNode("wordcount").rawValue = "There are " + wordCnt.length + " words.";
dmalloy
Registered: Oct 20 2010
Posts: 21
I am having a very simular issue but I am trying to count the characters in multiple feilds with a total amount not exceeding 24,000. I am trying to export the data and they gave me a cap on the amout of characters that I can export out of one document. So wanted to do a character count instead of a word count but I can get the coding right here is what I have. Not the greatest at java scrpit so try not to lol too much. :)

function CountLeft(field, count, max)
{
if (field.value.length > max) field.value = field.value.substring(0, max); else
count.value = max - field.value.length;
}

Only 24000 characters allowed!
characters left
gkaiseril
Online
Expert
Registered: Feb 23 2006
Posts: 4307
You have to call the function in order for the defined function to run.

Have you tried running your function in the JavaScript console?

To count the characters in a field, including leading zeros in numeric fields:

  1. function CountCharacters(cFieldName) {
  2. /*
  3. return the number of characters in a field
  4. */
  5. console.println("field name: " + cFieldName);
  6. // get the field object
  7. var oField = this.getField(cFieldName);
  8. console.println("object: " + oField);
  9. // get the value of the field
  10. var cFieldValue = oField.valueAsString;
  11. console.println(cFieldValue);
  12. //return the number of characters in the field including spaces
  13. return cFieldValue.length;
  14. } // end CountCharacters

To count the characters in a field named "Text1":
CountCharacters("Text1");

George Kaiser

dmalloy
Registered: Oct 20 2010
Posts: 21
Okay I am going to try this out and see what happens. I am going to post the results onces I get it to work.

dmalloy
Registered: Oct 20 2010
Posts: 21
okay nothing is happening when I put this inside of the "document Java script" field under advanced. Then I named the script cFieldName. delete everything in the box and place the script in the box and pressed okay and dont get any error messages. Was there something that I did wrong? Do I need to save the file and then reopen the file in order for it to run the script? Or I might be placing the script in the wrong place.

dmalloy
Registered: Oct 20 2010
Posts: 21
this is what I got in the debugger

Acrobat Annotations / Collaboration Built-in Wizard Functions Version 9.0
Acrobat SOAP 9.0


CountCharacters is not defined
1:AcroForm:text1:Keystroke
CountCharacters is not defined
1:AcroForm:text1:Keystroke
gkaiseril
Online
Expert
Registered: Feb 23 2006
Posts: 4307
You are causing yourself a lot of issues by duplicate post.

Where have you placed the function 'CountCharacters'?

If that code has not been initialized by the application, Acrobat/Reader, or the PDF, it does not exist for use.

Since you are trying to count the number of characters in a number of fields. The code will provide the count for one field. With this script you can build another script to add the other fields to obtain the total number of characters in all of the fields.

George Kaiser