Protect PDF document with Javascript

Explorer ,
Jun 23, 2020 Jun 23, 2020

Copy link to clipboard

Copied

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.
TOPICS
Acrobat SDK and JavaScript, How to, PDF forms, Security digital signatures and esignatures

Views

111

Likes

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
Adobe Community Professional ,
Jun 23, 2020 Jun 23, 2020

Copy link to clipboard

Copied

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

Likes

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 ,
Jun 24, 2020 Jun 24, 2020

Copy link to clipboard

Copied

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

1.png

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

Screenshot_2.png

 

Likes

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
Adobe Community Professional ,
Jun 25, 2020 Jun 25, 2020

Copy link to clipboard

Copied

LATEST

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.

Likes

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