Setting Form Field Properties Programatically
Setting form field properties using JavaScript can automate the process
If you will be setting form field properties while creating and positioning a form field programatically using the addField method you should first define a variable for the field by running the following script in the console like this:
var f = this.addField("MyField", "text", 0 , [0, 18, 72, 0]);
Now you can use f instead of typing this.getField("MyField") for each field property you are going to set:
f.alignment="center"; //sets the field alignment to center
f.fillColor=color.yellow; //sets the fill color of the field to yellow
f.textColor=color.blue; //sets the text color of the field to blue
f.textFont="Times-Bold"; //sets the font of the field to Times Bold
The JavaScript property settings are not always intuitive. For example, the font Helvetica Bold-Oblique as shown in the Font dropdown of the field properties in the image below cannot be set by copying that font description into the script.
The script is as follows:
f.textFont="Helvetica-BoldOblique";
Notice the difference in spacing and hyphen placement? To set the font to "Times Bold" as displayed in the field properties dropdown, you would use the following script in the console as outlined above:
f.textFont="Times-Bold";
Setting Colors
In the example above, colors were set to blue and yellow using color.blue and color.yellow. This method can be used for the following colors:
blue
yellow
red
green
cyan
black
white
transparent
How Do You Know The Correct Property Settings?
The easiest way to determine the correct property settings is to the set the property manually using the Acrobat User Interface, and then read the field property with a script like this:
this.getField("MyField").textFont;
When the script above is run from the console it will return the correct string to set that particular font with a script. Then you read the fill colors of fields with colors listed above, the script will not return color.blue, color.red, etc. It will instead return an array of the color model and numbers for that color model:
//With a field in which the fill color was set to color.red
this.getField("MyField").fillColor;
//returns RGB, 1, 0, 0
//With a field in which the fill color was set to color.blue
this.getField("MyField").fillColor;
//returns RGB, 0, 0, 1
//With a field in which the fill color was set to color.cyan
this.getField("MyField").fillColor;
//returns CMYK,1,0,0,0
//With a field in which the fill color was set to color.white
this.getField("MyField").fillColor;
//returns G, 1
//With a field in which the fill color was set to color.black
this.getField("MyField").fillColor;
//returns G, 0
Instead of color.red, color.blue, etc. to set the fill colors, you can also set them with the arrays like this (in order from the code block above):
f.fillColor=["RGB", 1, 0, 0];
f.fillColor=["RGB", 0, 0, 1];
f.fillColor=["
CMYK",1,0,0,0
];f.fillColor=["G", 1];
f.fillColor=["G", 0];
The "G" stands for Grey. Greys are all mixtures of black and white. The number after the G is how much white there is. 1 is all white. 0 is zero white, so it’s black. You can get any shade of grey by making the number a fraction of 1 like this:
f.fillColor=["G", 0.5];
f.fillColor=["G", 0.75];
Any Color From The Color Object
In fact, you can set the fill color, or the text color, or the border color of a field to any color in the color object by using the color picker to manually set the color, then reading the color with a script.
When the fill color is set to the color as shown above, reading the fill color of the field with a script returns: RGB,0.835296630859375,0.560791015625,0.1999969482421875
Almost Any Field Property
It’s not just the alignment, fill color, text color, and font properties that can be set with a script when creating a field, but almost any field property can be set. The trick is to find the JavaScript call for the particular property. If you search "field properties" in the Acrobat JavaScript reference guide you can find the entire list.
Not every field property can be read by running a script in the console. Formats, keystroke scripts, and mouse actions can be set using the setAction method but these properties can’t be read with a script.
In Using JavaScript To Add JavaScript to a PDF Part 2 I describe in detail how to obtain scripts to set these field properties.
Please Support My Work
Ways To Support My Work:
Leave me a comment - I started writing this newsletter in October of 2023. It publishes every Saturday at 6:00 am, and it’s free. I haven’t missed a week but it hasn’t been easy. Please leave a comment so I know you’re out there.
Give me a suggestion - Again, it’s not easy to come up with content week after week. Let me know in the comment section what subject you’d like me to cover and I’ll consider it.
Subscribe - If you haven’t already, please subscribe. It’s free.
Share this publication.
Purchase An Automation Tool - Check out the tools in our online store.
Take The Online Course - Acrobat Pro/JavaScript online course information.
Hire me:
Custom PDF automation solutions
Custom dynamic stamps
Custom fillable forms
Custom toolbar buttons and menu items
Custom scripts
Send me a message and start the conversation, or fill out the contact form on our website.
THANK YOU FOR YOUR SUPPORT!