javascript ExtractPages and SaveAs

Each month I get a bunch .ps file that are numerous pages longer.
I need to convert these to PDF (easily done through a Distiller watched folder) then extract each page and save as individual pages in a folder that uses part of the original file name as its path.
ie London_PS_date.pdf - would go to C:/Converted_PDFs/London/London_PS_date_1.pdf (and page 2 would end with a 2 and so on)

I'd like this to happen automatically just like the watched folder, but if its not possible, I will run it from Acrobat Pro 9 - Advanced - Document Processing - Batch Processing - Run Script.

Any help would be very much appreciated.

Thanks


Wayne Lee


15 Answers

To do this automatically you will need to write an external application, which is possible (I've done several such tools in the past), but more complex than writing a script for Acrobat.

If you want to do this in a batch process, you just use a loop to go over all the pages of the document, and then the extractPages command to save them to the desired path. You don't need to use saveAs, actually.
From what you describe it seems that you can split the file-name using the underscore and then use the first part as the folder name.

-------------------------------------
Visit my custom-made PDF scripts website: http://try67.blogspot.com
Contact me personally: try6767@gmail.com


Gilad D (try67)   

I thought as much, I will run a script through the batch processing option instead.

The ThomParker script that uses email and extraction gives me a the basis, but I dont have the knowledge to adapt it to save using the original file name. Can you help with the script?


Wayne Lee   

Yeah, sure. Post what you have and I'll let you know what should be changed...


Gilad D (try67)   

All I have is the code on this page
http://acrobatusers.com/tutorials/pri...


Wayne Lee   

This code should do the trick:

var outputPath = "/C/Converted_PDFs";
for (var i=0; i<this.numPages; i++) { var fileNameParts = this.documentFileName.split("_")
var newFileName = this.documentFileName.replace(/\.pdf$/i, "_"+(p+1)+".pdf");
var newFilePath = outputPath + "/" + fileNameParts[0] + "/" + newFileName;
this.extractPages({nStart: i, nEnd: i, cPath: newFilePath});}


Gilad D (try67)   

Your response;

var outputPath = "/C/Converted_PDFs";
for (var i=0; i<this.numPages; i++) { var fileNameParts = this.documentFileName.split("_")
var newFileName = this.documentFileName.replace(/\.pdf$/i, "_"+(p+1)+".pdf");
var newFilePath = outputPath + "/" + fileNameParts[0] + "/" + newFileName;
this.extractPages({nStart: i, nEnd: i, cPath: newFilePath});}

My response;

Thank you for the code, I'm in the process of running the code and have used the console and run the code line by line.
The console errored saying "p" wasn't defined. Do I need to declare this after the i=0?

Thanks again


Wayne Lee   

Sorry, replace

(p+1)
with

(i+1)


Gilad D (try67)   

Sorry for the delay in responding, I've been trying to get it to work this end.

I'm getting an error "RaiseError: The file may be read-only ....."

Ive tried numerous files and from different locations, ensuring all are not read only.

Do you think I need to save the file as .tmp first then convert to .pdf afterwards?


Wayne Lee   

Yes, you have to run this code on a PDF file only.


Gilad D (try67)   

When I'm running the script I use the advanced - document processing - advanced processing - run a sequence - run javascript

I've tried with the file already open, no files open, chosen "Run Commands from" file selected, folder selected.

All come back with the same RaiseError


Wayne Lee   

Been loocking via my friend google and have found this

http://forums.adobe.com/message/4605040

Do I need this wrapper app privledges and volume ?


Wayne Lee   

Acrobat thinks one of the files created through extraction is locked by the system. It's possible the file is open, but it is more likely that it was not closed properly. Acrobat has a tendancy to do this when it creates files. Try closing and restarting Acrobat. If this doesn't work you may need to log out or reboot.

Thom Parker
The source for PDF Scripting Info pdfscripting.com
All About PDF Stamps in Acrobat and Paperless Workflows - THE BOOK !!

The Acrobat JavaScript Reference, Use it Early and Often

The most important JavaScript Development tool in Acrobat
The Console Window (Video tutorial)
The Console Window(article)

Having trouble, Why Doesn't my Script Work


Thom Parker   

I've tried rebooting etc,. Still no luck

Thank you for the links, I can take a hint.

The information you have given me has been a tremendous help.

Thank you.

If anyone else can tell me if the privledges thing is liekly to be cause of the error do let me know how to get around it, as it'll take me ages to read through all the links and books.

Thank you.


Wayne Lee   

No, it's not related to the privileges, since batch processes are a trusted context.


Gilad D (try67)   

If you're not using the "File open in Acrobat" option, you must make sure the files you're running the batch on are not open when you run it.

Also, when selecting the output option, make sure to select Do Not Save Changes.


Gilad D (try67)   


Please specify a reason: