Text and dropdown fields have a format tab so the format of the field can be set.
In field editing mode, the properties of multiple fields can be set at the same time by simply selecting those fields and changing the properties of one of the selected fields in the properties window. This does not work for all properties because certain property tabs are missing when multiple fields are selected, as I described in Widgets. Format is one of those missing tabs.
Formatting Fields Using JavaScript
In Using JavaScript To Add JavaScript to a PDF Part 2, I described how to format a field using a script, and how to find those custom Format and Keystroke scripts. You have to name a field to format it with a script, so how do you format multiple fields simultaneously with a script? As I've stated several times, field naming requires some thought if you want to save time when programming PDF fillable forms.
If you use the right-click > Create multiple copies feature in form editing mode, the fields will automatically be named with a number suffix (.0, .1, .2, .3, etc.). This can be used to loop through all of the fields by adding an incrementing number to the field names:
for(var i=0;i<10;i++)
{
this.getField("Text."+i).setAction('Format','/*format script*/');
this.getField("Text."+i).setAction('Keystroke','/*keystroke script*/');
}
What if You Didn’t Use Right-click > Create Multiple Copies?
Here are the steps to format multiple fields simultaneously when those fields do not contain a number suffix that you can loop through:
In form editing mode, select the fields to be formatted.
Change one of the properties that can be changed for multiple selected fields at once.
Run a script in the console that loops through all form fields, tests for the type of field and the property from the previous step.
When fields in the loop pass the test, format those fields and return the property change from step 2 back to the original.
Criteria For Selecting The Field Property To Change
It needs to be a property that can be changed in multiple fields at once by first selecting those fields, then changing the property for one of them.
It should be a property that will most likely be same for all fields.
One of the properties that meets both criteria is the rotation property ("Orientation" in the properties window). This property can be set to 0, 90, 180, or 270. It will most likely be 0 in all text fields. 0 degree rotation in a text field means the filled in text will read left to right. 90 degrees reads bottom to top, 180 is right to left and upside down, and 270 is top to bottom. Changing the rotation property property does not change the field position.
Steps for Multiple Field Formatting
Suppose you want to format the fields as numbers, with a comma separator and two decimal places.
In form editing mode, select the fields that will be formatted.
Open the properties of one of the selected fields by right-clicking and selecting Properties. Ensure the fields remain selected.
In the General tab of the field properties, set the Orientation to 90 degrees.
Run the following script in the console:
for(var i = this.numFields - 1; i > -1; i--)
{
var fieldName = this.getNthFieldName(i);
if(this.getField(fieldName).type =="text" && this.getField(fieldName).rotation==90)
{
this.getField(fieldName).setAction('Format','AFNumber_Format(2,0,0,0,"",true);');
this.getField(fieldName).setAction('Keystroke','AFNumber_Keystroke(2,0,0,0,"",true);');
this.getField(fieldName).rotation=0;
}
}
To determine the format and keystroke scripts to use with the setAction( ) method, please read Using JavaScript To Add JavaScript to a PDF Part 2.