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

Step-by-step instructions for saving a PDF in Acrobat DC using a Javascript

Explorer ,
Jan 30, 2020 Jan 30, 2020

Copy link to clipboard

Copied

I've spent quite a while looking for information on this and come up short:

Apparently there are security settings which interfere with just using the command (I've tried setting my Desktop and a specific PDF on the Desktop to be trusted without success).

Running the command:

    app.getPath('user','javascript')

in the console gets the error:

    GeneralError: Operation failed.
    App.getPath:1:Console undefined:Exec

    2

    undefined
So I've been unable to determine where to create the "Privileged" (or is it "Priviledged"?) folder.
I simply want to know:
 - where does one store the Javascript function so that it will be "trusted:"
 - what code goes in that?
 - how does one call the code so that one can then use a prompted variable to save a PDF using a name (in this case a number which was used to extract all pages of a PDF which contain said number)
 
EDIT: turns out the problem was with some copied code not generating a valid path/filename, and Adobe Acrobat reporting its inability to save using the wrong code as a security issue when it wasn't. See the edited post below for complete code to:
 
 - prompt user for text
 - search the current pdf for the text and extract each page containing it
 - re-save the PDF of extracted pages using the prompted text for the name
TOPICS
Create PDFs , General troubleshooting , How to

Views

5.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 , Jan 30, 2020 Jan 30, 2020

About this Action Script. The path build has an issue with this code

 
aMyPath.push(newfilename);
aMyPath.push(".pdf");
// Put path back together and save
this.saveAs(aMyPath.join("/"));
 

 These lines will create a path that looks like this

"/c/path/name/.pdf"

 

Change it to this

 

aMyPath.push(newfilename);
// Put path back together and save
this.saveAs(aMyPath.join("/")+".pdf");
 
 
 

Votes

Translate

Translate
Community Expert ,
Jan 30, 2020 Jan 30, 2020

Copy link to clipboard

Copied

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
Explorer ,
Jan 30, 2020 Jan 30, 2020

Copy link to clipboard

Copied

I have. Several times.

It is out of date, and doesn't provide the specific details which I have asked after --- please either provide these specifics, or provide a link to up-to-date documentation which has them.

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 ,
Jan 30, 2020 Jan 30, 2020

Copy link to clipboard

Copied

Acrobat does not automatically create the "User" JavaScript folder, which is why you are getting the error. It has to be created manually, and you need to know where it is supposed to be. Unfortunately Adobe has not made this location obvious.

This aricle covers ths details:

https://www.pdfscripting.com/public/Installing_Automation_Tools.cfm

 

This article shows how to create and use trusted functions:

https://www.pdfscripting.com/public/Using-Trusted-Functions.cfm?sd=40

 

This article explains trust and privilege in Acrobat:

https://www.pdfscripting.com/public/Trust-and-Privilege-in-Acrobat-Scripts.cfm?sd=40

 

 

 

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
Explorer ,
Jan 30, 2020 Jan 30, 2020

Copy link to clipboard

Copied

Thank you.

 

For anyone who comes here via a search:

 

C:\Users\<USERDIRECTORY>\AppData\Roaming\Adobe\Acrobat\Privileged\DC\Javascripts

 

So I placed what I hope to be appropriate code into a globs.js file in the aforementioned directory:

 

var Trusted_MyDocSave = app.trustedFunction(function(oDoc)
{
   app.beginPriv();
       oDoc.saveAs(oDoc.path);
     app.endPriv();
});
 
which presumably worked since:
app.getPath("user","javascript");
 
yielded:
 
/C/Users/<USERDIRECTORY>/AppData/Roaming/Adobe/Acrobat/Privileged/DC/JavaScripts
 
but when I tried to call it with:
 
    Trusted_MyDocSave(aMyPath.join("/"));
 
received the error message:
 
ReferenceError: Trusted_MyDocSave is not defined
24:Batch:Exec
 

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
Explorer ,
Jan 30, 2020 Jan 30, 2020

Copy link to clipboard

Copied

Further, at: https://www.pdfscripting.com/public/Trust-and-Privilege-in-Acrobat-Scripts.cfm?sd=40

 

it says:

 

    "Acrobat Actions" is an automation tool that is only available in Acrobat Professional. It is a way to apply a sequence of document processing operations, including scripts, to multiple documents. Such "Action Sequences" were either created (through the Action tool) or deliberately placed on the user's system, so they are trusted.

 

and yet I am placing my code in an action, yet I still get an 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.saveAs:21:Batch 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.

 

which I assume is a security issue (when I searched that is what was indicated). If that's not it, I'd appreciate a pointer to what the actual difficulty is.

 

For the sake of compleatness' the code I have in the action is:

 

//
var dialogTitle = "Please specify ";
var defaultAnswer = "";
var newfilename = app.response("Client ID",
dialogTitle, defaultAnswer);
// Split Path into an array so it is easy to work with
var aMyPath = this.path.split("/");
// Remove old file name
aMyPath.pop();
// Add new file name
aMyPath.push(newfilename);
aMyPath.push(".pdf");
// Put path back together and save
this.saveAs(aMyPath.join("/"));

//Trusted_MyDocSave(aMyPath.join("/"));

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 ,
Jan 30, 2020 Jan 30, 2020

Copy link to clipboard

Copied

About this Action Script. The path build has an issue with this code

 
aMyPath.push(newfilename);
aMyPath.push(".pdf");
// Put path back together and save
this.saveAs(aMyPath.join("/"));
 

 These lines will create a path that looks like this

"/c/path/name/.pdf"

 

Change it to this

 

aMyPath.push(newfilename);
// Put path back together and save
this.saveAs(aMyPath.join("/")+".pdf");
 
 
 
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 ,
Jan 30, 2020 Jan 30, 2020

Copy link to clipboard

Copied

if the error message is telling you that the "Trusted_MyDocSave" is not defined, then it's not defined.

One possible issue is that Acrobat was not shutdown all the way. Please exit Acrobat and check the task manager to make sure it's not running. Then restart Acrobat and check to console window to see if any errors are reported.

 

As for the Action. It's likely Acrobat is blocking the JavaScript save because only the Action is allowed to save it. But it's also likely that Acrobat is just confused in it's error messages, it does that for security related issues. 

 

 

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
Explorer ,
Jan 30, 2020 Jan 30, 2020

Copy link to clipboard

Copied

This then circles around to the beginning --- how do I create a Javascript which will prompt the user when run for a string/number which will then be used internally in the script (that part works, I have it extracting just the pages for a given Client ID), and then save the extracted pages as a file using the name from the dialog?

 

I've quit Acrobat, rebooted, and re-tried, but still getting the same error messages, either undefined function, or not being allowed to save.

 

Why isn't there a single page which documents how to achieve what I'd think would be a pretty straight-forward (suitable as an example) task?

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 ,
Jan 30, 2020 Jan 30, 2020

Copy link to clipboard

Copied

There is a documents that does show how to do this, and you've already read it. The problem is that you are doing something wrong. We have to figure out what that is.

 

There is only one single issue here. Which is, "why isn't your folder level function getting defined?" This often happens because there is something else in the same file that is failing. Is there other code in this "Globs.js" file?

 

Did you check the console window after restarting Acrobat for any startup errors?

 

Something you can try to make sure the function works, is to define it in the Console. And then see if there is a problem with your other scripts using it.

 

On another note, you may want to add a path argument to the function, so you can control the save location of the extracted pages. 

 

 

 

 

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
Explorer ,
Jan 31, 2020 Jan 31, 2020

Copy link to clipboard

Copied

Turns out there were two issues.

 

First off, the code copied from that web page was wrong, and the corrected code which you provided:

 

aMyPath.push(newfilename);
// Put path back together and save
this.saveAs(aMyPath.join("/")+".pdf");

 

fixed this so that it now saves. Thank you.

 

Seems that having the Javascript in the Action is okay security-wise, so that was all a red-herring and it's not necessary to jump through the various hoops for that.

 

The remaining difficulty is that it saves the wrong PDF with the new name, the original one, not the extracted one.

 

which I was hoping was caused by a mis-placed parentheses, but shifting that didn't work. The solution of course is to replace this. with d.

 

//
var dialogTitle = "Please specify ";
var defaultAnswer = "";
var stringToSearchFor = app.response("Client ID",
dialogTitle, defaultAnswer);

/* Put script title here */
// Iterates over all pages and find a given string and extracts all
// pages on which that string is found to a new file.
var pageArray = [];
//var stringToSearchFor = "64718";
for (var p = 0; p < this.numPages; p++) {
 // iterate over all words
 for (var n = 0; n < this.getPageNumWords(p); n++) {
  if (this.getPageNthWord(p, n) == stringToSearchFor) {
   pageArray.push(p);
   break;
  }
 }
}
if (pageArray.length > 0) {
 // extract all pages that contain the string into a new document
 var d = app.newDoc();    // this will add a blank page - we need to remove that once we are done
 for (var n = 0; n < pageArray.length; n++) {
  d.insertPages( {
   nPage: d.numPages-1,
   cPath: this.path,
   nStart: pageArray[n],
   nEnd: pageArray[n],
  } );
 }
    // remove the first page
    d.deletePages(0);
   
// Split Path into an array so it is easy to work with
var aMyPath = this.path.split("/");
// Remove old file name
aMyPath.pop();
// Add new file name
aMyPath.push(stringToSearchFor);
// Put path back together and save
d.saveAs(aMyPath.join("/")+".pdf");
 

}

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
New Here ,
Apr 14, 2020 Apr 14, 2020

Copy link to clipboard

Copied

I also struggled to find how to create a simple menu that allows to save the current document on the desktop. Finally I did it!

It's with Acrobat 9. The script must be put in the Javascripts folder "C:\Users\[User name]\AppData\Roaming\Adobe\Acrobat\9.0\JavaScripts". I guess you'll have to find the equivalent folder for a different version. This is a very basic operation, I think it may help those who would search internet to make a Save As in Acrobat.

 

Here is the code:

// The trusted function that does the Save As
function SaveOnDesktop(oDoc)
{
app.beginPriv();
vPath = "/c/Users/[User Name]/Desktop/" + oDoc.documentFileName
oDoc.saveAs(vPath);
app.endPriv();
}

var Trusted_SaveOnDesktop = app.trustedFunction(SaveOnDesktop);

// The creation of the menu
app.addMenuItem({
cName: "Save on Desktop",
cParent: "Document",
cExec: "Trusted_SaveOnDesktop(this)"
});

 

It was the first time that I coded in JavaScript, so I discovered a few things :-):

- JS is case sensitive  oDoc.SaveAs() does not work, it's  oDoc.saveAs()

- I got confused with  oDoc  thinking it could be the name as the current document, but it's the object. Thus the need to create the  vPath  variable for the name of the document.

- I used the two-step variant for the trusted function, because when I searched the internet it was more complicated for me to understand examples where the two steps were merged. So you will see in my code that the creation of the variable  Trusted_SaveOnDesktop  comes as a 2nd step. You can merge the two together as shown in other examples below.

- I used alerts to know what was the content of the variables (that's how I discoverd that oDoc was the object).

app.alert(oDoc)
app.alert(vPath)

 

Super basic, I'm sure!  :-))  But quite happy it finally worked at the end of the day. I hope it will be useful for beginners like 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
Participant ,
May 24, 2021 May 24, 2021

Copy link to clipboard

Copied

I read your post with interest, but couldn't figure out how I can solve my problem.
Can I ask you how can I solve my problem?

I opened 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.

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

Copy link to clipboard

Copied

Why, exactly, can't you build a function that works? The instructions are good - so long as you don't try to skip anything - so we need complete details of what you do, and what the console says, and what happens.

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

The "app.execMenuItem " line is redundant. Remove it. 

 

Trusted functions are a somewhat advanced scripting topic. So, you have to pay attention to the details and be careful about how you write the code, and where that code is placed. 

 

Please post your folder level code in full.  

 

 

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

Copy link to clipboard

Copied

Hi
Trusted Level Function saved in a Notepad.js file under the Adobe / Javascript folder:
myPropagatorFunction = app.trustPropagatorFunction(function(oMyDoc,cMyPath)
{
app.beginPriv();
mySaveAs(oMyDoc,cMyPath);
app.endPriv();
}
);
PDF ButtonCode under button that is executed on Mouse up click:
var oMyDoc = this.extractPages (5)
var cMyPath = oMyDoc.saveAs("/C/Users/Tabene/Documents/QUESTION.pdf");
oMyDoc.closeDoc (true);
I press the button and in the console it appears written
NotAllowedError: Security settings do not allow access to this property or method.
Doc.saveAs: 2: Field Button2: 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
LEGEND ,
May 26, 2021 May 26, 2021

Copy link to clipboard

Copied

You've done half the job. You defined a privileged routine. It's not actually right, but it's the right idea. However, on your button, you contine to call oMyDoc.saveAs. This is just the standard saveAs routine; it failed before you made a privileged routine, and it keeps failing. 

 

I think you probably have the right instructions, but need to follow them more closely.

 

The key part to work on first is that you have to replace the oMyDoc.saveAs call by calling the privileged routine. The code in your document will never contain oMyDoc.saveAs

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

Copy link to clipboard

Copied

LATEST

You've sort-of have the idea.

This article provides the exact code you need:

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