30 Replies Latest reply on Feb 12, 2018 1:16 AM by try67

    Indicate if checkbox ticked for multiple fields using getNthFieldName

    Jo_2013 Level 1

      I have created an Action script to place an X in text fields on page 1 if a corresponding check box is ticked which is located on  other pages of the document.

       

      The pdf  file has multiple text fields on page 1 with the name of the text field beginning with C1.

      In the rest of the document there are multiple check boxes with the name of the check box beginning with C1.

       

      There are the same number of text fields and check boxes.

       

      If the first check box  is ticked, then the first text box on page 1 should then have an X in the field.

      If the second check box is ticked, then the second text box on page 1 should then have an X in the field etc.

       

      I am  unsure how to capture the check box as a variable into the text field, if anyone can provide assistance that will be most appreciated.

       

       

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

      var fname = this.getNthFieldName(i);

      var f = this.getField(fname);

      if (fname==null) continue;

      if ( f.type == "checkbox" && fname.substring(0,2) === "C1")  {

      console.println("checkbox " + this.getNthFieldName(i));

      }

      else if ( f.type == "textbox" && fname.substring(0,2) === "C1"){

      console.println("textbox " + this.getNthFieldName(i));

       

      var check = this.getField(??);

      var cbStatus = (check.isBoxChecked(0));

      if (cbStatus == 1) {

      event.value = "X";

      }

      if (cbStatus == 0) {

      event.value = "";

      }

      }

      }

        • 2. Re: Indicate if checkbox ticked for multiple fields using getNthFieldName
          Jo_2013 Level 1

          Thank you for your reply.  I will require further assistance please.

           

          I need to place the check box name into if type == textbox section of the script, can you please advise how to get the name of the checkbox as a variant? This is what I have been unable to work out.   It is highlighted in blue.

          • 3. Re: Indicate if checkbox ticked for multiple fields using getNthFieldName
            try67 MVP & Adobe Community Professional

            The variable fname contains the field name in your code.

            • 4. Re: Indicate if checkbox ticked for multiple fields using getNthFieldName
              Bernd Alheit Adobe Community Professional & MVP

              At the first console.println you have the name of the checkbox.

              • 5. Re: Indicate if checkbox ticked for multiple fields using getNthFieldName
                Jo_2013 Level 1

                Please see revised script, the console reports back as undefined, and the text box name is not reporting in the console, can you please see if there are any errors in the script? Thank you.

                 

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

                var fname = this.getNthFieldName(i);

                var f = this.getField(fname);

                if (fname==null) continue;

                if ( f.type == "checkbox" && fname.substring(0,2) === "C1")  {

                var check = this.getNthFieldName(i));

                }

                else if ( f.type == "textbox" && fname.substring(0,2) === "C1"){

                console.println("textbox " + this.getNthFieldName(i));

                var cbStatus = (check.isBoxChecked(0));

                if (cbStatus == 1) {

                event.value = "X"; }

                if (cbStatus == 0) {

                event.value = "";

                }

                }

                }

                • 6. Re: Indicate if checkbox ticked for multiple fields using getNthFieldName
                  Jo_2013 Level 1

                  The variant check may need to be declared global, to work in the text if statement

                   

                  Can you please advise how I can achieve this?

                   

                  I tried global.check = this.getNthFieldName(i);

                   

                  The looping did not work after declaring global in this way.

                  • 7. Re: Indicate if checkbox ticked for multiple fields using getNthFieldName
                    Bernd Alheit Adobe Community Professional & MVP

                    Use something like this:

                     

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

                      var fname = this.getNthFieldName(i);

                      var f = this.getField(fname);

                      if (fname==null) continue;

                      if ( f.type == "checkbox" && fname.substring(0,2) === "C1")  {

                        console.println("checkbox " + this.getNthFieldName(i));

                        var cbStatus = (f.isBoxChecked(0));

                        if (cbStatus == 1) {

                          event.value = "X";

                        }

                        if (cbStatus == 0) {

                          event.value = "";

                        }

                      }

                      else if ( f.type == "textbox" && fname.substring(0,2) === "C1"){

                        console.println("textbox " + this.getNthFieldName(i));

                      }

                    }

                     

                    Where do you use this code?

                    1 person found this helpful
                    • 8. Re: Indicate if checkbox ticked for multiple fields using getNthFieldName
                      Test Screen Name Most Valuable Participant

                      A note: the console always reports the last value returned in the code you run from the console. Where there is no result the word “undefined“ appears. This is not any kind of error or warning message.

                       

                      A question: you said the check box names start C1 and the text field names thwart C1 too. So how do you go from one to the other? What are the actual names of the second check box and second text field? You CANNOT rely on getNthFieldName to return them in the order you want.

                      1 person found this helpful
                      • 9. Re: Indicate if checkbox ticked for multiple fields using getNthFieldName
                        Jo_2013 Level 1

                        Thanks for your continuing help Bernd. The code will be placed inside all text fields on page 1 as a custom calculation script.

                        The text fields are on page 1 only, the checkbox fields appear on pages 2 to 24. There are 23 text fields and 23 checkbox fields in this particular document I am using as an example.

                         

                        I need the script to loop through each text box from 1 to 23 and place the (set action check box part of the script) to the custom calculation script.

                         

                        Eg first text box will have script for first checkbox, second text box will have script incremented to second check box etc.

                         

                        I have made changes to the script and reference to the check box instead of text box, as per your example, still not working, have you any ideas how to increment though each check box in the set action and assign to each text box?

                         

                        I really appreciate your continued assistance, thank you.

                         

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

                        var fname = this.getNthFieldName(i);

                        var f = this.getField(fname);

                        if ( f.type == "text" && fname.substring(0,2) === "C1")  {

                        f.setAction("Calculate","for (var i = 0; i < this.numFields; i++) {  var fname = this.getNthFieldName(i);  var f = this.getField(fname);if ( f.type == 'checkbox' && fname.substring(0,2) === 'C1')  {var cbStatus = (f.isBoxChecked(0));if (cbStatus == 1){event.value = 'X';}if (cbStatus == 0){event.value = '';}}}"); } }

                        • 10. Re: Indicate if checkbox ticked for multiple fields using getNthFieldName
                          Bernd Alheit Adobe Community Professional & MVP

                          You test only for field names with "C1" at the begin.

                           

                          What field names do you use?

                          • 11. Re: Indicate if checkbox ticked for multiple fields using getNthFieldName
                            Jo_2013 Level 1

                            Field Names for check boxes are

                            C10 on page 2

                            C11 on page 3

                            C12 on page 4

                            C13 on page 5

                            etc up to C122 on page 24

                             

                            There are a total of 23 check boxes in the document.

                             

                            Thanks Bernd.

                            • 13. Re: Indicate if checkbox ticked for multiple fields using getNthFieldName
                              Jo_2013 Level 1

                              Field Names for Text Boxes are:

                              C1 Reviewed and acceptedA

                              C1 Reviewed and acceptedB

                              C1 Reviewed and acceptedC

                              C1 Reviewed and acceptedD etc

                              There are a total of 23 text fields on page 1

                              Thanks Bernd

                              • 14. Re: Indicate if checkbox ticked for multiple fields using getNthFieldName
                                Bernd Alheit Adobe Community Professional & MVP

                                Use this document function:

                                 

                                function set_value(fname)

                                {

                                  var f = this.getField(fname);

                                  if (fname!=null) {

                                    var cbStatus = (f.isBoxChecked(0));

                                    if (cbStatus == 1) {

                                      event.value = "X";

                                    } else {

                                      event.value = "";

                                    }

                                  }

                                }

                                 

                                As calculation of field "C1 Reviewed and acceptedA" use:

                                 

                                set_value("C10");

                                 

                                As calculation of field "C1 Reviewed and acceptedB" use:

                                 

                                set_value("C11");

                                 

                                and so on.

                                • 15. Re: Indicate if checkbox ticked for multiple fields using getNthFieldName
                                  Jo_2013 Level 1

                                  Thanks Bernd.

                                   

                                  I have added the document javascript.

                                   

                                  When I run the script to add the custom calculation script to each text field it puts the same check box field name in each text field

                                  eg all text fields are reporting the name as C10 (there is no incrementing)

                                   

                                  I need the script to increment through each check box field and place it in each text box in the custom calculation script without having to manually input as there are 23 text fields (this will vary for each document as some documents may have over 100 text fields).

                                   

                                  I have revised the script to have the check box field name entered by incrementing.  All text fields are all referring to the same check box field name of C19 so this has not worked.

                                   

                                  Can you please look at the second script and advise how I can get the custom calculation script have the name of each check box incremented for each text box.

                                   

                                  First script which gets the value of checkbox field C10 in all of the 23 text boxes

                                   

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

                                  var fname = this.getNthFieldName(i);

                                  var f = this.getField(fname);

                                  if ( f.type == "text" && fname.substring(0,2) === "C1")  {

                                  f.setAction("Calculate","set_value('C10');");

                                  }

                                  }

                                   

                                  Second modified script which increments through the check box fields in the custom calculation script but returns the value from the one check box named C19 in all of the 23 text boxes

                                   

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

                                  var fname = this.getNthFieldName(i);

                                  var f = this.getField(fname);

                                  if ( f.type == "text" && fname.substring(0,2) === "C1")  {

                                  f.setAction("Calculate","for (var i = 0; i < this.numFields; i++) {  var fname = this.getNthFieldName(i);  var f = this.getField(fname);if ( f.type == 'checkbox' && fname.substring(0,2) === 'C1')  {set_value(fname);}}");

                                  }

                                  }

                                   

                                  Your continued assistance is very much appreciated - thank you.

                                  • 16. Re: Indicate if checkbox ticked for multiple fields using getNthFieldName
                                    Bernd Alheit Adobe Community Professional & MVP

                                    This will not work because all the names begin with "C1".

                                    1 person found this helpful
                                    • 17. Re: Indicate if checkbox ticked for multiple fields using getNthFieldName
                                      Jo_2013 Level 1

                                      Hi Bernd,

                                       

                                      I have been working on the script for the past week, and have used a different approach.

                                       

                                      I am adding new field names over the existing field names, then removing the old fields.

                                       

                                      There are 23 fields in total, with the script naming every field "testing".

                                       

                                      I need for the script to name the fields in sequence order starting from 0 to 22.

                                       

                                      eg

                                       

                                      testing0

                                      testing1

                                      testing2

                                      testing3 etc

                                       

                                      Can please advise where I can modify the script to add the number sequence to the new field name?

                                       

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

                                      var fname = this.getNthFieldName(i);

                                      var f = this.getField(fname);

                                      if ( f.type == "text" && fname.substring(0,2) === "C1")  {

                                      var myRect = f.rect;

                                      var m = this.addField("testing", "text", this.pageNum, myRect);

                                      }

                                      }

                                      for (var i = this.numFields - 1; i >= 0; i--) {

                                      var fname = this.getNthFieldName(i);

                                      var f = this.getField(fname);

                                      if ( f.type == "text" && fname.substring(0,2) === "C1")  {

                                      this.removeField(fname);

                                      }

                                      }

                                      Thank you.

                                      • 19. Re: Indicate if checkbox ticked for multiple fields using getNthFieldName
                                        Jo_2013 Level 1

                                        Giving up is not in my vocabulary. I have modified the script and it is now adding the fields with numbers from 0 to 22.

                                         

                                        Now for the very last part I need help with, it is not adding the new fields in sequential order, it is adding the fields randomly as follows.

                                         

                                        testing00

                                        testing01

                                        testing12

                                        testing16

                                        testing17

                                        testing18

                                        testing19

                                        testing20

                                        testing21

                                        testing22

                                        testing02

                                        testing03

                                        testing04

                                        testing05

                                        testing07

                                        testing08 etc to testing 15

                                         

                                        Can you please advise how to program the script so that it adds the fields in a sequential order?

                                         

                                        Once this is rectified the script will be complete (finally)

                                         

                                        Thanks Bernd .

                                         

                                        var count = 0;

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

                                        var fname = this.getNthFieldName(i);

                                        if ( this.getField(fname).type == "text" && fname.substring(0,2) === "C1") count++; }

                                        console.println("There are " + count + " text fields.");

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

                                        var fname = this.getNthFieldName(i);

                                        var f = this.getField(fname);

                                        if ( f.type == "text" && fname.substring(0,2) === "C1")  {

                                        var n = (i - count);

                                        if (n < 10) {

                                        n = "0" + n;

                                        }

                                        else n = n;

                                        var myRect = f.rect;

                                        var m = this.addField("testing" + n, "text", this.pageNum, myRect);

                                        }

                                        }

                                        for (var i = this.numFields - 1; i >= 0; i--) {

                                        var fname = this.getNthFieldName(i);

                                        var f = this.getField(fname);

                                        if ( f.type == "text" && fname.substring(0,2) === "C1")  {

                                        this.removeField(fname);

                                        }

                                        }

                                        • 20. Re: Indicate if checkbox ticked for multiple fields using getNthFieldName
                                          try67 MVP & Adobe Community Professional

                                          What difference does it make what the order is? 1+2+3 is the same as 3+1+2...

                                          • 21. Re: Indicate if checkbox ticked for multiple fields using getNthFieldName
                                            Jo_2013 Level 1

                                            I will be adding a set action to the new form fields. (this will be added to the last script posted).

                                             

                                            The set action will extract the number from the active text field to indicate if a check box is ticked.

                                             

                                            It is imperative that the new text boxes are in a sequential order, otherwise it will indicate when the incorrect check box is ticked.

                                             

                                            eg. page 1 has all the text boxes, pages 2 to 24 have the check boxes.

                                             

                                            testing0 text box will indicate an X in the form field when the corresponding check box C10 is ticked.

                                             

                                            testing1 text box will indicate an X in the form field when the corresponding check box C11 is ticked etc.

                                             

                                            On page 1

                                             

                                            Drawing X1233          Text form field - testing0      (Drawing X1233 is on page 2 with check box C10)

                                            Drawing X1234          Text form field - testing1      (Drawing X1234 is on page 3 with check box C11)  

                                            Drawing X1235          Text form field - testing2      (Drawing X1235 is on page 4 with check box C12)

                                            etc

                                             

                                            var a = event.target.name;

                                            var thenum = a.replace( /^\D+/g, '');  extracting the digits from the active text field ,text box is called testing0

                                            var g = this.getField("C1" + thenum); getting the check box value, the check box is called C10

                                            var cbStatus = (g.isBoxChecked(0));

                                            if (cbStatus == 1) {

                                            event.value = "X";

                                            }

                                            if (cbStatus == 0) {

                                            event.value = "";

                                            }

                                            console.println(g);

                                             

                                            If you can help me revise the script so that the text boxes are added in sequential order, this will have script in working order for use. ( I will be most grateful)

                                             

                                            As currently the end user has to manually enter X on page 1 for each check box ticked on the corresponding pages.  This can be quite time consuming as in some cases there can be 100 pages in a document.  This particular case has 23 pages.

                                            • 22. Re: Indicate if checkbox ticked for multiple fields using getNthFieldName
                                              try67 MVP & Adobe Community Professional

                                              I'm not really following the logic of what you're doing, but why are you iterating over all of the fields in the document if you know what the field names are, and they are consistent?

                                              Just use a loop from 1 to 22 and add up all the "testingXX" fields, if that's what you'd like to do.

                                              • 23. Re: Indicate if checkbox ticked for multiple fields using getNthFieldName
                                                Jo_2013 Level 1

                                                Thanks for your reply.

                                                Can you please modify the script to show how to add the loop from 1 to 22 .

                                                I have been playing around with the looping, but it is not working, it now numbers all the text boxes with the same number.

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

                                                var fname = this.getNthFieldName(i);

                                                var f = this.getField(fname);

                                                if ( f.type == "text" && fname.substring(0,2) === "C1")  {

                                                for (var j = 1; j < 22; j++) { this looping not working gives all of the new text boxes the same name of testing21

                                                var myRect = f.rect; var m = this.addField("testing" + j, "text", this.pageNum, myRect);

                                                }

                                                }

                                                }

                                                • 24. Re: Indicate if checkbox ticked for multiple fields using getNthFieldName
                                                  try67 MVP & Adobe Community Professional

                                                  Here you go:

                                                   

                                                  var total = 0;

                                                  for (var i=1; i<=22; i++) {

                                                      if (i<10) total+=Number(this.getField("testing0"+i).valueAsString);

                                                      else total+=Number(this.getField("testing"+i).valueAsString);

                                                  }

                                                  event.value = total;

                                                  • 25. Re: Indicate if checkbox ticked for multiple fields using getNthFieldName
                                                    Jo_2013 Level 1

                                                    Thank you for your help.

                                                     

                                                     

                                                    I need help with entering in additional new form fields to the pdf, and naming them with a sequential number to the form field name.

                                                     

                                                    There is no need to sum up the values, just entering new form fields and have them show sequentially on the pdf.  (sorry if there is any mis communication).

                                                     

                                                    By using the loop for (var i=1; i<=22; i++) the names of the text form fields all end up with the same number.

                                                     

                                                     

                                                    I need the new form fields to be named sequentially. Hope this makes sense.

                                                     

                                                    Can you please revise the looping so that it names all of the new text fields in a sequential order.

                                                     

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

                                                    var fname = this.getNthFieldName(i);

                                                    var f = this.getField(fname);

                                                    if ( f.type == "text" && fname.substring(0,2) === "C1")  {

                                                    for (var j = 1; j <= 22; ) { still creating new text fields named with the same number eg testing22 is named on all text boxes

                                                    var myRect = f.rect;

                                                    var m = this.addField("testing" + j, "text", this.pageNum, myRect);

                                                    }

                                                    }

                                                    }

                                                    • 26. Re: Indicate if checkbox ticked for multiple fields using getNthFieldName
                                                      try67 MVP & Adobe Community Professional

                                                      Your code will do that, but you're missing a critical part of the loop.

                                                      Change this:

                                                      for (var j = 1; j <= 22; )

                                                      To:

                                                      for (var j = 1; j <= 22; j++)

                                                      • 27. Re: Indicate if checkbox ticked for multiple fields using getNthFieldName
                                                        Jo_2013 Level 1

                                                        Thank you, we are getting so close.

                                                         

                                                        It will now add new text fields named with numbers from 1 to 22, except it repeats the process 22 times and there are 484 text boxes.

                                                         

                                                         

                                                        Can you please examine the code so it adds the 22 numbered text boxes once only?

                                                         

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

                                                        var fname = this.getNthFieldName(i);

                                                        var f = this.getField(fname);

                                                        if ( f.type == "text" && fname.substring(0,2) === "C1")  {

                                                        for (var j = 1; j <= 22; j++ ) {

                                                        var myRect = f.rect; var m = this.addField("testing" + j, "text", this.pageNum, myRect);

                                                        }

                                                        }

                                                        }

                                                        • 28. Re: Indicate if checkbox ticked for multiple fields using getNthFieldName
                                                          try67 MVP & Adobe Community Professional

                                                          This seems a more complex task. You want to add these fields based on the location of some "C1" fields?

                                                          You need to first describe what you want to achieve, exactly, and then try and write the code for it, not the other way around.

                                                          • 29. Re: Indicate if checkbox ticked for multiple fields using getNthFieldName
                                                            Jo_2013 Level 1

                                                            Hi, below is a description of what is required in a 24 page document.

                                                            On page 1 lists all the drawings (with new text form fields beside each drawing ).

                                                            On page 2 to 24 are all the drawings with a check box assigned on each page.

                                                            When a check box is ticked on the drawing page  then the corresponding text box on page 1 shows an X as a value in the field.

                                                            Eg there is a drawing listed on page 1 as ABC123, the actual drawing is on page 2, the check box is called C10.

                                                            The text form field on page 1 is called testingC10.

                                                            The set action of the form field will extract the digits from the active field name to find the name of the check box eg C10.

                                                            The script is actually working, except it does not place the numbered text form fields in the correct order. Eg testing01 then the next form field is testing12, instead it should be testing01, then testing02 etc. The text form fields need to be sorted into the correct ascending order, otherwise the drawing on page 1 will have an incorrect X marked in the form field. Below is the full script, if you can possibly advise how to add the text form fields in the correct ascending numerical sequence, this will be most helpful as the script will then be fully functional.

                                                             

                                                            var count = 0;

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

                                                            var fname = this.getNthFieldName(i);

                                                            if ( this.getField(fname).type == "text" && fname.substring(0,2) === "C1") count++; } count the number of fields beginning with C1

                                                            console.println("There are " + count + " text fields.");

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

                                                            var fname = this.getNthFieldName(i);

                                                            var f = this.getField(fname);

                                                            if ( f.type == "text" && fname.substring(0,2) === "C1")  {

                                                            console.println("C1" + (i - count)); this gives the correct number to assign to the new text form field eg 0,1,2,3,4 etc

                                                            var myRect = f.rect;

                                                            var m = this.addField("testing" + (i - count), "text", this.pageNum, myRect); adds 23 text form fields on page 1

                                                            m.setAction("Calculate"," var a = event.target.name; var thenum = a.replace(/^D+/g,var g = this.getField('C1'+thenum); var cbStatus = (g.isBoxChecked(0)); if (cbStatus ==1){event.value = 'X';} if(cbStatus == 0){ event.vaue = '';} "'); this adds the custom calculation script to the form field,

                                                            }

                                                            }

                                                            for (var i = this.numFields - 1; i >= 0; i--) {  this removes the old text form fields

                                                            var fname = this.getNthFieldName(i);

                                                            var f = this.getField(fname);

                                                            if ( f.type == "text" && fname.substring(0,2) === "C1")  {

                                                            this.removeField(fname);

                                                            }

                                                            }

                                                             

                                                            Look forward to your reply thank you.

                                                            • 30. Re: Indicate if checkbox ticked for multiple fields using getNthFieldName
                                                              try67 MVP & Adobe Community Professional

                                                              If you want it to be done in a specific order collect the fields names into an array, sort it and then process it.

                                                              If you want further help with this you can contact me privately.