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
Pages: Index 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280