9 Replies Latest reply on Aug 10, 2013 10:05 AM by gkaiseril

    Character counting (count down) in Acrobat

    b.whitham Level 1

      I have a PDF file with text fields that is going to be used by some of our writers and integrated into presentation decks across multiple programs. We have a lot of it working well and I've been able to piece some bits of code together here and there but I'm no programmer. Where I've hit a roadblock is with adding a visual character counter to the file.

       

      The file is set up to reflect an actual printed piece where there are limitations on line counts and characters per line. The line counts we aren't as concerned with but having a counter display how many characters you've typed to a line is important. I hope this makes sense. So basically we have one area where you can have a total of 10 lines and 30 characters (including spaces) on each line for a total of 300 characters. What would be great for us is if the text field could have a counter pop up showing something stating that the writer has entered "10 out of 30" characters for the particular line they're on.

       

      Is this a simple fix or something where I could be pointed somewhere to put a script togeteher? I've tried for a few days now and frankly this is like launching a missle into space for me. I'd greatly appreciate any help/insight you could offer and would be glad to share my expertise on any design or experience issues you may be having.

        • 1. Re: Character counting (count down) in Acrobat
          George_Johnson MVP & Adobe Community Professional

          Set up a document-level function like the following and call it from the text field's Keystroke event:

           

          // Document level function
          function showCharCount() {
          
              // Get everything that has been entered into the field
              var num = AFMergeChange(event).length;
          
              // Set the maximum number of characters
              var max = 300;
          
              // Set the value of the other text field to show how many character have been entered
              getField("Text2").value = num + " out of " + max;
          
          }
          

           

          Replace "Text2" with the name of the field that you've set up to display the count and adjust the max variable to suit your needs. You'd call it like this from the Keystroke event:

           

          showCharCount();
          
          1 person found this helpful
          • 2. Re: Character counting (count down) in Acrobat
            George_Johnson MVP & Adobe Community Professional

            On re-reading your post this doesn't do quite what you're asking for. It will show the total character count, not the count for a particular line. Doing that will be very difficult, though it would be slightly more manageable if you use a fixed width font (e.g., Courier), but you'd have to account for any carriage returns that have been entered.

            1 person found this helpful
            • 3. Re: Character counting (count down) in Acrobat
              b.whitham Level 1

              Thanks George, that's very helpful. That's at least a good start for us for now and is much simpler than other scripts we were piecing together. What you've written makes sense to me.

               

              I was assuming the count-per-line thing may be a bit more challenging. Guess I'll wait and see if anything else pops up on here. Otherwise I may be able to use your script and fake the line count for now with text fields for each line. Not ideal but may be a good solve for our version 1.0 of this.

              • 4. Re: Character counting (count down) in Acrobat
                maxwyss Level 4

                Is there a specific reason why you want to limit the numbers of characters? In order to make the used space predictable, you would need a fixed width font. Otherwise, well, it would get rather complicated (as stated).

                 

                You would have to somehow extract a character width table from the font, and then continuously count up the character widths as you type. However, that still would not give you a precise line length, as there are also kerning and tracking issues.

                 

                For very specific circumstances, that could work out. For general purpose use, you would better look at a plug-in based solution (because the API used for plug-ins has better access to font metrics etc.).

                 

                If you are just interested in an approximation (which actually is not thaaaat bad at all), you could evaluate your text, and use the run length of either a number character (if the number character widths are harmonized), or of the "n" character (which is a pretty good average). You can get an approximative run length of the character by creating a text field and then filling it with "n" characters, and count them; using the console, you can calculate the width of the field, and so you can get a value for the character width.

                 

                Knowing that, you can start analyzing your text, by counting and finding the line breaking characters closest to the "magic" line length. And that should give you an idea on the number of characters still available…

                 

                Hope this can help.

                 

                Max Wyss.

                • 5. Re: Character counting (count down) in Acrobat
                  b.whitham Level 1

                  Those are all great ideas Max and certainly stuff we can consider for building on this.

                   

                  The reason we need to block the characters after a certain count is due to a system this will be entered into. Our client will be copying and pasting the blocks of text into a technology platform that will then print the info. We were given a specific set of instructions for how many characters and lines the printed piece could hold.

                   

                  The fun part is that before this goes into production it has to first be presented several times in different presentation decks (some integrated into Powerpoint, others just PDFs). We've tried similar exercises in the past with Word and other programs with not a lot of satisfaction from our client. So, no pun intended, but Acrobat offered the most portable file format to accomplish our needs.

                   

                  Though the original code posted above doesn't exactly solve our problem it's a great start for us and gives us somethign to build off of.

                   

                  This is a simple one I think. What's the command to add text to a field? So if I wanted to add the character "\n" once the text was full. I guess that would be something like (just a note, not real code)…

                  var hardreturn = \n;
                  if (max == 25) {
                  addText.value = hardreturn;
                  }
                  

                   

                  (sorry a million times for my horrible programming skills)

                   

                  Thanks for everyone's input thus far. This is a great and informative discussion.

                   

                  Message was edited by: b.whitham

                  • 6. Re: Character counting (count down) in Acrobat
                    maxwyss Level 4

                    OK, I see the reason. For the following, I assume that a line of text can not be longer than a given number of characters (let's call that N).

                     

                    In fact, there is not even any need for limiting the numbers of characters in a field; it is a field property (either set via user interface, or using the charLimit Field Object property.

                     

                    You may also have a look at the code samples with the fieldFull Event Object property.

                     

                    Hope this can help.

                     

                    Max Wyss.

                    • 7. Re: Character counting (count down) in Acrobat
                      b.whitham Level 1

                      This is helpful, very. This has been a great discussion. The fieldFull event is somethign we had incorporated. Our fix for the line limitations is going to be just to make each line it's own text field and we've adapted George's counter script to read each of those boxes so our writers know what the limitations are. When they hit the end of line (max char) we're having it jump to the next box via focusing and the fieldFull event. Just trying to figure out now how to force a hard return character (\n) at the end of each line when it's reached it's limit. This is for when our client copies and pastes this it'll hold the formatting. Having a little trouble figuring out what the command is to add text to the field though. I thought it was

                       

                      if (fieldFull) {
                      MyTextField.value = "\n";
                      }
                      

                       

                      but that doesn't seem to be working. Is there something for "field empty"? I suppose it's not that simple. Would it be fieldFull == null  ?

                       

                      Can't thank you guys enough. This has been really informative.

                      • 8. Re: Character counting (count down) in Acrobat
                        jay fresno Level 1

                        George,

                        The script you presented seems like it would work perfectly for my Acrobat form.

                        I have a text field with a Limit of xxx characters, and Multi-line.

                        Another text field is set to Read Only, and is the field where I need to display the number of characters remaining.

                        I put your script into Document JavaScripts, and for the second text field, for Format > Custom, I added the Custom Keystroke Script you suggested.

                        However, the character count does not appear in the second text field.

                        I'm using Acrobat XI Pro. Can you help me with what I'm doing wrong?

                        • 9. Re: Character counting (count down) in Acrobat
                          gkaiseril MVP & Adobe Community Professional

                          You might want to look at Countdown Maximum Length Input and the linked file.