Skip to main content
scotts55494258
New Participant
March 10, 2016
Question

JS to Extract Multiple Pages and Name as Page Label

  • March 10, 2016
  • 2 replies
  • 10320 views

Good Morning! We have a group of employees that often taken a PDF that contains multiple pages (in the range of 200-500 pages) and extract them out into separate PDF's to load into a system. Often times, these large PDF's that they begin with hold page labels for each and every page, but when extracting, they lose these page labels and the individual files are named differently. What we're aiming to do is have a javascript that they can run to extract all pages out of a multi-paged PDF and in that process, it name the individual PDF files the same name as their page label.

I believe the two functions that we're dealing with are Doc.getPageLabel() and Doc.extractPages() but we're unsure as to how to tie this into a javascript that will do what we need it to do. Unfortunately, none of us have any experience with JS. I appreciate any and every ones help! Thank you!

2 replies

JR Boulay
Community Expert
Community Expert
April 1, 2025

[MOVED TO THE ACROBAT DISCUSSIONS]

Acrobate du PDF, InDesigner et Photoshopographe
try67
Community Expert
Community Expert
March 10, 2016

From what context do you want to use this script? From a menu item? An Action? The JS Console?

Basically you have it right. The only thing missing is a loop that iterates over all the pages, extracting each one using its label.

The basic code would be something like this:

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

    this.extractPages(p, p, this.path.replace(this.documentFileName, this.getPageLabel(p) + ".pdf"));

}

Known Participant
May 20, 2024

Wow, that is what i was looking for.

Can you write it in this way please:
filename.pdf to become filename___pagelabel.pdf

Thank you so much.

try67
Community Expert
Community Expert
October 2, 2024

I need some clarification on the steps to extrac the pages and keep the page labels.  Here is what i am doing, is this correct?

1) Open Pdf

2) Select "Organize Pages"

3) Highlight the pages i want to extract

4) Use Control J to pen up the Java window

5) input the script (see below) 

for (var p=0; p<this.numPages; p++)
{
this.extractPages(p, p, this.path.replace(this.documentFileName, this.getPageLabel(p) + ".pdf"));
}

6) highlight the script and use CTRL Enter to run it.

I get the follwoing error:

RaiseError: The file may be read-only, or another user may have it open. Please save the document with a different name or in a different folder.
Doc.extractPages:3:Console undefined:Exec
===> The file may be read-only, or another user may have it open. Please save the document with a different name or in a different folder.

undefined

Below is a screenshot of the whole page.  I have tried on multipole pdf's and get the same issue. What am i doing wrong?

 

 

 


Yes, that's it. It should work... Make sure the file is not located in a special folder, though, like the root drive folder (C:\), or something like C:\Windows, or a network folder. Also make sure you have full read/write permissions to that folder.