Creating PDF Form Fields Programmatically
Creating form fields using JavaScript can automate the process
The addField method creates a form field with four mandatory input parameters:
cName (The name of the field)
cFieldType (The type of field)
nPageNum (The 0-based number of the page to which the field will be added)
oCoords (An array of four numbers representing the rectangle box of the field’s position, or the rect property of the field)
Since all inputs are mandatory it's not necessary to use the parameter names if you use the correct order: this.addField(Name, Type, Page Number, Rect). For example, the following script will create a text field named MyField on the first page of the PDF, 1 inch wide by 1/4 inch high at the bottom left of the page:
this.addField("MyField", "text", 0 , [0, 18, 72, 0]);
Field Types
cFieldType can be one of the following:
text
button
checkbox
radiobutton
combobox
listbox
signature
combobox is often referred to as a "dropdown" field.
Field Positioning
The page box coordinates should be obtained in order to position a field relative to the edge of the page. Page box coordinates were covered in the Page Box Coordinates and Cropping and Reverse Cropping PDF Pages posts.
Example 1
Run the following script in the console to place a 1 inch by 1/4 inch text box, 1 inch from the top of page 1, and 1 inch from the right side of the page:
var cb=this.getPageBox("Crop", 0);
this.addField("myField", "text", 0, [cb[2]-72*2, cb[1]-72, cb[2]-72, cb[1]-72-72*0.25]);
There are 72 points per inch. The script above puts the math calculations into the script:
cb[2]-72*2 means the left side of the field is 2 inches from the right side of the page.
cb[1]-72 means the top of the field is 1 inch from the top of the page.
cb[2]-72 means the right side of the field is 1 inch from the right side of the page.
cb[1]-72-72*0.25 means the bottom of the field is 1 1/4 inches from the top of the page.
Example 2
To create the same size field 1 inch from the left side of the page, and 1 inch from the top of page 2, run the following script in the console:
var cb=this.getPageBox("Crop", 1);
this.addField("myField", "text", 1, [72, cb[1]-72, 72*2, cb[1]-72-72*0.25]);
Since cb[0] (the left side of the page) is always zero, it is omitted from the calculation.
Example 3
To place the same size button field 1 inch from the left edge of page 2, and 1 inch from the bottom edge of page 2, run the following script in the console:
this.addField("myButton", "button", 1, [72, 72+72*0.25, 72*2, 72]);
Since the left side of page box and the bottom of the page box are always zero, starting at the left side of the field and working clockwise, the field position in the script above means:
1 inch from the left side of the page.
1 1/4 inch from the bottom of the page.
2 inches from the left side of the page.
1 inch from the bottom of the page.
Example 4
To place the same size button field 1 inch from the right edge of page 2 and 1 inch from the bottom of page 2, run the following script in the console:
var cb=this.getPageBox("Crop", 1);
this.addField("myButton", "button", 1, [cb[2]-72*2, 72+72*0.25, cb[2]-72, 72]);
Starting at the left side of the field and working clockwise, the field position in the script above means:
2 inches from the right side of the page.
1 1/4 inch from the bottom of the page.
1 inch from the right side of the page.
1 inch from the bottom of the page.
In my next post I will discuss how to set other field properties when creating fields programmatically.
Take the eLearning course for JavaScript for Acrobat Pro