Acrobat JavaScript Tools
Exercise: Calculator
2
How the Calculator Displays Output
Calc.pdf
uses the Acrobat
Doc’s
getField()
method to place values in the calculator
display window, which is the text field
display
. In this statement,
getField()
binds
the variable
display
to this field:
var display = this.getField("Display");
Every time a numeric key is clicked, its value should be shown in the calculator display
window. To do this, we define a
Mouse Up
action for each numeric key in which the
number shown on the key is assigned to the calculator display window. For example, when
the user clicks the
7
button, the
Mouse Up
action associated with the button could assign
the value returned by
digit_button(7)
to the calculator window as shown below:
display.value = digit_button(7);
Calc.pdf
uses a similar mechanism to display the strings representing arithmetic
operations to the
func
text field, which is used to display the operation (PLUS, MINUS,
MULT, DIV). In this statement,
getField()
binds the variable
func
to this field:
var func = this.getField("Func");
The code below is contained in the
Mouse Up
action for the division (/) key, and represents
a call to the
func_button()
script:
func_button("DIV");
The action passes the string
"DIV"
to the
func_button()
function’s parameter named
req_func
, which is thus bound to that value.
Later in the function, this statement is encountered:
func.value = req_func
Because
getField()
has bound the
func
variable to the
"Func"
field, this statement
displays the string
"DIV"
in the field.
Values With More Than 1 Digit
To adjust for values greater than or equal to 10, the calculator multiplies the current value
by 10 and adds the value of the numeric key selected. For example, if the current value is 2
and the user clicks the
3
button, the new current value is 23 (10 times 2 plus 3).
For decimal values, a
Mouse Up
action is associated with the
.
button. In this action, the
calculator multiplies the divisor by 10. Subsequently, when other numeric keys are
selected, the calculator divides the selected value by the divisor, and adds that amount to
the current value. For example, if the current divisor is 1 and the user presses the
2, .,
and
4
buttons, respectively, the current value is first updated to 2, the divisor becomes 10, and 4 is
divided by the divisor to obtain 0.4, which is added to 2 to obtain 2.4, which is assigned to
the new current value and shown in the display area.
Testing the Calculator
To familiarize yourself with the basic calculator operations, open
Calc.pdf
in Acrobat and
try entering expressions and evaluating their results.
When you are familiar with the calculator’s operation, close this file.
Acrobat JavaScript Scripting Guide
61