Click or drag to resize

Working with Security

This topic contains the following sections:

Overview

If the PDF documents contain sensitive information, they can be encrypted so that only authorized users can access them. Patagames PDF SDK allows you to protect the PDF document using encryption and set permission to the PDF document operations like printing, editing, copy content etc. using user password and owner password.

Protect a PDF document with password

To set a password, use the PdfDocument.SetPasswordProtection(string openPassword, string permissionsPassword = null, PdfUserAccessPermission permissions= PdfUserAccessPermission.PermitAll, bool encryptMetadata = true, EncriptionAlgorithm algorithm = EncriptionAlgorithm.AES128) method. This method takes five parameters, some of which are optional.

openPassword (also known as User Password): prevents people from opening or viewing a PDF document. Once the User Password is set, to open the PDF document, Adobe Acrobat/Reader will prompt a user to enter this password. If it is not correct, the document will not open. By setting a PDF User Password, you can secure the PDF document.

permissionsPassword (Owner Password): sets PDF document restrictions, which can include printing, content copying, editing, page extracting, commenting, and more. Once the owner password is set, Acrobat will require this password to make any changes to the PDF document. It further secures the PDF document to set a PDF Owner Password.

permissions - The permission flags such as printing, content copying, editing etc.

encryptMetadata Indicates whether the document-level metadata stream is to be encrypted.

algorithm - The encryption algorithm. Two types of encryption algorithms are available:

  1. Advanced Encryption Standard (AES)

  2. Alleged Rivest Cipher 4 (RC4)

Protect a PDF document with user password

The following code snippet illustrates how to encrypt the PDF document with the User Password.

C#
  PdfDocument doc = ... //Open an existing PDF document or create new

doc.SetPasswordProtection("UserPassword");

//Security settings will not be applied to the document until you save the document.
doc.Save(@"sample-secured.pdf", SaveFlags.NoIncremental);

Protect a PDF document with owner password

You can protect the PDF document from printing, editing, copying with the Owner Password by using the following code snippet.

C#
doc.SetPasswordProtection(null, "OwnerPassword");

Protect a PDF document with both user password and owner password

C#
doc.SetPasswordProtection("UserPassword", "OwnerPassword");
Note  Note

While using both user and owner passwords, please specify different user and owner password while encrypting the PDF document for better security.

Set the permissions to the PDF document

C#
doc.SetPasswordProtection(null, "OwnerPassword", PdfUserAccessPermission.NoPermissions);

In the above code the PdfUserAccessPermission.NoPermissions flag is passed through 3rd parameter (permissions). This flag means that everything is prohibited.

The following code sets the permissions to print the document in high and low quality.

C#
doc.SetPasswordProtection(null, "OwnerPassword", PdfUserAccessPermission.PermitPrint | PdfUserAccessPermission.PermitFullQualityPrint | PdfUserAccessPermission.Reserved);
Important note  Important

The PDF specification also requires the setting of reserved bits. For this purpose, you can use the Reserved flag, which is a combination of reserved bits.

Encrypt all contents except metadata

C#
doc.SetPasswordProtection("UserPassword", null, PdfUserAccessPermission.PermitAll, false);
Note  Note

When you use User Password, the entire document content is encrypted. To exclude the document information (metadata) from encryption, pass FALSE in the 4th parameter.

Opening an encrypted PDF document

You can open an existing encrypted PDF document from either the file system or the stream or the byte array using the following overloads as shown below

C#
using (var doc = PdfDocument.Load(@"c:\sample.pdf", null, "password"))
{
    //...
}

using (var doc = PdfDocument.Load(stream, null, "password"))
{
    //...
}

using (var doc = PdfDocument.Load(byteArray, null, "password"))
{
    //...
}
Tip  Tip

As a "password", both the User Password and the Owner Password can be specified.

Changing the password of the PDF document

You can change the UserPassword of the existing PDF document by using following code snippet.

C#
using (var doc = PdfDocument.Load("secured.pdf", null, "UserOrOwnerPassword"))
{
    doc.SetPasswordProtection("NewUserPassword", "NewOwnerPassword", doc.Permission);

    doc.Save(@"secured-newpwd.pdf", SaveFlags.NoIncremental);
}
Remove passwords from the PDF document

You can remove all passwords from an encrypted PDF document by using the RemoveSecurity flag.

C#
using (var doc = PdfDocument.Load("secured.pdf", null, "UserOrOwnerPassword"))
{
    doc.Save(@"secured-newpwd.pdf", SaveFlags.NoIncremental | SaveFlags.RemoveSecurity);
}
How to determine whether the PDF document is password protected or not?

You can determine whether the existing PDF document is password protected or not by catching the InvalidPasswordException as shown below.

C#
  ...

    try
    {
        using (var doc = PdfDocument.Load("secured.pdf"))
        {
            ProcessDocument(doc);
        }

    }
    catch (InvalidPasswordException)
    {
        using (var doc = PdfDocument.Load("secured.pdf", null, "UserOrOwnerPassword"))
        {
            ProcessDocument(doc);
        }

    }
  ...

private void ProcessDocument(PdfDocument doc)
{
    //...process the Pdf document
}
See Also