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

Batch Extract multiple pages from pdf

steveald
Registered: Jun 29 2010
Posts: 15
Answered

I found the following code to use in creating a Batch Sequence to extract pages from a multi-page pdf file.

It works fine, but it creates a separate file for each single page, naming them Extract001.pdf, Extract002.pdf, etc.

I need to know how to modify this code so the Batch Processing Sequence will save 3 pages at a time per file (1st file is pages 1-3 of the original pdf, 2nd file is pages 4-6, etc.).
  
==Begin==
  
var filename = "Extract";

var nihe=1;

try {
for ( var i = 0; i < this.numPages; i++)
this.extractPages
({
nStart: i,
cPath: filename+ util.printf("%03d",i+nihe) +".pdf"
});
} catch (e) { console.println("Aborted: " + e) }
  
==End==
  
Can someone help?

Thank you.

My Product Information:
Acrobat Pro 8.1.7, Macintosh
DaveyB
Registered: Dec 10 2010
Posts: 70
Accepted Answer
The doc.extractPages() function contains your answer :) There are 3 parameters passed to it:

* 1st page to be extracted.
* Last page to be extracted.
* Name of the file to be generated, which these pages will be placed into.

The scripting simply needs to be adjusted to increment in steps of 3 instead of steps of 1, so an additional variable (other than 'i') is required to keep track of the page numbers. Don't forget to keep checking for EOF (end of file) in case the number of pages is, for some reason, not a multiple of 3.

The tutorial on this site may be very useful to you!Hope that helps!

DaveyB

LiveCycle Designer 8.0
"Genius is one percent inspiration, ninety-nine percent perspiration." ~~ Thomas Edison
"If at first you don't succeed, get a bigger hammer." ~~ Alan Lewis
"If the conventional doesn't work, try the unconventional" ~~ DaveyB

steveald
Registered: Jun 29 2010
Posts: 15
Thanks DaveyB,

The tutorial and your comments helped. Now I'm halfway there. With the following code modification, it now saves 3 pages at a time. (Replaced "nStart:i," with "nEnd:2,")

var filename = "Extract";

var nihe=1;

try {
for ( var i = 0; i < this.numPages; i++)
this.extractPages
({
nEnd:2,
cPath: filename+ util.printf("%03d",i+nihe) +".pdf"
});
} catch (e) { console.println("Aborted: " + e) }

Only it keeps saving the same 3 pages. I need to change something so that it moves on to page 4 and saves those 3 pages, then to page 7, and so on.

Thoughts?
gkaiseril
Online
Expert
Registered: Feb 23 2006
Posts: 4308
How are you incrementing the variable "nihe"?

You will also need to figure when you reach the end of your extraction.

Have a look at the code for "Creating a cut-and-paste automation script" for a possible way to extract a fixed number of pages from a multi page document.

George Kaiser

steveald
Registered: Jun 29 2010
Posts: 15
That did the trick! A closer look at the second half of the linked article (and some trial-and-error) revealed the changes I needed to make. Specifically, "i+=3" and "nStart: i, nEnd: i + 2,". Then it was just a matter of finding the correct calculation for the filenames to number incrementally: 001, 002, etc.

George, I'm not familiar enough with coding to answer your question. As for the end of the extraction, much like the 1040 example in the article, I know the original file will always have a total number of pages evenly divisible by 3. So I'm not worried about the EOF in this case.

Here's the working script:


var filename = "Extract";

var nihe=1;

try {
for ( var i = 0; i < this.numPages; i+=3)
this.extractPages
({
nStart: i, nEnd: i + 2,
cPath: filename+ util.printf("%03d",i/3+1) +".pdf"
});
} catch (e) { console.println("Aborted: " + e) }