Skip to main content
Inspiring
January 30, 2020
Answered

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

  • January 30, 2020
  • 3 replies
  • 9958 views

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
This topic has been closed for replies.
Correct answer Thom Parker

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("/"));

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");
 
 
 

3 replies

Participant
April 15, 2020

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!

Inspiring
May 24, 2021

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.

Legend
May 24, 2021

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.

Thom Parker
Community Expert
Community Expert
January 30, 2020

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 PDFScriptingUse the Acrobat JavaScript Reference early and often
Inspiring
January 30, 2020

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
 
Inspiring
January 30, 2020

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("/"));
Bernd Alheit
Community Expert
Community Expert
January 30, 2020
Inspiring
January 30, 2020

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.