Automating Page Actions

In a previous tip — Using Page Actions to Trigger Events in PDFs — I showed you how to add Actions (either Closing or Opening actions) to a Page in your PDF document. This tip will show you how to add actions to every page in your PDF document – in batch!

I recently published a large PDF document with different sized pages (A4, A2, A0 and even A6!) and what I found was that when I was navigating around the document I had to change the Zoom to suit each page – which quickly became a real pain.

So this tip sets out to solve my problem by adding a Page Action to each page in the PDF document to set the zoom to 100% (Actual) regardless of the current setting. Now normally I wouldn’t recommend this practice however in my case I wanted a consistent page scrolling mechanism.

To accomplish such a feat we need to use a little bit of Javascript that will whip round each page in the PDF check the Width and Height and then add the appropriate Page Action Javascript.

The process is quite straightforward and is outlined as follows:

  1. Enter a Loop based on the Page Count
  2. Add the Javascript to the Page Action for each Page

All that’s required is the following lines of code:

var pageCount = this.numPages;
for(var x = 0;x<pageCount;x++)
setPageAction(x,"Open", "this.zoom = 100;");

Note: The reserved words Acrobat uses for built-in properties can often be the first name you want to use when creating variables. Recently I got caught by creating a variable called ‘numPages’ which is of course already in use by Acrobat/Javascript – no error was raised so it took a while before I cottoned on. 😉

To make this script truly useful we can create a new Batch Sequence that can run this javascript against loads of PDF files at once.

From the Advanced Menu choose ‘Batch Processing…’.

Now select ‘New Sequence’ and enter an appropriate name for this sequence.

We need to scroll down the list and find the sequence named: ‘Execute Javascript’, once you’ve clicked ‘Add’ double click the entry on the Right hand panel which will display the Script window. Copy and Paste the Javascript from above (or from the optional code below) into the Window.

Click OK twice, and choose the ‘Browse’ button to set the path on which the Sequence is to operate on. Choose any other Sequence options and select OK. Once your happy with the settings etc, you can ‘Run’ the Sequence.

You’ll also notice if you’ve used the optional code that the bottom of Acrobat indicates the current Page number and the file being operated on.

Optional: I’ve created another version of the Javascript which shows up a progress bar at the bottom indicating the current page number and the file being operated on.

// Get the Thermometer Object
var thermo = app.thermometer;

var pageCount = this.numPages;

// Set the Max value for the Thermometer
thermo.duration = pageCount;

// Start the Thermometer

for(var x = 0;x<pageCount;x++)
// Set some status text in the Thermo
thermo.text = "[" +this.documentFileName+ "]
Processing Page: " +(x+1);
this.setPageAction(x,"Open", "this.zoom = 100;");

// Update the Value in the Thermometer display
thermo.value = x;

// All done
thermo.text = "Processing Complete.";

To recap we’ve actually touched on three important areas: Batch Sequences, Thermometer and Javascript inside Page Actions. I hope this gives you some food for thought.


Note: When copy pasting code into the Javascript Window make sure the Quotes are copied as straight quotes and not smart quotes or curly quotes. To correct problems simply change all curly quotes to Straight quotes inside the Javascript Window.

You May Also Like

About the Author: Dave Wraight

Leave a Reply