PDFill | Overview  | First |  Previous  |   Next  |  Last

PDF Document Management 14:  Add PDF JavaScript into PDF Document

The JavaScript language was developed by Netscape Communications as a means to create interactive web pages more easily. Adobe has enhanced JavaScript so that you can easily integrate this level of interactivity into your PDF documents.
Acro6JSGuide.pdf and AcroJS.pdf shows the detailed information about Acrobat JavaScript Scripting.

This function can create document-level JavaScript actions that apply to the entire PDF document.

Document Level Example 1:  Force Upper Case

Document Level Example 2: Validate Age

Document Level Example 3: Check Comment Characters Length

Document Level Example 4: Insert the current date into a Text Field when the PDF is opened

Document Level Example 5: Set document zoom to 100%


Here are the steps on how to Add PDF JavaScript into PDF Document:

Choose Document Menu > JavaScript

or click JavaScript Button in the Document Toolbar.

Here is the dialog of JavaScript for Document's Properties:

Here are some examples:

//Document Level Example 1:  Force Upper Case

var nameField = this.getField('Text 1');
nameField.setAction('Keystroke', 'forceUpperCase()');

function forceUpperCase()
{
    if(!event.willCommit)
        event.change = event.change.toUpperCase();
};

//Document Level Example 2: Validate Age

var ageField = this.getField('ageField');
ageField.setAction('Validate', 'checkAge()');

function checkAge()
{
    if(event.value < 12)
    {
        app.alert('Warning! Applicants age can not be younger than 12.');
            event.value = 12;
    }
};

//Document Level Example 3: Check Comment Characters Length

var commentField = this.getField('commentField');
commentField.setAction('Keystroke', 'checkLength()');

function checkLength()
{
    if(!event.willCommit && event.value.length > 10)
    {
        app.alert('Warning! Comment can not be more than 10 characters.');
        event.change = '';
    }
};

//Document Level Example 4: Insert the current date into a Text Field when the PDF is opened

var f = this.getField("Text1");
f.value = util.printd("mmm/d/yyyy", new Date());

//Document Level Example 5: Set document zoom to 100%

this.zoom = 100;

PDFill Copyright 2002-2022 by PlotSoft L.L.C..  All rights reserved.