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
aMyPath.pop();
// put subdir path
aMyPath.push(cPath);
// Add new file name
aMyPath.push(cFlName);
app.beginPriv();
// 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);
app.endPriv();
});
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.
Thanks,
Jon
