Skip to main content
saint_francish32929554
Participant
February 1, 2017
Answered

Split PDF using an Action with Java Scipt.

  • February 1, 2017
  • 2 replies
  • 866 views

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 has been closed for replies.
Correct answer try67

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: "..."

    });

}

2 replies

Legend
February 1, 2017

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

try67
Community Expert
try67Community ExpertCorrect answer
Community Expert
February 1, 2017

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: "..."

    });

}

saint_francish32929554
Participant
February 1, 2017

Thank you- worked perfectly.