How do I reference a field through javascript?

Hi everybody,
I'm using Adobe Acrobat X Pro, and I'd like to know how to find the ID to reference a specific field. Ultimately, I'd like to be able to say something like:
"If a dropdown menu has option X selected, show layer X, and hide layer Y".

Thanks so much for all the help!

Kindly,
Peter


Peter Hoeft


2 Answers

Voted Best Answer

When one creates a field, there is a "Name" assigned to that field. For most fields there will only be one name. The exception is for check boxes and radio buttons for which there can multiple fields with the same name creating an exclusionary group. Specific fields within the exclusionary group are accessed by the "widget" for the specific field. More information about widgets is in the Acrobat JavaScript Reference.

JavaScript is an object orientated language, so one must first access the field object and then specify the property or method of the field object to be used.

To obtain a field's object, one used the "doc" object "getField" method. The simplest form is:

var oField = this.getField("Field Name Goes Here"); // get a field name's object;

The above code will return the field object as the variable "oField" or a "null" value if the field is not found.

A simple script to show the value of the field:

app.alert("Field Field Name Goes Here value:" + this.getField("Field Name Goes Here").value);

One then can use the "value" property to obtain the value entered into the field.

You will find more information in the Tutorials on this site or in the Acrobat JavaScript Reference Manual.

I find using a the following document level function works well and reports error in field names.

    function GetField(cName) {
// get field object fro cName field with error reporting;
var oField = this.getField(cName);
// test for error;
if (oField == null) app.alert("Error accessing field " + cName);
return oField; // return field object or null;
} // end GetField funciton;

One could then use it like:

var cFieldName = "Field Name Goes Here"; // name of field;
app.alert(cFieldName + " has a value of: " + GetField(cFieldName).value);


By George Kaiser   

Besides George's advice, find following a hint to toggle layers visibility (so one of them is assumed to be initially off while the other is on).

Custom Keystroke Script of the drop-down box

if (event.willCommit) {
    if (event.value == "X") {
        var layerName1 = "layer1";
        var layerName2 = "layer2";
        var layerObj1 = findLayer(layerName1);
        var layerObj2 = findLayer(layerName2);
        // var layerState1 = layerObj1.state;
        layerObj1.state = !layerObj1.state;
        layerObj2.state = !layerObj2.state;
    }
}


function findLayer(layerName) {
    var ocgArray = this.getOCGs();
    for (var i=0; i < ocgArray.length; i++) {
        if (ocgArray[i].name == layerName) {
            return ocgArray[i];
        }
    }
}

the drop-down should have its property "Commit selected value immediately" chosen.


Ricardo Falegnami   


Please specify a reason: