Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Script to Extract Pages

Community Beginner ,
Apr 12, 2018 Apr 12, 2018

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]

TOPICS
Acrobat SDK and JavaScript
8.6K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Apr 13, 2018 Apr 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"

});

Translate
Community Expert ,
Apr 12, 2018 Apr 12, 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 PDFScripting
Use the Acrobat JavaScript Reference early and often

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Apr 13, 2018 Apr 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) }

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 13, 2018 Apr 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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Apr 13, 2018 Apr 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?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 13, 2018 Apr 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))

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Apr 13, 2018 Apr 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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 13, 2018 Apr 13, 2018
LATEST

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"

});

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines