Skip to main content
Anantha Prabu G
Legend
October 10, 2024
Answered

Script to Change Width and Height of All Checkboxes in PDF Not Working

  • October 10, 2024
  • 3 replies
  • 947 views

Hi everyone,

I’m working on a PDF form in Acrobat and trying to adjust the width and height of all checkboxes using JavaScript. I’ve written a script, but it doesn’t seem to be working on all checkboxes.

Here’s the script I’m using:

// Conversion: 0.12 inches = 8.64 points
var newSize = 8.64;

// Iterate through all the fields in the document
for (var i = 0; i < this.numFields; i++) {
    // Get the field name
    var fieldName = this.getNthFieldName(i);
    
    // Get the field object
    var field = this.getField(fieldName);
    
    // Check if the field is a checkbox
    if (field.type === "checkbox") {
        // Get the current position of the checkbox
        var rect = field.rect;
        
        // Calculate the center of the checkbox
        var centerX = (rect[0] + rect[2]) / 2; // Horizontal center (average of left and right)
        var centerY = (rect[1] + rect[3]) / 2; // Vertical center (average of bottom and top)

        // Calculate the new left, right, top, and bottom values
        var newLeft = centerX - newSize / 2;
        var newRight = centerX + newSize / 2;
        var newBottom = centerY - newSize / 2;
        var newTop = centerY + newSize / 2;

        // Apply the new size while maintaining the center position
        field.rect = [newLeft, newBottom, newRight, newTop];
    }
}
app.alert("All checboxes changed to 0.12 inches")

 

Could anyone help me figure out what’s going wrong or suggest improvements to make this work for all checkboxes?


Thanks!

Prabu G

This topic has been closed for replies.
Correct answer try67

This script works on individual fields, but not on those that are a part of a group. Such fields all share the same name, but they all have unique rect values. However, getField will only return the first field (also called a "widget") in the group.

To solve it you need to access the index number of each widget in the group and add it to the parameter you pass to getField, preceded by a period. Try this code:

 

// Conversion: 0.12 inches = 8.64 points
var newSize = 8.64;

// Iterate through all the fields in the document
for (var i = 0; i < this.numFields; i++) {
    // Get the field name
    var fieldName = this.getNthFieldName(i);
    
    // Get the field object
    var field = this.getField(fieldName);
    
    // Check if the field is a checkbox
    if (field.type === "checkbox") {
		var idx = 0;
		var widget = this.getField(fieldName+"."+idx);
		while (widget!=null) {
			// Get the current position of the checkbox
			var rect = widget.rect;
			
			// Calculate the center of the checkbox
			var centerX = (rect[0] + rect[2]) / 2; // Horizontal center (average of left and right)
			var centerY = (rect[1] + rect[3]) / 2; // Vertical center (average of bottom and top)

			// Calculate the new left, right, top, and bottom values
			var newLeft = centerX - newSize / 2;
			var newRight = centerX + newSize / 2;
			var newBottom = centerY - newSize / 2;
			var newTop = centerY + newSize / 2;

			// Apply the new size while maintaining the center position
			widget.rect = [newLeft, newBottom, newRight, newTop];
			idx++;
			widget = this.getField(fieldName+"."+idx);
		}
    }
}
app.alert("All checboxes changed to 0.12 inches")

 

3 replies

PDF Automation Station
Community Expert
Community Expert
October 10, 2024

Adding to what @try67 said, you have the newBottom and newTop reversed in the rect array.  It should be:

[newLeft, newTop, newRight, newBottom];

Although it will still work in this instance.

Anantha Prabu G
Legend
October 10, 2024
Thanks,PrabuDesign smarter, faster, and bolder with InDesign scripting.
try67
Community Expert
try67Community ExpertCorrect answer
Community Expert
October 10, 2024

This script works on individual fields, but not on those that are a part of a group. Such fields all share the same name, but they all have unique rect values. However, getField will only return the first field (also called a "widget") in the group.

To solve it you need to access the index number of each widget in the group and add it to the parameter you pass to getField, preceded by a period. Try this code:

 

// Conversion: 0.12 inches = 8.64 points
var newSize = 8.64;

// Iterate through all the fields in the document
for (var i = 0; i < this.numFields; i++) {
    // Get the field name
    var fieldName = this.getNthFieldName(i);
    
    // Get the field object
    var field = this.getField(fieldName);
    
    // Check if the field is a checkbox
    if (field.type === "checkbox") {
		var idx = 0;
		var widget = this.getField(fieldName+"."+idx);
		while (widget!=null) {
			// Get the current position of the checkbox
			var rect = widget.rect;
			
			// Calculate the center of the checkbox
			var centerX = (rect[0] + rect[2]) / 2; // Horizontal center (average of left and right)
			var centerY = (rect[1] + rect[3]) / 2; // Vertical center (average of bottom and top)

			// Calculate the new left, right, top, and bottom values
			var newLeft = centerX - newSize / 2;
			var newRight = centerX + newSize / 2;
			var newBottom = centerY - newSize / 2;
			var newTop = centerY + newSize / 2;

			// Apply the new size while maintaining the center position
			widget.rect = [newLeft, newBottom, newRight, newTop];
			idx++;
			widget = this.getField(fieldName+"."+idx);
		}
    }
}
app.alert("All checboxes changed to 0.12 inches")

 

Anantha Prabu G
Legend
October 10, 2024

@try67 

 

Thank you so much for helping me with the PDF scripting!

Thanks,PrabuDesign smarter, faster, and bolder with InDesign scripting.
Nesa Nurani
Community Expert
Community Expert
October 10, 2024

Script looks ok, how did you create checkbox fields?

Can you share a file?

Anantha Prabu G
Legend
October 10, 2024

@Nesa Nurani 

 

Thank you for your response. 

Thanks,PrabuDesign smarter, faster, and bolder with InDesign scripting.