How To Create A Progress Bar in Acrobat
Also known as the thermometer object in the Acrobat application, it let's the user known that something is happening and the amount of progress that has been completed so far.
In my last article, JavaScript: Reverse Loops I used a simple example of a loop that could be easily run in the console. It’s a good idea to run this code right now to see the result:
for(var i=1;i<11;i++)
{
console.println(i);
}
The script above returns the numbers from 1 through 10, each on a separate line. Because the list is so short, the result should appear instantly. With more complex automation projects, the results can take a while to appear, leaving the user wondering if anything is “happening”. While the script is running the user won’t be able to do anything else with the application. The more steps in the script, and the more iterations in the loop, the longer it takes for the final result. Instead of running the following script in the console, copy and paste it as a mouse up action in a button field and notice how long it takes for the console to open with the results after the button is clicked:
console.clear();
for(var i=1;i<2001;i++)
{
console.println(i);
}
console.show();
Instead of running 10 times, it runs 2,000 times. The user might wonder if anything is happening.
That’s Where The Thermometer Comes In
The thermometer appears as a progress bar in the bottom left corner of the Acrobat window as pictured in the image at the top of this post. It’s actually quite simply to program:
The thermometer is acquired and assigned a variable. var t = app.thermometer;
The duration is assigned a number representing the full thermometer. t.duration=500;
The thermometer is initialized. t.begin( );
The loop is initialized. for (var i=1; i<=500; i++) {
The thermometer value is set to the loop variable. t.value=i;
The thermometer text (if any) to display is set inside the loop, using the loop variable if necessary. t.text = "Writing number " + i + " of 500 to the console.";
The loop script follows and the loop is closed. console.println(i); }
Putting It All Together
Copy the following script and paste it into the mouse up action (run a JavaScript) of a button field on a PDF form. The script clears the console, runs the script with the thermometer, then opens the console:
console.clear();
var t=app.thermometer; //Acquire the thermometer object
t.duration = 500;
t.begin();
for(var i=1;i<=500;i++)
{
t.value=i;
t.text= "Writing number " + i + " of 500 to the console."
console.println(i);
}
t.end();
console.show();
Getting Fancy
I like place my the text of my thermometer in between 2 horizontal lines, and also brand the thermometer with www.pdfautomationstation.com. To do this, simply add a couple of lines of underscores to the text of the thermometer, separated by line breaks (\r). I also like a moving percentage of the completed progress. This can be done with a simple math equation that divides the duration into the loop variable. Replace the button script with the following and try again:
console.clear();
var t=app.thermometer; //Acquire the thermometer object
t.duration = 500;
t.begin();
for(var i=1;i<=500;i++)
{
t.value=i;
t.text="______________________________________________________________\r"+
"Writing number " + i + " of "+ 500 +" ("+Math.round(i/500*100*100)/100+ "%) to the console.\r"+
"______________________________________________________________\r"+
"www.pdfautomationstation.com";
console.println(i);
}
t.end();
console.show();
If you want to show your appreciation for my work you can buy me a coffee:
Just a reminder that today is the last day to save $195 on my JavaScript course for Acrobat by entering the promo code JS-Substack.