0 Replies Latest reply on Mar 29, 2017 9:27 AM by jonwater

    Automate adding security to a pdf while saving


      I have seen several people trying to do a similar thing in this forum, but I didn't see an actual working solution...


      I have a livecycle pdf document that when it opens, runs through a loop loading specified xml data files, and saves the files, with the saving done by a trusted function.  All that works perfectly. All I want to add is applying a security policy to make the files read only. My .js function looks like this:


      var ETASaveAs = app.trustedFunction( function(oDoc,cPath,cFlName)


          var aMyPath = oDoc.path.split("/");


          // Remove old file name


          // put subdir path



          // Add new file name




              // apply security policy       

                  var oMyPolicy = null;

                  var aPols = security.getSecurityPolicies()

                  for(var index=0;index<aPols.length;index++){

                      //console.println("policy " + index + " is " + aPols[index].name    );

                      if(aPols[index].name == "prevent_changes"){

                          oMyPolicy = aPols[index]; break;



                  if(oMyPolicy == null){ app.alert("Policy Not Found"); return; }

                  // Now, Apply the security Policy


                  var rtn = oDoc.encryptUsingPolicy({oPolicy: oMyPolicy });

                  if(rtn.errorCode != 0) app.alert("Encrypt Security Error: " + rtn.errorText);


                 // Put path path back together and save document

              var rtn = oDoc.saveAs(aMyPath.join("/"),"","",true );  // save as copy, otherwise active doc will be in subdirectory

                  if(rtn.errorCode != 0) app.alert("Error saving: " + rtn.errorText);






      The problem is that the line


                  var rtn = oDoc.encryptUsingPolicy({oPolicy: oMyPolicy });


      gives: "NotAllowedError: Security settings prevent access to this property or method."


      From the docs I can't see that the restrictions for encryptUsingPolicy are any different from saveAs, which works just fine, so I'm not sure what the problem is.


      I'm using Acrobat Professional 9.5.