Search code examples
javascriptacrobat

Populate combo box using user input


Is there a way to add a item to a combo box from a text box on the same page? Also how would you add that item to multiple combo boxes on the same page? This is an acrobat pdf form. Any examples would be greatly appreciated.


Solution

  • This is possible.

    The best is to get the Acrobat JavaScript documentation, which is part of the Acrobat SDK, downloadable from the Adobe website. Of particular interest are the entries for setItems() and getItems() and their referenced Field object methods.

    Due to the implementation of adding items, you would first read the current items, then add your new item, and then write them back (for the example, we assume that there are no specific return values):

    var f = this.getField("myComboBox") ;
    var fitms = new Array() ;
    
    for (var fi = 0 ; fi < f.numItems ; fi++ ) {
    fitms[fi] = f.getItemAt(fi, false) ;
    }
    
    if (this.getField("myAddingValue").valueAsString.length > 0) {
    fitms[fitms.length] = this.getField("myAddingValue").valueAsString ;
    }
    
    f.setItems(fitms) ;
    

    Add this code to a button to add the item in the field myAddingValue, and that's about it. There are code samples in the above mentioned documentation.

    You might create a document-level function which takes the combo box field and the field for the add-on value as argument.