Skip to main content
saint_francish32929554
Participant
Feb 1, 2017 3:44 PM UTC
Answered

Split PDF using an Action with Java Scipt.

Our company needs to split mulitple pdf files.  The files must = 25 pages or less after spliting.  For example 102 page file would output 5 files.  4=25pages, and 1=2 remaining pages. I have a script but it only outputs 2 pages.

/*Extract pages 25 at a time with leading zerors for proper sort */

function padLeft (s,len,c){

c=c || '0';

while (s.length< len) s= c+s;

return s;

}

// regular expression used to acquire the base name of file

var re = /\.pdf$/i;

//file name is the base name of the file acrobat is working on

var filename = this.documentFileName.replace (re,"");

try {for (var i = 0; (i*2) < this.numPages; i++)

this.extractPages({

nStart: i * 2,

nEnd: (i * 2) + 1,

cPath: filename + "-" + padLeft ((i + 1) + "",3) +".pdf"

});

} catch (e) { console.println("Aborted: " + e)}

This topic is closed to new replies. Start a new post to keep the conversation going.
try67
try67Correct answer
Community Expert
Feb 1, 2017 5:16 PM UTC

This code should work better:

for (var i=0; i<this.numPages; i+=25) {

    var startPage = i;

    var endPage = i+24;

    if (endPage>=this.numPages)

        endPage = this.numPages-1;

    this.extractPages({

        nStart: startPage,

        nEnd: endPage,

        cPath: "..."

    });

}

3 replies

Legend
Feb 1, 2017 5:16 PM UTC

That's what it is written to do. Adjust the loop counters and the nStart and nEnd.

Translate
try67
Community Expert
try67Community ExpertCorrect answer
Community Expert
Feb 1, 2017 5:16 PM UTC

This code should work better:

for (var i=0; i<this.numPages; i+=25) {

    var startPage = i;

    var endPage = i+24;

    if (endPage>=this.numPages)

        endPage = this.numPages-1;

    this.extractPages({

        nStart: startPage,

        nEnd: endPage,

        cPath: "..."

    });

}

Translate
saint_francish32929554
Participant
Feb 1, 2017 6:12 PM UTC

Thank you- worked perfectly.

Translate