5 Replies Latest reply: May 3, 2012 11:07 AM by GKaiseril RSS

    Populate a drop down list using Javascript in Acrobat, not LifeCycle

    JDMAWoods Community Member

      I have a series of forms that are being provided to me with standard text boxes.  These forms are developed for worldwide access, but in this country I need to prepare a variation of the form with a dropdown list containing over 350 items.  I lose all of the settings that are provided in the master document when I try to switch the PDF to a LifeCycle form, so I need to keep the form as an Acrobat form.  I do have a working copy of the Javascript in LifeCycle, but how do I move that over into Javascript within the Acrobat form?  There doesn't appear to be a way to "dynamically populate" the drop down selection list from XML data.  Has anyone else tried to do this or know what I am doing wrong?  Here is a sample of what I have down so far...

       

      function PN_Populate(dropdownField)

                {

                for (var i=0; i < ConvData.ConvnName.length; i++)

                          dropdownField.addItem(ConvData.ConvnName[i]);

                }

       

       

      function PN_ReadOut(EvntWkName, ConvDate, ConvNum)

                {

                for (var i = 0; i < ConvData.ConvnName.length; i++)

                          {

                                    if (ConvData.ConvnName[i] == EvntWkName)

                                    {

                                    ConvDate.rawValue = ConvData.ConvnDate[i];

        break;

                                    }

                          }

                }

       

      //These are sample variable arrays

       

      var ConvnName = new Array ("name1","name2","name3","name4","name5","name6","name7","etc.")

      var ConvnDate = new Array ("date1","date2","date3","date4","date5","date6","date7","etc.")

      var ConvnNum = new Array ("num1","num2","num3","num4","num5","num6","num7","etc.")

       

      //In LifeCycle, I associated this code with the initialize call...

       

      this.clearItems();

      ConvProg.PN_Populate(this);

       

      ConvProg.PN_ReadOut(xfa.event.newText, ConvDate, ConvNum);

       

      //And this is the code with the change call...

       

      ConvProg.PN_ReadOut(xfa.event.newText, ConvDate, ConvNum);

       

       

      Any guidance is appreciated!

       

      - Jason

        • 2. Re: Populate a drop down list using Javascript in Acrobat, not LifeCycle
          JDMAWoods Community Member

          Thank you, George, for your response.  So, I'm reading through all the instructions on how this would work, but I'm having difficulty knowing where to put the code.  I tried associating it with the "Actions" tab of the property, and with an "onFocus" run a JavaScript, but that creates an error.  I also tried associating it with a "calculate" script, but I don't want the field calculated.  Finally, I tried to just place the code in the "All Document Scripts" section, but apparrently I don't have the right syntax for that section either...

           

          As I mentioned, I figured it out with lifecycle, but this "simple JavaScript editor" that is built in to Acrobat, is a little to non-descript for me. 

           

          Can you identify where I should setup this action?

           

          Maybe point me to some better sample code? 

           

          Thanks!

          • 3. Re: Populate a drop down list using Javascript in Acrobat, not LifeCycle
            George Johnson CommunityMVP

            It is 't clear to me when you need it to run. If you could provide more information about how you need this to behave, it would be helpful.

            • 4. Re: Populate a drop down list using Javascript in Acrobat, not LifeCycle
              JDMAWoods Community Member

              My apologies.  I just want the data for my dropdown list to be obtained from a script (I'm assuming it must be a JavaScript) rather than manually add them to the Dropdown Properties / Options tab because I have a list of almost 400 entries to make.  So, technically, I guess it could run on document open.  I'm not dynamically changing the contents of the list.  It is a fixed list.

              • 5. Re: Populate a drop down list using Javascript in Acrobat, not LifeCycle
                GKaiseril CommunityMVP

                Unlike LiveCycle Designer, Acrobat allows the setting of multiple entires and their export value at one time. You can define an array of the option items and their optional export values and then use the field object's "setItems" method.

                 

                You could define the array as a document level variable and then have a document level function to load that array to the field. Once setup, one could use the JavaScript console to load the drop down box only as needed or call the function in a document level JS and load the drop down box each time the form was opened.

                 

                // define array of entries and export values for drop down array

                // define array of states and abbreviations
                var aStates = new Array(["Select State", ""],
                ["Alabama", "AL"],
                ["Alaska", "AK"],
                ["Arizona", "AZ"],
                ["Arkansas", "AR"],
                ["California", "CA"],
                ["Colorado", "CO"]);

                 

                // function to load a combo/list box with an array of values
                function LoadOptions(oField, aValues) {
                var bResult = false;
                // load array of values into field object
                if(oField.type != "combobox" && oField.type != "listbox") {
                app.alert("Load Options function requires a combobox or listbox", 0, 0);

                bResult = false;
                } else{
                oField.setItems(aValues); // set values
                bResult = true;
                }
                return bResult;
                } // end  LoadOptions

                 

                // load the data - comment out after updating when only doing array changes
                LoadOptions(this.getFeild('Combo Box1'), aStates);

                 

                I would keep this in the PDF because a list can always change and it is a real pain to maintain a combo box or list box if there are multiple changes and reordering of the list.