Tips & Tricks

Printing page ranges quickly with JavaScript

December 15, 2004

Advertisement
Advertisement
 

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;
    }
}

(ARTS PDF and Planet PDF have been since acquired by Debenu)

PDF In-Depth Free Product Trials Ubiquitous PDF

Debenu Quick PDF Library

Get products to market faster with this amazing PDF developer SDK. Over 900 functions and an equally...

Download free demo

Back to the past, 15 years ago! Open Publish 2002

Looking back to 2002, it's amazing how much of the prediction became a reality. Take a read and see what you think!

September 14, 2017
Platinum Sponsor





Search Planet PDF
more searching options...
Planet PDF Newsletter
Most Popular Articles
Featured Product

Debenu PDF Aerialist

The ultimate plug-in for Adobe Acrobat. Advanced splitting, merging, stamping, bookmarking, and link control. Take Acrobat to the next level.

Features

Adding a PDF Stamp Comment

OK, so you want to stamp your document. Maybe you need to give reviewers some advice about the document's status or sensitivity. This tip from author Ted Padova demonstrates how to add stamps with the Stamp Tool along with related comments.