Skip to main content
Dmitriy K.
Known Participant
June 23, 2020
Question

Protect PDF document with Javascript

  • June 23, 2020
  • 1 reply
  • 1901 views
How to password protect(take off) PDF document using Javascript code?

Pre-created a protection policy with the name "MySecurityPol", and run this code from the console in Acrobat Pro DC, but there is a syntax error! What am I doing wrong?

var DoApplyMySecurity = app.trustedFunction( function(){
    var oMyPolicy = null;    app.beginPriv();
    // First, Get the ID of My Security Policy
    var aPols = security.getSecurityPolicies()
    for(var index=0;index<aPols.length;index++){
        if(aPols[index].name == "MySecurityPol"){            oMyPolicy = aPols[index]; break;
        }
    }
    if(oMyPolicy == null){ app.alert("Policy Not Found"); return; }
    // Now, Apply the security Policy
    var rtn = this.encryptUsingPolicy({oPolicy: oMyPolicy });
    if(rtn.errorCode != 0) app.alert("Security Error: " + rtn.errorText); app.endPriv();
});
I want to put a code in the form field to protect the document, when I click on the form, the code is launched and the document is encrypted.
This topic has been closed for replies.

1 reply

try67
Community Expert
Community Expert
June 23, 2020

I'm not getting any syntax errors for this code. What is the full text of the error message?

Dmitriy K.
Known Participant
June 25, 2020

Sorry for the absence, it looks like an error if you run the code from the console

if you put the code in the button form field, then the error looks like this

 

Bernd Alheit
Community Expert
Community Expert
June 25, 2020

Does you select the whole script when you execute it in the console?

 

You can't define a trusted function in a field button. You must put in a folder level script.