When 'Create Multiple Copies' Runs Out of Space
Use this trick to create multiple copies of fields across multiple pages.
I notice a lot of questions on the Acrobat Users Forum have examples of fields in rows where the field names contain numbers without periods.
Numbered fields like this are handy for writing scripts that loop through all the fields containing the non-number part of the field name and do something to those fields. For example, you could format the dates for all the date fields, or add a border to all the tax fields, or use the fields in a calculation without naming them all individually in your script. The screenshot above only contains three rows so it’s not a big deal to name the fields in your script, but imagine if there were 25, or 50, or any other number higher than 50. It’s a lot of work that is prone to errors.
The following scripts loop through the fields and do something with each field (assumes 25 fields):
// 1. Format the Date fields
for(var i=1;i<=25,i++)
{
this.getField("Date"+i).setAction('Format','AFDate_Format("mm/dd/yyyy")');
this.getField("Date"+i).setAction('Keystroke','AFDate_Keystroke("mm/dd/yyyy")')
}
// 2. Change the border color of the Tax fields to black
for(var i=1;i<=25;i+++)
{
this.getField("Tax"+i).borderColor=color.black;
}
/* 3. Count how many Amount fields contain a value and display that total in a field*/
var count=0;
for(var i=1;i<=25;i++)
{
if(this.getField("Amount"+i).value)
{count++;}
}
event.value=count;
To modify the scripts above for a different number of rows, change 25 to the number of rows.
A Lot of Work
The issue with field names described so far is that they have to be named manually by individually typing in the field names (unless you write an automation script, which in itself is a lot of work because you have to determine the coordinates for each field). The solution is Right-click > Create multiple copies. While in form editing mode, simply right click a field or select an entire row and right-click one of the fields, select Create multiple copies, then enter the number of fields you require in the down field.
The first field or row of fields will be renamed with a suffix of .0 and the created fields will be named with suffixes .1 through .24 (when creating 25 fields down). The following screenshot shows the first three rows:
For the three scripts in the previous code block, a few changes need to be made:
Periods will need to be added to all instances of Date, Amount, and Tax (Example: this.getField("Date."+i) ).
The numbers in the first line of the loop will need to be adjusted from 1 to 0, and 25 to 24 (Example: for(var i=0; i<=24; i++) ).
An alternative to the second point above is to create 26 rows instead of 25 and then delete the first row containing the .0 suffixes. This leaves 25 rows with suffixes .1 through .25, making it easier to write scripts with the row numbers that are consistent with their suffixes. In this case the first line of the loops in the code block does not need to be changed.
Style and Format of The Multiple Copies
When creating multiple copies the created fields are exact copies of the original fields and retain their properties (except name and location on the page). Properties like font size, border color, alignment, format, etc. should be verified first and will save having to adjust the properties of multiple fields after the face. Some properties can be changed by selecting multiple fields and opening the properties, but others cannot (see Widgets post). Another advantage of creating fields using Create multiple copies is that you can change the properties of all fields with root name before the decimal without looping through them. For example, if you have fields Tax.1 through Tax.200, you can change the border color to black by simply using the root field name like this:
this.getField("Tax").borderColor=color.black;
This is not possible when the fields don’t contain a period to separate the root name from the suffix.
One Calculation
You can add a custom calculation that will work in all field when Create multiple copies is used by anticipating the new field names and splitting the part before the suffix out of the name to obtain the row number and using this variable in the remainder of the script (see post referenced below).
Running Out of Space Issue
What if your multiple copies span more than one page? In the screenshot above, 200 fields or rows are required, but only 49 will fit on a page. That means 5 pages are required. Not only is there no way to force Create multiple copies to continue onto subsequent pages, there’s no way to use the last field (field 49) on the next page to repeat Create multiple copies. That’s because field 49 will have a period and a suffix (Example: Date.48). If Date.48 is copied and pasted to the next page and the Create multiple copies function is used on it, a suffix will be added on to the suffix (Example: Date.48.0, Date.48.1, Date.48.2, etc.) This pattern will mess any scripting loop and force you to created multiple loops inside other loops.
Running Out of Space Solution
The solution to this problem is to create a fresh one-page PDF that can fit all 200 fields down, add the first row of fields, use Create multiple copies, then copy and paste one page of fields from the newly created document to the document in question. To create the new document, simply run the app.newDoc( ) method in the console.
The method takes two optional input parameters in points. There are 72 points per inch. Without any input parameters the method will produce a standard one-page PDF that is 8.5 inches wide and 11 inches high (612 points by 792 points).
To create a page long enough to fit 200 fields or rows down, simply include the input parameters and make the height five times higher than than a standard size page (3960 points). You don’t need to use your calculator, you can let the script do the math for you:
app.newDoc(612, 792*5);