Using Javascript in PDF's

We have created the code to populate the form with the correct information and also to pass a value back indicating whether or not we had a successful match for the provided Order Number. Upon returning data to the form, it would be helpful if we could do a few things, for example :

This can be achieved by using a little Javascript and the Adobe Object Model - more on which can be found in the Adobe Acrobat Javascript Guide, which also appears as an option on the Help menu in Adobe Acrobat.

Acrobat offers a selection of objects that can contains Javascript. Document Javascripts - Tools, Forms, Document Javascripts - contain global functions, variables and code that is to be run when the document is opened. Page Javascripts - Document, Set Page Action - determine what functions to run whenever the current page is opened or closed within the viewer. Each field can also have its own Javascripts that run when certain actions or situations occur.

One of these is the Calculation script that will be run whenever the field they are attached to is refreshed - either by the document being opened, by a user scrolling onto a page containing the field etc. We will use this type of script in conjunction with a hidden field on the Form which contains the return value from our ASP page indicating if we had a successful match, thus whenever that value is changed it will run the recalculate script. The actual Javascript that we will use is as follows :

var status=this.getField("retval");
// if the value is one, then we got a record
if (status.value=="1")
{
    // hide the Retrieve button and show the Save one
    var saveButton=this.getField("savebtn");
    saveButton.hidden=false;
    var getButton=this.getField("submitbtn");
    getButton.hidden=true;
    // set the Customer Name to be Read Only
    var orderNo=this.getField("orderno");
    orderNo.readonly=true;
}
// else inform the user
else
{
    app.alert("No matching records were found.",0,0);
}
// reset the status value
status.value="0";

If you try entering several different Order Numbers onto the third version of our form then you should be able to see this function in operation.

In the final section of this article, we review what has been covered and provide some links to books and websites for futher study.