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

Adding the character count of two fields while typing

revoxo
Registered: Jul 20 2009
Posts: 17
Answered

If anyone can help point me in the right direction I'd be very grateful.

I'm currently using Acrobat Pro v8.0 (Mac)

I have three fields: "Name1", "Name2" and "CharCnt"

I would ideally like the field "CharCnt" to add the two other field lengths together to give a total character count while typing text in either "Name1" or "Name2". A live count if you will.

I have "CharCnt" doing the job already but it only calculates the total when I exit or move to the next field. I've tried mucking around with various custom keystroke scripts (through much digging of this forum) but they either don't work at all (probably because of my feeble attempts) or I'm back where I started.

Any help gratefully received :0)

My Product Information:
Acrobat Pro 8.1.7, Macintosh
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
If you already have a script for counting characters and you understand how it works then you are a good bit of the way there. And you are correct that the keystroke event is the right way to go. It is the only event that will deliver the realtime data entry. The keystroke event is tricky to use. Have you read this article?

http://www.acrobatusers.com/tutorials/2006/formatting_text_fields

It is important that the script is qualified with event.willCommit. Also, the full entry string is not provided, but there is enough information in the event object to build it.
if(!event.willCommit){var nChars = event.selEnd - event.selStart;var aFull = event.value.split("");aFull.splice(event.selStart, nChars, event.change);var strFull = aFull.join(""); ... Code for Counting characters ...}

The next bit, adding two counts together, is even trickier. It is an extremely bad idea to use a calculation event in this case. In fact, all calcuations should be turned off while the user is entering data so that the form doesn't go out to lunch. This particular keystroke scirpt will be entering data into a field (the count field), which will prompt acrobat to fire calculation event.

Here's the technique, since the user can only enter data into one field at a time, the keystroke script should acquire the other text field, do the addition and put the result into the count field. The count field is then set from whichever text field the user is currently using.

Thom Parker
The source for PDF Scripting Info
[url=http://www.pdfScripting.com]pdfscripting.com[/url]

The Acrobat JavaScript Reference, Use it Early and Often
[url=http://www.adobe.com/devnet/acrobat/javascript.php]http://www.adobe.com/devnet/acrobat/javascript.php[/url]

Then most important JavaScript Development tool in Acrobat
[url=http://www.pdfscripting.com/public/34.cfm#JSIntro][b]The Console Window (Video tutorial)[/b][/url]
[url=http://www.acrobatusers.com/tutorials/2006/javascript_console][b]The Console Window(article)[/b][/url]

Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

revoxo
Registered: Jul 20 2009
Posts: 17
Thanks Thom

Where would we be without Sir "Tim" Berners-Lee!

You've given me a lot to think over, I'd not seen your article before so I'll digest and cogitate.
If I get this to work I'll post the code.

Many thanks again.
Dave
revoxo
Registered: Jul 20 2009
Posts: 17
Further to my question I'd like to thank Thom once again!
The solution works very nicely indeed. The tutorial document was a goldmine of useful stuff I'll be applying to my other projects. For anyone who has a similar need, the code I finally used follows:

Code:
//put in Custom Keystroke Script of the two "Name" fields to be counted
if(!event.willCommit)
{
var oName = this.getField("Name2"); //the other field to count (change accordingly)
var nDisplay = this.getField("CharCnt"); //to display the result
var nChars = event.selEnd - event.selStart;
var aFull = event.value.split("");
aFull.splice(event.selStart, nChars, event.change);
var strFull = aFull.join("");
nDisplay.value = strFull.length + oName.value.length;
}

A further modification for a word count:

Code:
//put in Custom Keystroke Script of field to be counted
if (!event.willCommit) {
if (event.change) {
var nDisplay = this.getField("WdCount");
var aFull = event.value.split(" ");
nDisplay.value = aFull.length;
}
}

Also seems to work well, though I've only tested briefly.

Without this forum and all the contributors and experts who devote some of their valuable time, I'd be well and truly stuck on many an occasion!

Dave
Greaser
Registered: Jun 1 2011
Posts: 4
The above code worked perfectly for me, but for only two fields. How can I alter it to take 5 different fields of character counts rather than just two?