Printing page ranges quickly with JavaScript

If you want to print ranges of pages from Acrobat, your options are a somewhat limited. You have the choice of setting a single, continuous page range (e.g. pages 1-10), all even pages, or all odd pages. While this may be sufficient for most purposes, what can you do if you need to print multiple page ranges? One such situation would be if you needed to print out several non-sequential passages from a textbook or manual, say, chapters 1, 4, 5, 7 and 10. Using Acrobat’s native functionality, that would require the execution of at least 4 separate print commands (chapter 1, chapters 4 & 5, and another each for chapters 7 and 10.)

With the help of Acrobat JavaScript, there’s a much easier way — I’ve included a JavaScript sample I wrote to simplify the process of printing page ranges from Acrobat. The great thing about JavaScript is that once the code is written, you can re-use it as much as you like. Using this sample on the document creation side, you could add a ‘Print’ button that ran the JavaScript, making life easier for viewers — even those using the free Reader. On the document consumption side, you could either execute it individually using Acrobat Professional’s JavaScript debugger or add it as a custom toolbar button in Acrobat Standard or Professional if you have our ARTS PDF Aerialist plug-in.

Note:
Although I originally wrote this to be executed via a custom tool button using ARTS PDF Aerialist, you can also execute it from Acrobat Professional’s JavaScript debugger.


/*
Title: PRINT USER DEFINED PAGE RANGE
Author: ARTS PDF, Sean Stewart
Purpose: Prompt user to enter required page
range to print, e.g. 1-5,7,9-20
*/


//Get user response

var cResponse = app.response({
cQuestion: 'Enter in the pages you wish to print, e.g. 1,5,10-19,25',
cTitle: 'Print',
cDefault: '',
cLabel: 'Pages:'
});

if ( cResponse == null) {
app.alert('No pages entered');
} else {
var strInput = cResponse;
var strChar;
var arPrint = new Array(10);
var arCount = 0;
arPrint[arCount] = '';

for (var i = 0; i < strInput.length; i++){

strChar = strInput.substr(i,1);

//Check character and form page group
if (IsInteger(strChar) == 0){
arPrint[arCount] = arPrint[arCount] + strChar;
}

if (IsDash(strChar) == 0){
arPrint[arCount] = arPrint[arCount] + strChar;
}

if (IsComma(strChar) == 0){
arCount++;
arPrint[arCount] = '';
}

}

for (i=0;i<(arCount+1);i++){

if (arPrint[i].indexOf('-') > 0){
var dashPos;
dashPos = (arPrint[i].indexOf('-'));

var pageStart = arPrint[i].substr(0,dashPos);
var pageEnd = arPrint[i].substr(arPrint[i].indexOf('-')+1,
(arPrint[i].length - dashPos+1));

this.print({
bUI: false,
nStart: pageStart - 1,
nEnd: pageEnd - 1,
bSilent: true
});

} else {
this.print({
bUI: false,
nStart: arPrint[i] - 1,
nEnd: arPrint[i] - 1,
bSilent: true
});
}
}
}

function IsComma(strChar){

if (strChar == ',') {
return 0;
} else {
return -1;
}
}

function IsSpace(strChar){
if (strChar == ' ') {
return 0;
} else {
return -1;
}
}

function IsDash(strChar){
if (strChar == '-') {
return 0;
} else {
return -1;
}
}

function IsInteger(strChar){
if (strChar >=0 || strChar <= 9) {
return 0;
} else {
return -1;
}
}

You May Also Like

About the Author: Sean Stewart

Leave a Reply