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

how to rename extracted pages with JavaScript

Participant ,
Apr 13, 2021 Apr 13, 2021

Copy link to clipboard

Copied

Hello. I have a 28 page pdf file. I would like to create a button that allows me to extract page 5 and rename it before saving it in the documents folder.
With another button, I would like to email as an attachment page 5 renamed.
I found this:
var cToAddr = "apctr@example.com"
var cSubLine = "Paperwork"
var cBody = "Here is your paperwork."
var page0 = this.extractPages (5);
page0.mailDoc ({bUI: true, cTo: cToAddr, cSubject: cSubLine, cMsg: cBody});
page0.closeDoc (true);
This works, but I can't rename page 5 of the pdf.
Can anyone help me please?
Thank you

TOPICS
JavaScript

Views

4.3K

Translate

Translate

Report

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 14, 2021 Apr 14, 2021

Then the value of cPath needs to be: "/C/Users/Tabene/Documents/QUESTION.pdf"

Read Thom's tutorial to learn how to set up this trusted function.

Votes

Translate

Translate
Community Expert ,
Apr 13, 2021 Apr 13, 2021

Copy link to clipboard

Copied

You do that by specifying the cPath parameter of the extractPages method, but that requires running this command from a trusted context (such as a folder-level script). Can you install a script on the local machine of each person who is going to be using this file? Also, this will only work in Acrobat, of course, as Reader can't extract pages.

Votes

Translate

Translate

Report

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
Participant ,
Apr 13, 2021 Apr 13, 2021

Copy link to clipboard

Copied

I thank you for the answer. The file is used by a single user with acrobat dc.
The path to the file is C: \ Users \ Tabene \ Documents
Can anyone help me to compose a script?

Votes

Translate

Translate

Report

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, 2021 Apr 13, 2021

Copy link to clipboard

Copied

How do you want to name the file with the single page?

Votes

Translate

Translate

Report

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
Participant ,
Apr 14, 2021 Apr 14, 2021

Copy link to clipboard

Copied

I want to call it "QUESTION". 
Thank you

Votes

Translate

Translate

Report

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 14, 2021 Apr 14, 2021

Copy link to clipboard

Copied

Then the value of cPath needs to be: "/C/Users/Tabene/Documents/QUESTION.pdf"

Read Thom's tutorial to learn how to set up this trusted function.

Votes

Translate

Translate

Report

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
Participant ,
Apr 14, 2021 Apr 14, 2021

Copy link to clipboard

Copied

Hello. I have a 28 page file. I want to extract page 5. Then I want to save the page with a new name in a folder.
As suggested to me, I read Thom's tutorial, but I didn't come up with anything.

Votes

Translate

Translate

Report

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 14, 2021 Apr 14, 2021

Copy link to clipboard

Copied

Save or to extracting pages to a named file requires a trusted function. It can't be done from a script in a docuemnt. The article outlines the methodology for saving a file and provides the code. 

This article is more general:

https://www.pdfscripting.com/public/Using-Trusted-Functions.cfm

 

 

 

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

Votes

Translate

Translate

Report

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, 2021 Apr 13, 2021

Copy link to clipboard

Copied

This article shows how to write a custom SaveAs function. This function can easily be modified to extract your pages to a specific file name. Or it could be used as with your existing script to save your extracted pages to a file with a specific name.

https://www.pdfscripting.com/public/How-to-Save-a-PDF-2.cfm

 

 

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

Votes

Translate

Translate

Report

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
Participant ,
Apr 15, 2021 Apr 15, 2021

Copy link to clipboard

Copied

Hello. I have read Thom's tutorial.
I was able to get this feature to work
var oMyDoc = this.extractPages (5)
app.execMenuItem ("SaveAs", oMyDoc);
Page 5 is extracted and the save window opens.
The pdf page is so named "A9Re8zj55_hnmzv_nnw.tmp.pdf".
I tried to insert a script to locate a destination folder:
var oMyDoc = this.extractPages (5)
var cPath = this.saveAs ("/ C / Users / Tabene / Documents / QUESTION.pdf");
app.execMenuItem ("SaveAs", oMyDoc, cPath);
Does not work. Page 5 is extracted and that's it.
Even the only function this.saveAs ("/ C / Users / Tabene / Documents / QUESTION.pdf");
does not work

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

The extracted page is in "oMyDoc", but the "saveAs" call uses the current document, i.e., "this", so it is the current document that is renamed. The code needs to use the document object of the extracted page.

 

oMyDoc.saveAs(...);

 

The call to execute the "SaveAs" menu item doesn't do anything.

 

 

 

 

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

Votes

Translate

Translate

Report

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
Participant ,
Apr 24, 2021 Apr 24, 2021

Copy link to clipboard

Copied

Thank you for the help Thom.
I changed the function like this:
var oMyDoc = this.extractPages (5)
var cPath = oMyDoc.saveAs ("/ C / Users / Tabene / Documents / QUESTION.pdf");
app.execMenuItem (oMyDoc, cPath);
The formula doesn't work.
Page 5 is extracted, but it is not saved in the directory.

Votes

Translate

Translate

Report

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
Participant ,
May 24, 2021 May 24, 2021

Copy link to clipboard

Copied

Hi
i open the console with ctrl + j.
I tried this formula:
var oMyDoc = this.extractPages (5)
var cMyPath = oMyDoc.saveAs ("/C/Users/Tabene/Documents/QUESTION.pdf");
app.execMenuItem ("Save", oMyDoc, cMyPath);
It works.
If I put the formula in a button it doesn't work.
I need to build a trust with a Trusted Function to be placed in the javascript folder.
I've read the instructions on this page, https://www.pdfscripting.com/public/Using-Trusted-Functions.cfm, but I can't build a function that works.
Please, can anyone help me?

Votes

Translate

Translate

Report

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 ,
May 24, 2021 May 24, 2021

Copy link to clipboard

Copied

You don't need to use execMenuItem, saveAs does the same thing. You do need to run it from a trusted function, though

Votes

Translate

Translate

Report

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
Participant ,
May 24, 2021 May 24, 2021

Copy link to clipboard

Copied

Yes. But I can't build a trusted function

Votes

Translate

Translate

Report

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 ,
May 24, 2021 May 24, 2021

Copy link to clipboard

Copied

Why not? You have all the information needed.

If you still can't figure it out you can always hire someone to do it for you.

Votes

Translate

Translate

Report

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
Participant ,
Jun 02, 2021 Jun 02, 2021

Copy link to clipboard

Copied

Can you help me? I am willing to pay you

Votes

Translate

Translate

Report

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 ,
Jun 02, 2021 Jun 02, 2021

Copy link to clipboard

Copied

Yes, you can contact me via [try6767 at gmail.com] to discuss it further, including the price.

Votes

Translate

Translate

Report

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 ,
Jun 02, 2021 Jun 02, 2021

Copy link to clipboard

Copied

Message me through this forum and we can work something out.  What you are asking for is a small job. 

 

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

Votes

Translate

Translate

Report

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
Participant ,
Jun 04, 2021 Jun 04, 2021

Copy link to clipboard

Copied

Hi
I have a pdf file of many pages. I would like to extract the pages, rename them and save them in a path. I have to extract from page 1 to page 4, then page 5, then page 6 to 8 etc.
I created a button and entered the following function:

var Doc = this.extractPages (5)
var Path = Doc.saveAs ("/ C / Users / Tabene / Documents / QUESTION.pdf");
Doc.closeDoc (true);

In Adobe's Javascript folder, I have placed in file with ".js" extension containing the following function

 

var Trusted_MyDocSave = app.trustedFunction(function(Doc)
{
app.beginPriv();
Doc.saveAs(Doc.path);
app.endPriv();
});

I closed and reopened the pdf file. I pressed the button, but only page 5 is extracted.
I tried the button formula in the acrobat console. It works.
After pressing the button in the console this message appears:

NotAllowedError: Security settings do not allow access to this property or method.
Doc.saveAs: 2: Field Button1: Mouse Down

Votes

Translate

Translate

Report

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 ,
Jun 04, 2021 Jun 04, 2021

Copy link to clipboard

Copied

Why aren't you using the trusted function you've declared?

Votes

Translate

Translate

Report

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 ,
Jun 04, 2021 Jun 04, 2021

Copy link to clipboard

Copied

Also, this trusted function will not do what you want. It will just save the under its current path. You need to provide the new path as another parameter to it.

Votes

Translate

Translate

Report

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
Participant ,
Jun 04, 2021 Jun 04, 2021

Copy link to clipboard

Copied

The path always remains the same, because I want to save pages 1 to 4 and page 5 etc in the same folder creating different files each with a different name.
In the example I wrote only this.extractPages (5) because I imagine that for the other extracted pages it will be enough to repeat the function

Votes

Translate

Translate

Report

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 ,
Jun 04, 2021 Jun 04, 2021

Copy link to clipboard

Copied

It doesn't matter if it stays the same, you still have to specify it. You can either pass it as a parameter or hard-code it into the code, but it needs to be somewhere.

Votes

Translate

Translate

Report

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
Participant ,
Jun 05, 2021 Jun 05, 2021

Copy link to clipboard

Copied

For a formula like this
var Doc = this.extractPages (5) etc ..........
I have to write a trust function, instead for a formula like this
var Doc = this.extractPages (1,5)
var Path = Doc.saveAs ("/C/Users/Tabene/Documents /QUESTION.pdf");
Doc.closeDoc (true);
I have to write a new trust function,
if I write: var Doc = this.extractPages (6,10) another trust function and so on.
Is that so, did I understand correctly?

Votes

Translate

Translate

Report

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