Encrypt, Decrypt and Set Privileges on PDF File

Encrypt PDF File using Different Encryption Types and Algorithms

In order to encrypt a PDF file, you need to create PdfFileSecurity object and then call the EncryptFile method. You can pass user password, owner password and privileges to EncryptFile method. You also need to pass KeySize and Algorithm values to this method.

The following code snippet shows you how to encrypt PDF file.

Decrypt PDF File using Owner Password

In order to decrypt a PDF file, you need to create PdfFileSecurity object and then call the DecryptFile method. You also need to pass the owner password to DecryptFile method. The following code snippet shows you how to decrypt PDF file.

Set Privileges on an Existing PDF File

To set a PDF file’s privileges, create a PdfFileSecurity object and call the SetPrivilege method. You can specify the privileges using the DocumentPrivilege object and then pass this object to the SetPrivilege method. The following code snippet shows you how to set the privileges of a PDF file.

Change Password of a PDF File

In order to change password of a PDF file, you need to create PdfFileSecurity object and then call the ChangePassword method. You need to pass existing owner password and new user and owner passwords to the ChangePassword method.

The following code snippet shows you how to change passwords of a PDF file.

Remove Extended Rights Feature from the PDF

PDF documents support the extended rights feature to enable end-user to fill data into form fields by using Adobe Acrobat Reader and then save a copy of the filled form. However, it ensures that PDF file is not modified and if any modification to the structure of the PDF is made, the extended rights feature is lost. When viewing such a document, an error message is displayed, stating that extended rights are removed because the document was modified. Recently, we received a requirement to remove extended rights from PDF document.

To remove the extended rights from a PDF file, a new method named RemoveUsageRights() has been added to the PdfFileSignature class. Another method named ContainsUsageRights() is added to determine if source PDF contains extended rights.

The following code shows ho to remove usage rights from the document: