Applying PDF Password Security Using Acrobat JavaScript
- PDF Document Security and Batch Processing
- It is a common task to apply a password security to a number of PDF documents using Adobe Acrobat batch processing framework. However, Adobe Acrobat does not store passwords in a batch sequence file (sequence files are stored as plain text) for a security reason. Once you restart Adobe Acrobat and try to run a batch sequence that contains a "Document / Security" batch command, you will be prompted again to enter a password. The same happens if you are using AutoBatch plug-in to execute this batch sequence from a command-line prompt or a system batch file. This is very inconvenient and often impossible if a batch file is executed on unattended computer (server) using Task Scheduler or another batch automation tool.
- Using Acrobat JavaScript For Password-Protection
- There is a simple solution for this problem. It is easy to password-protect PDF documents using Acrobat's JavaScript batch command. Acrobat JavaScript contains several objects, functions and properties for handling different aspects of PDF security. The doc.encryptUsingPolicy() function provides a convenient option for applying password protection using an existing security policy.
- Step-by-Step Instructions
- Create a security policy using "Advanced > Security > Manage Security Policies..." menu (available in Acrobat Pro and above). This security policy can apply password protection or restrict access rights.
- Create an Acrobat batch sequence using "Advanced > Document Processing > Batch Processing..." menu (available in Acrobat Pro and above).
- Add a "JavaScript" / "Execute JavaScript" batch command to the batch sequence. Add JavaScript code that applies security policy that you have created in step 1 (see code sample below).
- Apply security to PDF documents by executing this batch sequence.
- Optionally use AutoBatch plug-in to run this batch sequence via a command line. Use "Plug-ins > Create Batch Sequence" menu to automatically generate a system batch file from a desired batch sequence.
- Acrobat JavaScript Code For Applying a Security Policy
- Use the following code to apply an existing security policy to a PDF document.
Make sure to change the name of the security policy (SimplePassword
in the example below) to one of your existing policies. You can download this code
in a plain text format here.
/* Applying a Security Policy to a PDF Document */
/* You need to create a security policy before running this script */
/* Use "Advanced > Security > Manage Security Policies" menu */
var ApplySecurity = app.trustedFunction(
function()
{
var oMyPolicy = null;
var sPolicyName = "SimplePassword"; // Replace "SimplePassword" with your policy name
app.beginPriv();
// First, Get the ID of SimplePassword security policy
var aPols = security.getSecurityPolicies();
for(var i=0; i<aPols.length; i++)
{
if(aPols[i].name == sPolicyName)
{
oMyPolicy = aPols[i];
break;
}
}
if(oMyPolicy != null)
{
// Now, Apply the security Policy
var rtn = this.encryptUsingPolicy({oPolicy: oMyPolicy });
if(rtn.errorCode != 0)
{
// Print error message into JavaScript Debugger Console
console.println("Security Error: " + rtn.errorText);
}
}
app.endPriv();
});
ApplySecurity(); - Further Reading
- If you want to know more low-level details about Acrobat JavaScript and security policies, then read this excelent article by Thom Parker: http://www.acrobatusers.com/tutorials/2007/10/apply_security_with_js