® b c Inspiration becomes reality.™ www.Adobe.com www.Adobe.com b c ©2000 Adobe Systems Incorporated. All rights reserved. February 2000 Using JavaScript with Acrobat Forms James C. King A Principal Scientist b c JavaScript in Acrobat Forms n n bc Brief Introduction Examples Brief Introduction n JavaScript is a programming language Defined by Netscape n Object oriented n Classes: Boolean, Number, Date, Math, String,and Array. n n Has been specialized for Acrobat use Predefined classes and objects n Objects: app, console, doc, event, field, global, util and of course this. n bc Brief Introduction n Learn Acrobat Forms in stages The basics n Validate, calculate, actions n Submit and receive with WWW Server n Scripting simple things n Scripting programs n n Learn by Example Copy and modify example scripts n Can do a lot with very simple scripts n bc Documentation n Two important documents JSSpec.pdf n AcroJS.pdf n n Found in the Acrobat “Help” folder n bc See especially “Field Properties” section of AcroJS.pdf Alerts n Example -- before typing into field n Field A: Field A -- Actions Script: app.beep(0); app.alert("Please be accurate while typing into this field."); bc Asking Questions n Example -- accessing other fields n Field B: Field B -- Actions Script: app.beep(0); var happy = app.alert("Are you happy?", 3, 2); var h = this.getField("HappyFace"); var s = this.getField("SadFace"); if (happy == 4) {h.hidden = false; s.hidden = true;} else {h.hidden = true; s.hidden = false;} bc Mouse overs n Example -- changing properties n Field C: Adobe Systems Incorporated Field C -- Actions Script on Mouse Enter: var f = this.getField("FieldC"); f.fillColor = color.yellow; f.strokeColor = color.blue; f.textColor = color.black; bc Field C -- Actions Script on Mouse Exit: var f = this.getField("FieldC"); f.fillColor = [ "CMYK", 1.0, 0.2, 0.2, 0.0]; f.strokeColor = color.yellow; f.textColor = ["RGB", 1, 1, 1]; Calculate Script n Example -- complicated sums Cost * Quantity = Total Cost.0 $2.00 1 Cost.1 1 Cost.2 1 Cost.3 1 bc $2.00 Total -- Calculate Script var L1 = "Cost"; var L2 = "Quantity"; var N = 4; var sum = 0; for (var i = 0; i<N; i++) { var tok1 = L1 + "." + i; var tok2 = L2 + "." + i; sum += this.getField(tok1).value * this.getField(tok2).value; } f = this.getField("Total"); f.value = sum; Levels of Scripts n n n bc Field Level -- in field actions, validate, calculate Document Level -- in “Tools->Forms->Document JavaScripts” Plug-in Level -- in “Plug-ins/AcroForms/JavaScript” folder Levels of Scripts n Example Document Level -- Change all backgrounds Mouse Up Action: Push Me! for (var i=0; i<this.numFields; i++) {f = this.getField(this.getNthFieldName(i)); f.fillColor = ["RGB", 0.8, 0.8 ,0.0 ];} Mouse Up Action: ChangeAllBackgrounds(color.transparent); Reset Colors! bc In Document JavaScripts: function ChangeAllBackgrounds(color) {for (var i=0; i<this.numFields; i++) {f = this.getField(this.getNthFieldName(i)); f.fillColor = color;}} Variable categories n n n n bc Locals -- use “var y” Document wide -- use “this.y” “Within Session Globals” -- use “global.setPersistent (y, false)” “Across Session Globals” -- use “global.setPersistent (y, true)” Variable categories Var: (ZZ) local value set this: (YY) setup Persistent(false): (WW) setup Persistent(true): (XX) setup bc Set All: setup ® b c Inspiration becomes reality.™ www.Adobe.com www.Adobe.com b c ©2000 Adobe Systems Incorporated. All rights reserved. Variable categories Var: (ZZ) local value set this: (YY) setup Persistent(false): (WW) setup Persistent(true): (XX) setup bc