6
Using Acrobat JavaScript in Forms
Forms Essentials
If you are accustomed to calculating the positions of form fields from the top left corner of a
page, the following example will serve as a template for obtaining the correct position. In
this example, we will position a 1 inch by 2 inch form field 0.5 inches from the top of the
page and 1 inch from the left side:
// 1 inch = 72 points
var inch = 72;
// obtain the page coordinates in Rotated User Space
var aRect = this.getPageBox({nPage: 2});
// position the top left corner 1 inch from the left side
aRect[0] += 1 * inch;
// make the rectangle 1 inch wide
aRect[2] = aRect[0] + 1*inch;
// top left corner is 0.5 inch down from the top of the page
aRect[1] -= 0.5*inch;
// make the rectangle 2 inches tall
aRect[3] = aRect[1] - 2*inch;
// draw the button
var f = this.addField("myButton", "button", 2, aRect);
Duplicating Form Fields
It may sometimes be useful to duplicate information typed in by the user in other pages of
the document. For example, you might wish to display the user’s name on every page of
the document.
To automate this, give all such form fields the same name and actions. Then whenever the
user triggers a related action, the same information appears in all form fields containing
that name.
To duplicate form fields in general, assign the same name and actions to each of them. In
the example below, we will create duplicate text fields, each named
myField
, on page 2 of
the document, and we will set the background color of every instance to yellow:
for (var i = 0; i < 5; p++)
{
var aRect = [36, 36+100*i, 72, 144+100*i];
var f = this.addField("myField", "text", 2, aRect);
f.fillColor = yellow;
}
104
Acrobat JavaScript Scripting Guide
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