These forums are now Read Only. If you have an Acrobat question, ask questions and get help from one of our experts.

Help with extract pages function

jcallis
Registered: Jul 26 2006
Posts: 22
Answered

Hi,
 
I've slightly modified a script from another post here. It is mostly working but I need to know how to change two things about it. Not knowing a lot about scripting any help would be appreciated.
 
Currently it is setup to extract pages in 100 page increments. What I need to know is how to make it get the last group of pages when the overall page count is not divisible by 100.
 
Also need to base the new file name on the original document file name the pages are being extracted from. So instead of using the _Extract shown below in var filename it would use the name of the source file minus the extension.
 
So if I had a file named acrobat pages.pdf with 420 pages the result would be 5 extracted files with the first 4 containing 100 pages and the 5th containing 20 pages. The files would be named:
 
0001_acrobat pages.pdf
0002_acrobat pages.pdf
0003_acrobat pages.pdf
0004_acrobat pages.pdf
0005_acrobat pages.pdf
 
Thanks for any direction that you can provide.
  
Below is the script I'm currently working with.
  
/* EXTRACT IN 100'S */
var filename = "_Extract";
 
var nihe=1;
 
try {
for ( var i = 0; i < this.numPages; i+=100)
this.extractPages
({
nStart: i, nEnd: i + 99,
cPath: util.printf("%04d",i/100+1) +filename +".pdf"
});
} catch (e) { console.println("Aborted: " + e) }

My Product Information:
Acrobat Pro 9.4.3, Macintosh
try67
Expert
Registered: Oct 30 2008
Posts: 2398
Change the code inside the for-loop to this:

var lastPage = i + 99;
if (lastPage>=this.numPages)
lastPage = this.numPages-1;

var filePath = util.printf("%04d",i/100+1) + "_" + this.documentFileName;

this.extractPages ({
nStart: i,
nEnd: lastPage,
cPath: filePath
});

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

jcallis
Registered: Jul 26 2006
Posts: 22
When I try the above as:

/* EXTRACT IN 100'S */
var filename = "_Extract";

var nihe=1;

try {
for ( var lastPage = i + 99;
if (lastPage>=this.numPages)
lastPage = this.numPages-1;

var filePath = util.printf("%04d",i/100+1) + "_" + this.documentFileName;

this.extractPages ({
nStart: i,
nEnd: lastPage,
cPath: filePath
});

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



Acrobat gives me a syntax error at line 9 when trying to get our of the javascript editor window.
Maybe I'm not putting your code in the right spot.
Any ideas what I need to change?

Thanks
try67
Expert
Registered: Oct 30 2008
Posts: 2398
Accepted Answer
No, leave the for-loop as it was...

try {
for ( var i = 0; i < this.numPages; i+=100) {
... my code
}
} catch (e) { console.println("Aborted: " + e) }

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

jcallis
Registered: Jul 26 2006
Posts: 22
Awesome. That does the trick.
Really appreciate the help.

Here is the complete working script . . .

/* EXTRACT IN 100'S */
var filename = "_Extract";
var nihe=1;
try {
for ( var i = 0; i < this.numPages; i+=100) {
var lastPage = i + 99;
if (lastPage>=this.numPages)
lastPage = this.numPages-1;
var filePath = util.printf("%04d",i/100+1) + "_" + this.documentFileName;
this.extractPages ({
nStart: i,
nEnd: lastPage,
cPath: filePath
});
}
} catch (e) { console.println("Aborted: " + e) }
try67
Expert
Registered: Oct 30 2008
Posts: 2398
PS - you can remove the first two lines. Those variables are not used anywhere.

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com