JavaScript Loops
JavaScript loops are very handy for repeating scripts, counting things, and automating processes. They can be executed easily from the JavaScript console in Acrobat Pro. The variable in a loop is incremented (or decremented) to determine how many iterations should be performed. Here’s a simple loop that can be run from the console to return the numbers 1 through 10:
for(var i=1;i<11;i++)
{
console.println(i);
}
Inside the brackets of the first line are three statements (left to right) separated by semi-colons:
A declaration of the variable i and an assignment of its value as 1. The variable doesn’t need to be i, but it is commonly used in loops (for “incrementor”).
A Boolean expression (returns true or false). As long as i is less than 11 (returns true), the loop continues.
An incrementor for the variable. ++ increments the variable i by 1. It could increment by any number (i+=2, i+=5).
The script inside the { } runs once for each iteration. { } is not necessary for a one-line script, but it is my habit to include it for consistency. The script could also be written like this:
for(var i=1;i<11;i++)
console.println(i);
Save $100
Adobe Acrobat PRO is a PDF reader, writer, and fillable form creator loaded with functionality for creating PDFs and automating PDF-related workflows. It has a very robust JavaScript engine which, when properly utilized, can achieve remarkable results.
“How can I develop this course in a way that will get beginners up to speed as quickly as possible while they retain their knowledge and learn how to find answers quickly on their own?”
-David Dagley, founder of PDF Automation Station and course developer
The course:
One year of access
Work at your own pace
Practical lessons, interactive PDFs, videos, and quizzes
Total of 54 lessons and quizzes
Questions answered personally by the course developer
Bonus premium membership to www.pdfautomationstation.com which includes several automation and download tools.
Enter the promo code JS-Substack and receive a $100 discount (regular price: $395)
Programming A Loop Script - Example
Suppose you have a 20-page PDF and and another 1-page PDF. You want to insert the page from the 1-pager between every page of the 20-pager, creating a 40-page PDF with the 1-pager on every other page, starting at page 2.
The this.insertPages( ) method will insert a page from one PDF into another. Since the script will access the file system, it will need to run from a privileged context (console, trusted function in a folder level script, or action). To ensure the script does not trigger any access denied errors, ensure the following setting under Edit > Preferences on the Acrobat Pro menu (or Ctrl +k) is not enabled. If it is you will have to restart Acrobat after unchecking it:
The this.insertPages( ) method will be placed inside a loop and iterated 20 times. The input parameters for this method are:
The 0-based index of the page after which to insert the source document page.
The path to the source document.
The starting 0-based page index of the range of pages to insert from the source document.
The ending 0-based page index of the range of pages to insert from the source document.
For example, the following script will insert the first page of the source document after the first page of the document referenced by the script:
this.insertPages(0, "/C/Users/The User/Documents/Source.pdf",0,0)
To obtain the path to the source document in the correct format, open the document and run the following script in the console:
this.path;
Now, to put the script into a loop, first define a variable for the path so it’s easier to work with. Then the only thing that would change is the first parameter of the insertPages method, the page after which to insert the page. That is because the page doesn’t change, nor does the page range from the source document. At first glance you might think the script would be:
var pth ="/C/Users/The User/Documents/Source.pdf";
for (var i=0; i<20; i++)
{
this.insertPages(i, pth, 0, 0)
}
It inserts a page after page 1, then increments i by 1, and inserts another page after page i (2), and so on, 20 times. This would work if adding pages didn’t change the page count. Once you add a page after page 1, it becomes page 2, and page 2 becomes page 3, and every page after advances by 1. This happens with each iteration. So instead of adding a page after every page to end up with the source document page on every other page, you end up with 20 pages of the source after page 1, and the remaining 19 pages after those.
The solutions is to add another variable representing the page index number of the document, and increment that as well. I will explain how to do this shortly, but first I’ll explain an easier way.
Reverse The Loop
If you add a page after the last page in the document, page 20 (0-base index page 19), then decrement the variable by one, the next page will be added after page 18, then after page 17, etc. In this case, the page numbers you are adding the pages after do not change their page number until after a page has already been added after them, so they no longer matter in the script. This is easily done by reversing the loop:
for (var i=this.numPages-1; i>-1; i--)
{
this.insertPages(i, pth, 0, 0)
}
The insert pages line does not change. However, i is defined as the last page of the document (this.numPages -1; //there are 20 pages, so subtract 1 to get the 0-based index of 19). Since the loop is going backwards (i-- decrements the number by 1), the i must not go below zero for the Boolean expression to return true (i > -1;) and continue the loop.
Another Way
There’s another way to do this, as mentioned previously, without reversing the loop. Simply define another variable that will represent the page number index before starting the loop, then increment that variable by 2 inside the loop:
var pg=-2;
for (var i=0; i<20; i++)
{
pg+=2;
this.insertPages(pg, pth, 0, 0);
}
The i represents the number of iterations, as in the previous scripts. pg represents the 0-based page index after which the page will be added and should be incremented by 2. i<20 could be replaced by i<this.numPages-1 so it will work with a document of any number of pages.
There’s a lot of cases where reversing the loop makes things easier, especially when dealing with page numbers and annotations, because when you loop through all of them and add or delete some, it can change the order of those remaining, causing errors.