Skip to main content
Known Participant
April 13, 2018
Answered

Script to Extract Pages

  • April 13, 2018
  • 4 replies
  • 8800 views

I'm currently trying to write a JavaScript to Extract pages from PDF files and save those extracted pages as a separate document.

Say I have a 100 Page document, I wish to extract 30 Pages, now the first 90 should each be 3 separate 30 page documents, now when I get to last 10 I wish to save those 10 pages  as another document.  What's the best way to ensure this in a Script?

[Here is the list of all Adobe forums... https://forums.adobe.com/welcome]

[Moved... Encore is to author a DVD and has nothing to do with a PDF... Mod]

This topic has been closed for replies.
Correct answer try67

Change the inside of the for-loop to:

var firstPage = i;

var lastPage = (((i+29)>=this.numPages) ? this.numPages-1 : (i+29));

this.extractPages({

    nStart: firstPage, nEnd: lastPage,

    cPath: "/F/temp/"+filename+"_" + (firstPage+1) +"-" + (lastPage+1) +".pdf"

});

4 replies

Known Participant
April 13, 2018

Thank you that fixed that bug, I have one last thing, very minor but will make it easier to know which set of files were saved.  How can I have the script save what pages are in the document.  So I the first document is saved as img-23456_0.pdf I want the file to say img-23456_Pages_1-30.pdf

try67
Community Expert
try67Community ExpertCorrect answer
Community Expert
April 13, 2018

Change the inside of the for-loop to:

var firstPage = i;

var lastPage = (((i+29)>=this.numPages) ? this.numPages-1 : (i+29));

this.extractPages({

    nStart: firstPage, nEnd: lastPage,

    cPath: "/F/temp/"+filename+"_" + (firstPage+1) +"-" + (lastPage+1) +".pdf"

});

Known Participant
April 13, 2018

Alright Thank You, just one last thing I require, say I have a 35 page document, I still want to save the 30 pages as their own document, how could I have the script save the leftover pages as their own document?

try67
Community Expert
Community Expert
April 13, 2018

Yeah, I thought that might be an issue... Change this:

nEnd: i+29

To:

nEnd: (((i+29)>=this.numPages) ? this.numPages-1 : (i+29))

Known Participant
April 13, 2018

Thank You, I created a script to extract every page and save them, so how would I modify it to extract every 30?

/* Extract pages to folder */

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

   var re = /\.pdf$/i;

   // filename is the base name of the file Acrobat is working on

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

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

         this.extractPages({

            nStart: i,

            cPath: "/F/temp/"+filename+"_" + i +".pdf"

         });        

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

try67
Community Expert
Community Expert
April 13, 2018

Change:

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

To:

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

And:

nStart: i,

To:

nStart: i, nEnd: i+29

Thom Parker
Community Expert
Community Expert
April 13, 2018

Pages are extracted with the Doc.extractPages() function. Here's the entry for it in the Acrobat JavaScript Reference:

Acrobat DC SDK Documentation

It has inputs for start page, end page, and a path for saving the file.

Acrobat uses Device Independent File Paths. Here'a an article that covers the topic:

https://acrobatusers.com/tutorials/file-paths-acrobat-javascript

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often