Control Script: Securing PDF attachments

The Print context can be attached to an email in the form of a PDF file and secured with a password. This can be done without a Control Script, see Email attachments and Email PDF password.
With a Control Script, you can do the same, and more: the attachment can be split into multiple attachments (see Parts). Each attachment may have a different (or no) set of passwords, so you could mix secured and unsecured attachments in a single email. This topic shows how.

For information about Control Scripts in general, see Control Scripts and Control Script API. If you don't know how to write scripts, see Writing your own scripts.

Setting passwords in script

To set a password on a Print section in a Control Script, the script should first retrieve the Print section/s using merge.template.contexts.PRINT.sections or merge.context.sections (also see the example below).
Next, the script can split the attachments, if needed (see Parts: splitting and renaming email attachments), and it can set a password on each section. For example:

  • merge.template.contexts.PRINT.sections['Section 2'].password = 'secret';

  • merge.template.contexts.PRINT.sections['Section 2'].ownerPassword = 'secret';

When producing a single attachment, the password(s) should be set on the first Print section.
When producing multiple attachments, it should be set on the first section of each part.

Password types

PDF allows for two types of passwords to be set on a secured PDF file: a user password and owner password. The user password allows a limited access to the file (e.g. printing or copying text from the PDF is not allowed). The owner password allows normal access to the file. The Email PDF password script sets both the user and owner password to the same value, so that when the recipient provides the password, he can manipulate the file without limitations.

In a Control Script:

  • password is used to set the user password and owner password for a PDF attachment to the same value.

  • ownerPassword is used to set the owner password for a PDF attachment. Setting only the owner password creates a secured PDF that can be freely viewed, but cannot be manipulated unless the owner password is provided. Note that the recipient needs Adobe Acrobat to do this, because the Acrobat Reader does not allow users to enter the owner password.

Removing a password

Passwords set in the Control Script override the password set through the Email PDF password script (see Email PDF password). This allows you to change or remove the password from a specific part. Removal is done by setting the password field to null or an empty string ("").

This script splits the Print output into two PDF attachments and sets a password for the second attachment.

Copy
var printSections;
if (channel == Channel.EMAIL) { // only when generating Email output
if (merge.context.type == ContextType.PRINT) { 
printSections = merge.template.contexts.PRINT.sections;
    printSections['Section 1'].part = 'PDFAttach1';
    printSections['Section 2'].part = 'PDFAttach2'
    printSections['Section 2'].password = 'secret';
    }
}