PDF: A button to add a row of text fields on fillable form

155 views Asked by At

The original thread: https://community.adobe.com/t5/acrobat-discussions/have-a-button-in-adobe-acrobat-that-adds-a-row-of-columns-then-adds-another-row-below-it/m-p/14250883#M439889

The script works to create fillable text fields, however I need to limit the number of rows it creates. Below is the javascript:

var myRows = new Array();
var rowCounter = 1;

function addRow() {

if (rowCounter >= 5) {
return;
}

myRows[rowCounter - 1] = "Row" + rowCounter + ".Column1";
myRows[rowCounter] = "Row" + rowCounter + ".Column2";
myRows[rowCounter + 1] = "Row" + rowCounter + ".Column3";
myRows[rowCounter + 2] = "Row" + rowCounter + ".Column4";


var w = 144;
var h = 18;
var wGap = 6;

for (var j = 0; j < 1; j++) {  // Adjust the number of rows per line (here set to 2 for 4 columns)

    var coordinates = [10, 700 + (rowCounter * 24)]; // Adjust the vertical position for each new row
    var afields = [coordinates[0], coordinates[1], coordinates[0] + w, coordinates[1] - h];

    for (var i = 0; i < myRows.length; i++) {
        this.addField(myRows[i], "text", 0, afields);
        afields[0] += (w + wGap);
        afields[2] += (w + wGap);
    }
    rowCounter += -1;
}
}

The button functions under actions: Mouse Up > Execute a Menu Item > 'Run a javascript':

addRow();

Right now it seems to create an infinite number of text field and need to limit the rows to at least 5 rows. I would like to also add a button that would delete row(s) if too many is created. Is this possible?

1

There are 1 answers

1
Max Wyss On

Yes, you can remove fields. The appropriate Doc Object method is called

removeField()

To remove the last row of fields, find out its rowCounter value, and use this method for the according fields. As you have a separator in the field name, it may be sufficient to just pass "Row" + rowCounter as argument instead of deleting the individual fields.

FWIW, a little advice: Because the number of rows value should not rely on a value in memory, you might add a hidden field where you store the rowCounter value, and update it every time when you do some modifications with the rows.