javascript to split PDF into 5 page segments and save & rename
top of page

javascript to split PDF into 5 page segments and save & rename


You can use javascript code in Adobe Acrobat to extract set ranges of pages from a PDF file and then save and rename each extracted PDF range as a new file.


A user named vvb posted the following code here:


/* Extract Pages to Folder */var re = /.*\/|\.pdf$/ig; var filename = this.path.replace(re,""); var lastPage=this.numPages-1; { for ( var i = 0; i < this.numPages; i = i + 2 ) this.extractPages ({ nStart: i, nEnd: i + 1, cPath : filename + "_page_" + (i+1) + ".pdf" }); };


This code can be inserted in Acrobat and edited so that it takes 5 pages at a time from a source PDF file and then names them sequentially with a prefix.


In Acrobat go to More Tools and select Action Wizard. On the top toolbar select the option for 'New Action'. In the 'More Tools' menu select the 'Execute Javascript' option - doubleclick on it so it appears in the right pane.


Uncheck the 'Prompt User' option, and then click on 'Specify Settings'.


Edit the code so that the line beginning, " for ( var i = 0; i < this.numPages; i = i + " specifies how many pages you want each PDF to be., and the line beginning "nEnd: i + " ends with one number less. Modify the line beginning, " cPath : " so that it has the letter prefix for each file. The script will name each extracted file with the page number from the original file that the excerpt begins with.



/* Extract Pages to Folder */var re = /.*\/|\.pdf$/ig;


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

var lastPage=this.numPages-1;

{

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

this.extractPages

({

nStart: i,

nEnd: i + 4,

cPath : "ACME" + (i+1) + ".pdf"

});

};




Save and name the action.



Click on the action in the Actions List and then select the files that you want to run it on. Click the 'Start' button.



The script will create a new file (in the same folder as the source file(s)) named with the prefix you enter in the code and the page number on which the excerpt begins.



bottom of page