Zero-based Indices
A of lot things in Document Object Model (DOM) JavaScript are zero-based, meaning the 1st is zero, the 2nd is 1, the 3rd is 2, etc.
Here are some examples:
You can test the scripts in the code blocks by running them in the console.
Page numbers: this.pageNum returns 0 if you are on page 1, and 1 if you are on page 2. In JavaScript the first page of the PDF document is always 0. The last page, if the number of pages is not known, can be calculated by subtracting 1 from the script for the number of pages:
this.numPages-1;
This is helpful if wanted to write an action to run on multiple PDFs with various page lengths, that does something to the last page.
Field Widgets: Widgets are copies of the same field.
In other words, if you have 5 text fields named First Name, the field First Name would have 5 widgets. Some field properties are applied at the field level, meaning there can only be one such property for all widgets. Some properties can be applied at the widget level, meaning the property can be different for each widget. Field value is an example of a field level property. Fill color is an example of property than can be applied at the widget level. To change the fill color to yellow for the first First Name widget, run the following script:
this.getField("First Name.0").fillColor = color.yellow;
For the last (5th) First Name widget run the following script:
this.getField("First Name.4").fillColor = color.yellow;
To change all widgets the script can be run without the number suffix:
this.getField("First Name").fillColor = color.yellow;
Nth Field Name: The following scripts will return the names of the nth form field in the document:
this.getNthFieldName(0);//First field name this.getNthFieldName(1);//Second field name this.getNthFieldName(this.numFields-1);//Last field name
Nth Word On A Page: The getPageNthWord document method take two input parameters - the page number and the nth word. The following scripts will return the nth word on a page:
this.getPageNthWord(0,0);//The 1st word on the 1st page; this.getPageNthWord(0,1);//The 2nd word on the 1st page; this.getPageNthWord(1,0);//The 1st word on the 2nd page; this.getPageNthWord(1,1);//The 2nd word on the 2nd page; this.getPageNthWord(this.numPages-1,this.getPageNumWords(this.numPages-1)); //The last word on the last page
Array Items: The first item in an array is 0, the second item is 1, etc.
var letters=["A","B","C","D","E"]; letters[0];//returns "A" letters[1];//returns "B" letters[4];//returns the 5th item, "E"
If the number of items in the array is unknown, you can get the last item by subtracting 1 from the script for the length of the array:
letters[letters.length-1];//returns "E"
Many Functions and Methods Return Arrays
Annotations - this.getAnnots( ) returns an array of all annotations in the document.
this.getAnnots()[0];//returns the first annotation this.getAnnots().length;//returns the number of annotations this.getAnnots()[this.getAnnots().length-1];//returns the last annotation
Templates - this.templates returns an array of template objects.
this.templates[0];//returns the first template object this.templates.length;//returns the number of template objects this.templates[this.templates.length-1];//returns the last template object
OCGs (Layers) - this.getOCGs( ) returns an array of OCG objects.
this.getOCGs()[0];//returns the first OCG object this.getOCGs().length;//returns the number of OCG objects xthis.getOCGs()[this.getOCGs().length-1];//returns the last OCG object