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

Action to extract p1 from all PDFs in folder and save as JPEG to different folder

New Here ,
Aug 22, 2021 Aug 22, 2021

Hi helpful legends, 

I am using Acrobat Pro DC (version 2021.005.20058). 

 

I am trying to automate a process to make cover images from a folder of PDFs. I want an action that will process each PDF within a given folder to extract page 1, convert that to JPEG and save in a different folder. 

 

I have been poking around the interwebz for a while tonight but my understanding of javascript is close to zero so I'm hitting a wall. I can see that this should be possible, but I can't make it work. 

I have found a script that extracts page 1 and saves it to the same folder as a PDF:

this.extractPages(0, 0, this.path.replace(/\.pdf$/i, "_p1.pdf"));

 

I also found a separate action that converts PDFs to JPEGs. I can do those sequentially and get the job done, but it requires a bit of fiddling around with sorting files to process only the ones I want into JPEGs. 

I would love to get an all-in-one script that would convert page 1 into a jpeg in one action, so I can share an easy-to-use solution with my colleagues. 

 

Any wonderful coders out there who can help? Thank you so much!

TOPICS
Edit and convert PDFs , JavaScript
1.4K
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
1 ACCEPTED SOLUTION
Community Expert ,
Aug 22, 2021 Aug 22, 2021

One possible way of doing it is to delete all the other pages in the file, and then save the only remaining page (which is the first page from the original file) as a JPEG. You can do it like this:

 

 

 

 

if (this.numPages>1) this.deletePages(1, this.numPages-1);
this.saveAs({cPath: this.path.replace(".pdf", "_p1.jpg"), cConvID: "com.adobe.acrobat.jpeg"});
this.dirty = false;

 

 

 

 

If you run it as a part of an Action then you could use it on multiple files, and since there's no save command added to it, the original files will remain in tact.

 

Edited: Full code added, for future reference.

View solution in original post

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 ,
Aug 22, 2021 Aug 22, 2021

One possible way of doing it is to delete all the other pages in the file, and then save the only remaining page (which is the first page from the original file) as a JPEG. You can do it like this:

 

 

 

 

if (this.numPages>1) this.deletePages(1, this.numPages-1);
this.saveAs({cPath: this.path.replace(".pdf", "_p1.jpg"), cConvID: "com.adobe.acrobat.jpeg"});
this.dirty = false;

 

 

 

 

If you run it as a part of an Action then you could use it on multiple files, and since there's no save command added to it, the original files will remain in tact.

 

Edited: Full code added, for future reference.

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
New Here ,
Aug 22, 2021 Aug 22, 2021

@try67, thank you so much for helping me with this! This solution is close, which is making me very excited. There are a couple of issues still:

  1. It prompts the user for each file 'Do you want to save changes to 'Original PDF file name' before closing?' Can you make it so Acrobat automatically closes the file without saving and the user doesn't need to interact? 
  2. If there are any files in the folder that just have a single page, the script doesn't work on them. In an ideal world the action would work on a folder of mixed PDFs with any number of pages in them. 

 

Is there any way to tweak it to get those outcomes? I can't ask users to sit there and click 'No' 63 times every time they run this 🙂 

 

Thank you again so much. I cannot tell you how much time and tedious manual effort this will save if we can get it to work! Really appreciate your help.

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 ,
Aug 22, 2021 Aug 22, 2021

1. Add this:

 this.dirty = false;

 

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
New Here ,
Aug 23, 2021 Aug 23, 2021

Thank you so much @Bernd Alheit, that does indeed fix issue #1. Yay! 

 

Now the only issue is it not handling 1-page documents. This is not a dealbreaker as I could just use the plain 'Convert PDF to JPEG' action on those. It would just be neater to have one action to rule them all when creating cover images. 

 

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 ,
Aug 23, 2021 Aug 23, 2021

2. Add this to the start of the code:

if (this.numPages>1)

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 ,
Aug 23, 2021 Aug 23, 2021

You'll find an Action for extracting the first page of a document here:

https://acrobatusers.com/actions-exchange/

 

 

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
New Here ,
Aug 23, 2021 Aug 23, 2021
LATEST

Thank you so much everyone for your contributions! It is so exciting to have this solved. This is going to really help my team's workflow and I am incredibly grateful to you all. THANK YOU.

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