Applying PDF Password Security Using Acrobat JavaScript

Acrobat JavaScript Code For Applying a Security Policy.

http://www.verypdf.com/app/javascript-to-pdf-embedder-cmd/index.html

Step-by-Step Instructions
  1. 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.
  2. Create an Acrobat batch sequence using "Advanced > Document Processing > Batch Processing..." menu (available in Acrobat Pro and above).
  3. 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).
  4. Apply security to PDF documents by executing this batch sequence.

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.

/* 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();

You can save above JS code to a text file, you can run following command line to insert this Javascript code into PDF file,

addjs2pdf.exe -runatopen js.txt in.pdf out.pdf

out.pdf file will contain the Javascript code, when you open this PDF file in Adobe Acrobat software, above Javascript code will be executed automatically.

VN:F [1.9.20_1166]
Rating: 0.0/10 (0 votes cast)
VN:F [1.9.20_1166]
Rating: 0 (from 0 votes)

Related Posts

This entry was posted in VeryPDF Blog and tagged , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *


Verify Code   If you cannot see the CheckCode image,please refresh the page again!