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

Planet PDF and ARTS PDF are both divisions of BinaryThing.

Related Products at PDF Store

ARTS PDF Stamper

Insert text, page numbers, bates numbering, headers and footers to your PDFs. With ARTS PDF Stamper ... View full product details
Download free demo

ARTS PDF Split & Merge Lite

The easiest way to split and merge PDFs! It provides a simpler method of splitting and merging your ... View full product details
Download free demo

ARTS PDF Aerialist

Take Acrobat to the next level with advanced splitting and merging; flexible bookmark creation and m... View full product details
Download free demo

PDF In-Depth Free Product Trials Ubiquitous PDF

Nitro PDF Professional

the perfect PDF product for business and enterprise, combining an extremely competitive price with a...

Download free demo

XpdfViewer

This ActiveX control (OCX) provides a PDF file viewer component, enabling developers to add PDF viewing...

Download free demo

Ubiquitous PDF: PDF eBooks-Library

If you are looking for a good store of PDF content, you could do a lot worse than visiting eBooks-Library.com...

September 03, 2009
Search Planet PDF
more searching options...







Download PDF Creator

Download The Best of Planet PDF volume 2
Planet PDF Newsletter
Most Popluar Articles
Features

Collating PDFs using JavaScript

Despite the numerous benefits, there can be potential issues with the conversion of paper documents into electronic archives. When scanning paper pages into PDF, it's possible to end up with the odd- and even-numbered pages in separate PDF files. It can be very time-consuming to collate them manually, but there is an easier way. Sean Stewart explains.

Featured Product

BCL easyPDF SDK

BCL easyPDF SDK is a set of PDF Programming Libraries designed specifically to help Software Developers / Programmers build and deploy enterprise class PDF applications for corporate wide PDF...

Platinum Sponsor
Create & Edit PDF - Nitro PDF Software

ARTS PDF

Silver Sponsors

PDF-Tools enfocus

QuickPDF: The Unrivaled PDF Developer Toolkit