21 Replies Latest reply on Mar 22, 2010 6:50 AM by Norb11

    Combobox, how add items from array ?

    Norb11

      Hi,

      I have two  arrays   with items  (names and age),  and I want  add items from "names"  to   combo box using for  loop and add age to  input text using if statement   for exemple :

       

       

      if(names[i] == .x)

           mytextBox =age[i];

       

      I  added "combobox"   to my pdf and set  name "mycombo", and now I want add all item

      How I can do this using Java Script on Acrobat ?

       

       

      var i=0;

      for (i=0;i<names.length;i++){

           mycombo.setItem =names[i];

      }

        • 1. Re: Combobox, how add items from array ?
          try67 MVP & Adobe Community Professional

          Problem 1: There's no such method as setItem, only setItems. Another option

          is insertItemAt.

          Problem 2: You can't use a method with the assignment operator '='. You must

          place the value you pass to it in parentheses.

          I suggest you go back to the reference files and have another look at these

          methods. There are examples there.

          • 2. Re: Combobox, how add items from array ?
            Norb11 Level 1

            Thank for reply,

            Can You  give me link to reference  and tell  me with chapter I must read?

            Im new acrobat user and  I don't have any experience with JS and adobe.

            • 3. Re: Combobox, how add items from array ?
              try67 MVP & Adobe Community Professional

              The reference is here:

              http://livedocs.adobe.com/acrobat_sdk/9.1/Acrobat9_1_HTMLHelp/JavaScript_SectionPage.70.1. html

              However, if you have no experience at all with JS expect a steep learning

              curve.

              • 4. Re: Combobox, how add items from array ?
                Norb11 Level 1

                Thank for link.

                I have some experience with JS, but  no  with adobe acrobat.

                 

                 

                 

                OK, I try   to do   some like this:

                 

                var starter = this.getField("starter");
                starter.editable = true;

                 

                var j=0;
                    for(j=0; j<name.length;j++){
                        starter.insertItemAt(name[j],j);   
                    }

                 

                And after save and restart acrobat, when I click on  combobox, I don't have any exception,  only windows event  alert sound, and my combobox list is clear.

                 

                 

                 

                When I choice "Debug from start"   from menu,

                i have this alert after clikc on combobox

                 

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

                 

                 

                Is this code is correct?

                 

                I read  about this method on JS API  reference manula page:  458

                • 5. Re: Combobox, how add items from array ?
                  try67 MVP & Adobe Community Professional

                  Where's the definition of the variable called "name"?

                  • 6. Re: Combobox, how add items from array ?
                    Norb11 Level 1

                    This is my code:

                     

                    //-------------------------------------------------------------
                    //-----------------Do not edit the XML tags--------------------
                    //-------------------------------------------------------------

                     

                    //<AcroForm>
                    //<ACRO_source>Starter:Annot1:MouseUp:Action1</ACRO_source>
                    //<ACRO_script>
                    /*********** belongs to: AcroForm:Starter:Annot1:MouseUp:Action1 ***********/

                    var temp =trustedDataGet();//get array
                    var tmp;

                     

                    var name = new Object();
                    var date = new Object();
                    var nr = new Object();
                    var  i=0;
                    var starter = this.getField("starter");
                    starter.editable = true;
                    var j=0;

                     

                    for(i =0;i<temp.length;i++){
                        tmp =temp[i].split(";");
                        name[i] =tmp[0]+" "+tmp[1];
                        date[i]=tmp[2];
                        nr[i]=tmp[3];
                    }

                     

                    for(j=0; j<name.length;j++){
                    //    starter.insertItemAt(name[j],j);
                        console.println(name[j]);    //it doesn't work, I can't see it when I try  debug this code
                    }
                    //</ACRO_script>
                    //</AcroForm>

                    • 7. Re: Combobox, how add items from array ?
                      try67 MVP & Adobe Community Professional

                      Where's "trustedDataGet" defined?

                      • 8. Re: Combobox, how add items from array ?
                        Norb11 Level 1

                        trustedDataGet  this is  function from "C:/Prog...Files/acr..... /Javascript/config.js",

                        this is function to get data from file,

                         


                        My script from  previous post  work corrently to place when I try  add data to combobox. (line).

                        Problem is on  last loop.

                        But I  don't know when and why

                        • 9. Re: Combobox, how add items from array ?
                          try67 MVP & Adobe Community Professional

                          Try defining name as an Array (instead of Object) and using the push()

                          method to add new items to it.

                          1 person found this helpful
                          • 10. Re: Combobox, how add items from array ?
                            Norb11 Level 1

                            It works


                            This is  working code:

                             

                             

                            var name = [];// new Object();
                            var date = [];//new Object();
                            var nr = [];// new Object();
                            var  i=0;
                            var starter = this.getField("starter");
                            starter.editable = true;
                            var j=0;

                             

                            for(i =0;i<temp.length;i++){
                                tmp =temp[i].split(";");
                                name.push(tmp[1]+" "+tmp[0]);
                                date.push(tmp[2]);
                                nr.push(tmp[3]);
                            }

                             

                            for(j=0; j<name.length;j++){
                                starter.insertItemAt(name[j],j);
                               // console.println(name[j]);   
                            }

                             

                            I have next question, how automatical clear combobox after exit ( close pdf file) ?

                            for now, im add on start  :

                            this.getField("starter").clearItems();

                             

                            And my last  question is,   how get the combobox numerical value

                                starter.insertItemAt(name[j],j); //  how get  "j"  value ?

                            • 11. Re: Combobox, how add items from array ?
                              try67 MVP & Adobe Community Professional

                              I'm not sure what you mean. You can't change the file after it's closed.

                              You can either do it just before it closes (using the "WillClose" event), or

                              when it opens.

                              To clear a combo-box use the clearItems() method.

                              • 13. Re: Combobox, how add items from array ?
                                Norb11 Level 1

                                I  modify

                                my script,  im  add a part responsibility for load combobox items  to on open event,  and  next  when I click on combobox, I try  add some text to textbox,

                                 

                                this is my  code:


                                var name = [];// new Object();
                                var date = [];//new  Object();
                                var nr = [];// new Object();
                                var  i=0;
                                var starter =  this.getField("starter");
                                starter.editable = true;
                                //this.getField("starter").clearItems();
                                var  j=0;

                                 

                                for(i  =0;i<temp.length;i++){
                                    tmp =temp[i].split(";");
                                     name.push(tmp[1]+" "+tmp[0]);
                                    date.push(tmp[2]);
                                     nr.push(tmp[3]);
                                }

                                 

                                for(j=0; j<name.length;j++){
                                     starter.insertItemAt(name[j],j);
                                    console.println(name[j]);   
                                }

                                 

                                //</ACRO_script>
                                //</Page-Actions>

                                 

                                //<Page-Actions>
                                //<ACRO_source>Page1:Page  Close:Action1</ACRO_source>
                                //<ACRO_script>
                                /***********  belongs to: Page-Actions:Page1:Page Close:Action1 ***********/

                                 

                                this.getField("starter").clearItems();

                                 


                                //</ACRO_script>
                                //</Page-Actions>

                                 

                                //<AcroForm>
                                //<ACRO_source>starter:Annot1:MouseUp:Action1</ACRO_source>
                                //<ACRO_script>
                                /***********  belongs to: AcroForm:starter:Annot1:MouseUp:Action1 ***********/
                                //-------------------------------------------------------------
                                //-----------------Do  not edit the XML tags--------------------
                                //-------------------------------------------------------------
                                //var  starter = this.getField("starter");

                                 

                                var dl = this.getField("Text10");
                                var  brith = this.getField("Text9");
                                brith.value=date[starter.index];
                                dl.value  =nr[starter.index];

                                 

                                 

                                On  last part, I  try  to add some value  to  textbox forms

                                but  I  can't see  any string from  arrays ("date", and "nr" ) on pdf document, I  don't have any error or warning ,   but  I see only "undefined"

                                 

                                Why? Or when I can see full combobox property list ?

                                which property have a value of current selected item ?

                                • 14. Re: Combobox, how add items from array ?
                                  try67 MVP & Adobe Community Professional

                                  You didn't define date or nr in the last script.

                                  • 15. Re: Combobox, how add items from array ?
                                    Norb11 Level 1

                                    No,

                                    I define this arrays  on script ( create this array in event , when  open document)


                                    all code:

                                    //-------------------------------------------------------------
                                    //-----------------Do not edit the XML tags--------------------
                                    //-------------------------------------------------------------

                                    //<Page-Actions>

                                     

                                    //<ACRO_source>Page1:Page Open:Action1</ACRO_source>
                                    //<ACRO_script>
                                    /*********** belongs to: Page-Actions:Page1:Page Open:Action1 ***********/
                                    var temp =trustedDataGet();//get array var tmp;
                                    var tmp;

                                     

                                    var name = [];// new Object();
                                    var date = [];//new Object();
                                    var nr = [];// new Object();
                                    var  i=0;
                                    var starter = this.getField("starter");
                                    starter.editable = true;
                                    //this.getField("starter").clearItems();
                                    var j=0;

                                     

                                    for(i =0;i<temp.length;i++){
                                        tmp =temp[i].split(";");
                                        name.push(tmp[1]+" "+tmp[0]);
                                        date.push(tmp[2]);
                                        nr.push(tmp[3]);
                                    }

                                     

                                    for(j=0; j<name.length;j++){
                                        starter.insertItemAt(name[j],j);
                                        console.println(name[j]);   
                                    }

                                     

                                    //</ACRO_script>
                                    //</Page-Actions>

                                     

                                    //<Page-Actions>
                                    //<ACRO_source>Page1:Page Close:Action1</ACRO_source>
                                    //<ACRO_script>
                                    /*********** belongs to: Page-Actions:Page1:Page Close:Action1 ***********/

                                     

                                    this.getField("starter").clearItems();

                                     


                                    //</ACRO_script>
                                    //</Page-Actions>

                                     

                                    //<AcroForm>
                                    //<ACRO_source>starter:Annot1:MouseUp:Action1</ACRO_source>
                                    //<ACRO_script>
                                    /*********** belongs to: AcroForm:starter:Annot1:MouseUp:Action1 ***********/
                                    //-------------------------------------------------------------
                                    //-----------------Do not edit the XML tags--------------------
                                    //-------------------------------------------------------------
                                    //var starter = this.getField("starter");

                                     

                                    var dl = this.getField("Text10");
                                    var brith = this.getField("Text9");

                                     

                                     

                                     

                                    brith.value=date[starter.selectedIndex];
                                    dl.value =nr[starter.selectedIndex];

                                     


                                    //<AcroForm>
                                    //<ACRO_source>Starter:Annot1:MouseUp:Action1</ACRO_source>
                                    //<ACRO_script>
                                    /*********** belongs to: AcroForm:Starter:Annot1:MouseUp:Action1 ***********/

                                     

                                     

                                     

                                    //</ACRO_script>
                                    //</AcroForm>

                                     

                                     

                                    All code is in this same file, so I think all variable is  global var.  ?

                                    • 16. Re: Combobox, how add items from array ?
                                      Norb11 Level 1

                                      No,

                                      I define this arrays  on script ( create this array in  event , when  open document)


                                      all code:

                                      //-------------------------------------------------------------
                                      //-----------------Do not edit the XML tags--------------------
                                      //-------------------------------------------------------------

                                      //<Page-Actions>

                                       

                                      //<ACRO_source>Page1:Page Open:Action1</ACRO_source>
                                      //<ACRO_script>
                                      /*********** belongs to: Page-Actions:Page1:Page Open:Action1  ***********/
                                      var temp =trustedDataGet();//get array var tmp;
                                      var tmp;

                                       

                                      var name = [];// new Object();
                                      var date = [];//new Object();
                                      var nr = [];// new Object();
                                      var  i=0;
                                      var starter = this.getField("starter");
                                      starter.editable = true;
                                      //this.getField("starter").clearItems();
                                      var j=0;

                                       

                                      for(i =0;i<temp.length;i++){
                                           tmp =temp[i].split(";");
                                           name.push(tmp[1]+" "+tmp[0]);
                                           date.push(tmp[2]);
                                           nr.push(tmp[3]);
                                      }

                                       

                                      for(j=0; j<name.length;j++){
                                           starter.insertItemAt(name[j],j);
                                           console.println(name[j]);   
                                      }

                                       

                                      //</ACRO_script>
                                      //</Page-Actions>

                                       

                                      //<Page-Actions>
                                      //<ACRO_source>Page1:Page Close:Action1</ACRO_source>
                                      //<ACRO_script>
                                      /*********** belongs to: Page-Actions:Page1:Page Close:Action1  ***********/

                                       

                                      this.getField("starter").clearItems();

                                       


                                      //</ACRO_script>
                                      //</Page-Actions>

                                       

                                      //<AcroForm>
                                      //<ACRO_source>starter:Annot1:MouseUp:Action1</ACRO_source>
                                      //<ACRO_script>
                                      /*********** belongs to: AcroForm:starter:Annot1:MouseUp:Action1  ***********/
                                      //-------------------------------------------------------------
                                      //-----------------Do not edit the XML tags--------------------
                                      //-------------------------------------------------------------
                                      //var starter = this.getField("starter");

                                       

                                      var dl = this.getField("Text10");
                                      var brith = this.getField("Text9");

                                       

                                       

                                       

                                      brith.value=date[starter.selectedIndex];
                                      dl.value =nr[starter.selectedIndex];

                                       


                                      //<AcroForm>
                                      //<ACRO_source>Starter:Annot1:MouseUp:Action1</ACRO_source>
                                      //<ACRO_script>
                                      /*********** belongs to: AcroForm:Starter:Annot1:MouseUp:Action1  ***********/

                                       

                                       

                                       

                                      //</ACRO_script>
                                      //</AcroForm>

                                       

                                       

                                      All code is in this same file, so I think all variable is  global  var.

                                      • 17. Re: Combobox, how add items from array ?
                                        try67 MVP & Adobe Community Professional

                                        No variable is global unless you define it as one. Look at the global object

                                        in the reference files.

                                        • 18. Re: Combobox, how add items from array ?
                                          Norb11 Level 1

                                          If this arrays isn't global, why  i can do this when I click on combobox?

                                           

                                          console.println(date[10]);

                                           

                                          this print string.

                                           

                                           

                                          But OK,   for now, I move  my code from event  (open file)  to  event click on combobox. (leater I move this part of code to  open file event)

                                           

                                          And now when I try do this:

                                           

                                          var driveLicense = this.getField("Text10");
                                          var brith = this.getField("Text9");
                                          console.println(starter.selectedIndex);
                                          brith.value=date[starter.selectedIndex];
                                          driveLicense.value =nr[starter.selectedIndex];

                                           

                                          i see "undefined"  on textbox 

                                           

                                           

                                          starter.selectedIndex - is  OK?

                                          Maybe  I try  use wrong property?

                                          I use notepad++ as a editor,   when I choice ctrl+space keys  I can see long list  , but i don't know whitch property is OK

                                          • 19. Re: Combobox, how add items from array ?
                                            try67 MVP & Adobe Community Professional

                                            There's no such thing as selectedIndex. You need to use the reference files.

                                            Notepad++ doesn't have the special Acrobat objects built-in.

                                            1 person found this helpful
                                            • 20. Re: Combobox, how add items from array ?
                                              Norb11 Level 1

                                              >Notepad++ doesn't have the special Acrobat objects built-in.

                                              :/

                                               

                                              I try search  on API reference and Developing Acrobat Aplication  but  I don't see property list of combobo on one place, table  or some  like that.


                                              i try use some property:

                                               

                                              numItems  -  return a int number of all  items, but  I need a numeber of selected item

                                               

                                               

                                              .so...  maybe   "value" .. ?

                                               

                                               

                                              Yes,  this is It

                                               

                                              Thank You Try67, thank you  for Your time and corectly answer.

                                              • 21. Re: Combobox, how add items from array ?
                                                try67 MVP & Adobe Community Professional

                                                You're welcome, but you can't program based on guesses...

                                                Try downloading this PDF reference file:

                                                http://www.adobe.com/devnet/acrobat/pdfs/AcroJS.pdf

                                                It's not for the latest version of Acrobat, but it's much easier to use than

                                                the online version, and the differences between it and the latest version

                                                are not that big.

                                                A combo-box is a field, so you need to look under the Field object for the

                                                properties and methods available for it.