This
example demonstrates how to hide buttons when printing a form, as
well as how to hide and show objects by changing the presence values
at run time.
Note:
You
can also use the Action Builder dialog box on the Tools menu to
hide and show objects in forms that have a flowable layout, without
writing scripts. For information, see
Building
actions in forms
In this example, all form objects are showing in the form.
The form filler can use the drop-down lists in the Presence Values
area to show or hide objects. In the following diagram, the Address
field is hidden and the form layout has adjusted accordingly. The
Print Form button is also invisible.
Note:
To hide and show objects at run time, you must
save your form as an Acrobat Dynamic PDF Form file.
To see this scripting example and others, visit visit the
Developer
Center
.
Scripting the presence values for subforms
The
script for the subform presence values uses a switch statement to
handle the three presence options that a form filler can apply to
the subform object:
switch(xfa.event.newText) {
case 'Invisible':
Subform1.presence = "invisible";
break;
case 'Hidden (Exclude from Layout)':
Subform1.presence = "hidden";
break;
default:
Subform1.presence = "visible";
break;
}
Scripting the presence values for text fields
The
script for the text fields presence values requires two variables.
The first variable stores the number of objects contained in Subform1:
var nSubLength = Subform1.nodes.length;
The
second variable stores the name of the text field that the form
filler selects in the Text Fields drop-down list:
var sSelectField = fieldList.rawValue;
The
following script uses the
replace
method to remove
all of the spaces from the name of the field stored in the
sSelectField
variable,
which allows the value of the drop-down list to match the name of
the object in the Hierarchy palette:
sSelectField = sSelectField.replace(' ', '');
This
script uses a
For
loop to cycle through all of
the objects contained in Subform1:
for (var nCount = 0; nCount < nSubLength; nCount++) {
If
the current object in Subform1 is of type
field
and
the current object has the same name as the object that the form
filler selected, the following switch cases are performed:
if ((Subform1.nodes.item(nCount).className == "field") & (Subform1.nodes.item(nCount).name == sSelectField)) {
The
following script uses a switch statement to handle the three presence
values that a form filler can apply to text field objects:
switch(xfa.event.newText) {
case 'Invisible':
Subform1.nodes.item(nCount).presence = "invisible";
break;
case 'Hidden (Exclude from Layout)':
Subform1.nodes.item(nCount).presence = "hidden";
break;
default:
Subform1.nodes.item(nCount).presence = "visible";
break;
}
}
}
Scripting the presence values for buttons
The
script for the buttons presence values requires two variables. This
variable stores the number of objects contained in Subform1:
var nSubLength = Subform1.nodes.length;
This
variable stores the name of the button that the form filler selects
in the Buttons drop-down list:
var sSelectButton = buttonList.rawValue;
The
following script uses the
replace
method to remove
all of the spaces from the name of the button stored in the
sSelectField
variable,
which allows the value of the drop-down list to match the name of
the object in the Hierarchy palette:
sSelectButton = sSelecButton.replace(/\s/g, '');
This
script uses a
For
loop to cycle through all of
the objects contained in Subform1:
for (var nCount = 0; nCount < nSubLength; nCount++) {
If
the current object in Subform1 is of type
field
and
the current object has the same name as the object that the form
filler selected, perform the following switch cases:
if ((Subform1.nodes.item(nCount).className == "field") &
Subform1.nodes.item(nCount).name == sSelectButton)) {
This
script uses a
switch
statement to handle the five
presence values that a form filler can apply to button objects.
Note:
The relevant property indicates whether an object
should appear when the form is printed.
switch(xfa.event.newText) {
case 'Invisible':
Subform1.nodes.item(nCount).presence = "invisible";
break;
case 'Hidden (Exclude from Layout)':
Subform1.nodes.item(nCount).presence = "hidden";
break;
case 'Visible (but Don\'t Print)':
Subform1.nodes.item(nCount).presence = "visible";
Subform1.nodes.item(nCount).relevant = "-print";
break;
case 'Invisible (but Print Anyway)':
Subform1.nodes.item(nCount).presence = "invisible";
Subform1.nodes.item(nCount).relevant = "+print";
break;
default:
Subform1.nodes.item(nCount).presence = "visible";
break;
}
}
}
Scripting for resetting the drop-down lists
Use
the
resetData
method to reset all of the drop-down
lists to their default values:
xfa.host.resetData();
Use
the
remerge
method to remerge the form design and
form data. In this case, the method effectively returns the objects
in the Form Objects area to their original states:
xfa.form.remerge();