Anticipating Field Names in PDF Custom Calculation Scripts
In my last column on how to create a check box scoring questionnaire I described the right-click > Create Multiple Copies function, which is one of the handiest shortcuts for creating fillable fields.
It will be used again in this column, as well as a math addition shortcut for fields with the same route name that was also described in the previous article.
It's important to point out that the Create Multiple Copies shortcut actually makes copies of the original fields, so all formatting and scripting contained in the original fields will be present in the copied fields. It's also important to point out that certain field properties can be changed for multiple fields simultaneously by selecting the fields and changing properties for one of the selected fields. However, other field properties cannot be changed for multiple fields this way.Â
Take the Acrobat Pro/JavaScript eLearning Course Acrobat Like a Pro
FORMATTING MULTIPLE FIELDS AT THE SAME TIME
It's easy to determine which properties can be changed for multiple fields and which can't by simply opening the field properties with one field selected versus opening the field properties with multiple fields selected.
The screenshot on the left is the Acrobat UI field properties with one text field selected. The screenshot on the right is the Acrobat UI field properties with multiple fields selected. Not only are the Name and Tooltip properties disabled in the second screenshot, but three of the tabs (Format, Validate, and Calculate) are missing. If you want to set any of the properties in any of these missing tabs, and have those properties consistent for all copies of the fields, you can set them prior to activating the Create Multiple Copies shortcut.
For example, if you wanted the fields formatted as two decimal place numbers with dollar sign currency symbols, you would format the first field prior to placing multiple fields. If you forgot to format the fields you would either have to remove the fields and start again or format the fields one at a time. For purposes of the forthcoming example, the fields will be formatted as described, and will also contain a custom calculation script. Here's the scenario:
FORM EXAMPLE
An "invoice" form with a quantity column, a dropdown field item column, and a subtotal column.
Each subtotal field is calculated as the product of the corresponding quantity field and the corresponding price (that is, the export value of the dropdown selection).
TWO VALUES IN DROPDOWNS
Dropdowns, or Combo Box fields, can have two values:
1)Â Â Â Â Item name (what the users see in the dropdown field after making the selection).
2)    Export value (the value of the field when the corresponding item name is selected).   Users don't see the export values.
The export values are optional. If the export values aren't used, the item names become the export values. When a dropdown field value is called with a script it returns the export value (if it exists) or the item name if the export value does not exist. This structure comes in handy when designing forms. Here's a couple of examples:
1)    A dropdown field that contains employee names as the item names, and employee email addresses as the export values. I used this structure to design a telephone message system for an office. The receptionist would select the employee's name from the dropdown and automatically email a message to the employee. The form was grabbing the email address from the value of the dropdown that had employee names as the visible item names.
2)   A dropdown that contains descriptions of products as the item names, and their corresponding prices as the export values. This structure will be used in the example.
THE STEPS
1)Â Â Â Create a horizontal row of three fields:
a)Â Â Â A text field named Quantity, formatted as a number with no decimal places.
b)   A dropdown named Item, with product descriptions for item names, and corresponding prices for export values. Commit selected value immediately will be checked in the options tab of the field properties.
c)   A text field called Subtotal, formatted as a number with two decimal places. This field will also contain a custom calculation script that sets its value by multiplying the corresponding Quantity field value by the corresponding Item field value.
2)   Anticipate the field names after using Create Multiple Copies. We know that a period and number will be added as a suffix to each field. The original field names will be changed, as .0 is added to the names. The new fields will be followed by .1, .2, .3, etc.
3)   Add a custom calculation script to the Subtotal field that will work for all Subtotal fields created with Create Multiple Copies. The is done by extracting the number from the field name as a variable, then adding that variable back into the other field names in the calculation. The script:
var num = event.target.name.replace("Subtotal.","");
/*the line above extracts the suffix number by replacing "Subtotal." with an empty string. It doesn't matter what the number is, the variable num will represent the number in the field names*/
event.value=this.getField("Quantity."+num).value * this.getField("Item."+num).value;
When you add the script above as a custom calculation in the Subtotal field and Create Multiple Copies for all three fields, the calculation will work in all fields.
Video
To view in full screen mode on a PC click the square frame in the bottom right of the video when it starts playing.
To exit full screen and come back to the article, press the Esc key.
Shameless Plug: The Dropdown Filler Tool demonstrated in the video above can be purchased at www.pdfautomationstation.com
VIDEO AFTERTHOUGHT
It happens. The article is already written, posted, and scheduled. Upon another proofreading or video viewing I find something that needs to be changed or added. I just noticed it in the video above. I don’t want all those zeros in the calculated Subtotal fields until a selection is actually made so I went back and tweaked my custom calculation script. I’m not going to re-do the video though so I made another short one. That’s one of the things that drew me to Substack, I can easily change the content even when something is already posted, or scheduled to be posted. If you find any typos or errors in any of my content, let me know in the comments and I’ll go back and spruce it up, for everyone’s benefit. Cheers. See you next time…