11 Replies Latest reply on Jan 11, 2012 3:02 AM by JR_Boulay

    problem formatting multiple field types

    T_Stevens

      This one has me stumped. I'm creating an action that will, when working, generate form fields and format them to my company's standards. Because I'm using Acrobat's "detect form fields" feature, I have no control over the names or types of the fields, and that's fine since 90% of the time Acrobat creates what I want anyway.

      The javascript I'm using to format the fields is as follows:

       

      var errorArray = new Array();

      for(var i = 0; i < this.numFields; i++)

      {

      var fname = this.getNthFieldName(i);

      var f = this.getField(fname);

      if(f.type != "checkbox"){

      try{

      f.textSize = 9;

      f.doNotScroll = true;

      f.userName = "";

      f.value = "Test"; //This line is to confirm formatting

      }

      catch(e){

      errorArray[errorArray.length] = this.getNthFieldName(i);

      }

      }

      else{

      f.style = style.cr;

      }

      }

      app.alert("The following fields could not be formatted: " + errorArray);

       

      When I try and run this I end up with two main problems. The first is that every time I test it I get at least one text field that isn't formatted. Not only that, but if there are multiple checkboxes in my form the action only runs to the second checkbox, so none of the fields after that checkbox are formatted and I don't get the alert. When I try the script without checking for field type like this:

       

      var errorArray = new Array();

       

      for(var i = 0; i < this.numFields; i++)

      {

      var fname = this.getNthFieldName(i);

      var f = this.getField(fname);

      try{

      f.textSize = 9;

      f.doNotScroll = true;

      f.userName = "";

      }

      catch(e){

      errorArray[errorArray.length] = this.getNthFieldName(i);

      }

      }

      app.alert("The following fields could not be formatted: " + errorArray);

       

      The action runs all the way through, but I still don't get ALL of my text fields formatted and I can't do anything with my checkboxes. Any ideas on what I'm doing wrong?

        • 1. Re: problem formatting multiple field types
          try67 MVP & Adobe Community Professional

          What you're doing wrong is you're not checking the field type in a correct way. Some

          properties apply to fields of all types, but others only exist to fields of

          a certain type. For example, textSize is universal, but doNotScroll can

          only be used on fields of type "text".

          So don't just check if a field is not a check-box, check if it is a text field...

          • 2. Re: problem formatting multiple field types
            T_Stevens Level 1

            Thanks for responding try67. I understand that I need to test for field type, but everytime I do the script doesn't work. I've tried the following:

             

            var errorArray = new Array();

             

            for(var i = 0; i < this.numFields; i++)

            {

            var fname = this.getNthFieldName(i);

            var f = this.getField(fname);

            if(f.type == "text"){

            try{

            f.textSize = 9;

            f.doNotScroll = true;

            f.userName = "";

            f.value = "Test";}

            catch(e){

            errorArray[errorArray.length] = this.getNthFieldName(i);}

            }

            else if(f.type == "checkbox"){

            f.style = style.cr;}

            }

            app.alert("The following fields could not be formatted: " + errorArray);

             

            And I have the same problem. The script stops once it hits a checkbox. Anything after the first checkbox isn't formatted and I don't get my alert. The most success I've had is not checking for field type at all. When I don't check for any field type then the text fields format correctly and I get the alert listing the checkboxes.

            • 3. Re: problem formatting multiple field types
              try67 MVP & Adobe Community Professional

              Open the JS console and check what error messages are printed there.

              • 4. Re: problem formatting multiple field types
                T_Stevens Level 1

                The errors I'm getting are:

                f is null

                8:Batch:Exec

                f is null

                15:Batch:Exec

                 

                If I don't test the type of field I'm a lot closer. I've had the most success with the following, but it still doesn't catch all the checkboxes.

                 

                var errorArray = new Array();

                 

                for(var i = 0; i < this.numFields; i++)

                {

                var fname = this.getNthFieldName(i);

                var f = this.getField(fname);

                try{

                f.textSize = 9;

                f.doNotScroll = true;

                f.userName = "";

                f.value = "Test";

                }

                catch(e){

                try{

                f.style = style.cr;

                }

                catch(e){

                errorArray[errorArray.length] = fname;

                }

                }

                }

                app.alert("The following fields could not be formatted: " + errorArray);

                • 5. Re: problem formatting multiple field types
                  try67 MVP & Adobe Community Professional

                  If you wish, send me the file by email to try6767 at gmail dot com and I'll

                  have a look at what's going wrong.

                  1 person found this helpful
                  • 6. Re: problem formatting multiple field types
                    T_Stevens Level 1

                    Thanks try67 for finding that bug. I now have the following script (which is hopefully a little better written than my previous attempts):

                     

                    var errorArray = new Array();

                     

                    for(var i = 0; i < this.numFields; i++)

                    {

                    var fname = this.getNthFieldName(i);

                    var f = this.getField(fname);

                    if(f == null){

                    errorArray[errorArray.length] = fname;

                    continue;

                    }

                    if(f.type == "text"){

                    try{

                    f.userName = "";

                    f.doNotScroll = true;

                    f.textSize = 9;

                    f.value = "Test";

                    }

                    catch(e){

                    errorArray[errorArray.length] = fname;

                    }

                    }

                    else if(f.type == "checkbox"){

                    try{

                    f.userName = "";

                    f.textSize = 9;

                    f.style = style.cr;

                    var myRect = f.rect;

                    myRect[0] -= 2;

                    myRect[1] += 2;

                    myRect[2] += 2;

                    myRect[3] -= 2;

                    f.rect = myRect;

                    }

                    catch(e){

                    errorArray[errorArray.length] = fname;

                    }

                    }

                    }

                    //app.alert("The following fields could not be formatted: " + errorArray);

                     

                    It no longer crashes when it hits a null field, but it still won't format the all of the checkboxes and I can live with that. (It's the reason I'm keeping the alert in the code, even if it's commented.) Unfortunately, I do have one other problem that I can't figure out with the checkboxes. My script is successfully changing the checkbox style, textSize, and resizing the field to the dimensions that I want. However, when I test the action the check-mark itself is only the correct size if I highlight the existing form fields. If I manually go in and change the font size in the field properties, the problem goes away. I'm assuming this is a bug in Acrobat, but if anyone knows differently I would appreciate the assistance.

                    • 7. Re: problem formatting multiple field types
                      try67 MVP & Adobe Community Professional

                      You're welcome. Just so others might also understand what went wrong, it

                      seems that the form fields detection wizard acts a bit buggy and produces

                      "ghost" fields with a blank field name which do not actually exist.

                      I would recommend that you get rid of the try-catch clauses, so that you

                      could see in the console what actually went wrong.

                      Also, if you want to add values to an array you should use the push()

                      method.

                       

                      I didn't really follow that last bit. What exactly is the issue?

                      • 8. Re: problem formatting multiple field types
                        T_Stevens Level 1

                        Thanks for the writing tips. I'm still fairly new to scripting and still don't have the best coding style.

                         

                        The problem that I'm having with the checkboxes is after I've run the action. I go to the form and start typing in text fields and checking boxes to make sure that I didn't miss any formatting issues that I should have included in the script. I noticed that on the checkboxes, even though the field has been resized and the font size changed (which should increase the size of the 'x' mark), the actual x is still small. I've included a screenshot below. You can see in the image that the top box is the correct size, while the lower box (which is selected) is not. If I turn off the "highlight existing fields" feature then all of the check marks are the smaller size. Manually changing the font size seems to fix the issue. I'm wondering if this is just a bug in Acrobat, or is there a problem with my code?

                        checkboxes.png

                        • 9. Re: problem formatting multiple field types
                          try67 MVP & Adobe Community Professional

                          But does the font size has the correct value (even if the X is not in the

                          correct size)? If so, it might just be an issue of unchecking/re-checking

                          the box after having changed its position and fontSize.

                          • 10. Re: problem formatting multiple field types
                            T_Stevens Level 1

                            The font size is correct, and I've tried checking, unchecking, and re-checking the box. If I don't use the 'highlight' option, the check marks are always small. If I do use the option then the mark is small until I leave the field. I've even checked the boxes, saved the file, quit Acrobat, reopened the file, and the check marks are still small unless the 'highlight' option is on.

                            When I go into the field properties and check the font size (which is set at 9 in my script) it is correct. If I change that font size to anything else, then immediately change it back to 9, the problem goes away and the check mark is always the larger size whether or not I use the 'highlight' option.

                            I know it's not a big problem... but it is really annoying.

                            • 11. Re: problem formatting multiple field types
                              JR_Boulay Adobe Community Professional

                              This is not a direct answer to the question, but since you're using Acrobat's "detect form fields" feature you may need to use the "Tooltips remover" Action too, freely avalaible here:

                              http://acrobatusers.com/actions-exchange

                               

                              ;-)