Set Privileges, Encrypt and Decrypt PDF File

Set Privileges on an Existing PDF File

To set privileges on a PDF file, create an object of the DocumentPrivilege class and specify the rights you want to apply to the document. Once the privileges have been defined, pass this object as an argument to the Document object’s Encrypt method. The following code snippet shows you how to set the privileges of a PDF file.

  public static void SetPrivilegesOnExistingPDF()
  {
   // Load a source PDF file
   Document document = new Document(_dataDir + "input.pdf");

   // Instantiate Document Privileges object
   // Apply restrictions on all privileges
   DocumentPrivilege documentPrivilege = DocumentPrivilege.getForbidAll();
   // Only allow screen reading
   documentPrivilege.setAllowScreenReaders(true);
   // Encrypt the file with User and Owner password.
   // Need to set the password, so that once the user views the file with user password,
   // Only screen reading option is enabled
   document.encrypt("user", "owner", documentPrivilege, CryptoAlgorithm.AESx128, false);
   // Save updated document
   document.save(_dataDir + "SetPrivileges_out.pdf");
  }

Encrypt PDF File using Different Encryption Types and Algorithms

You can use the Encrypt method of the Document object to encrypt a PDF file. You can pass the user password, owner password, and permissions to the Encrypt method. In addition to that, you can pass any value of the CryptoAlgorithm enum. This enum provides different combinations of encryption algorithms and key sizes. You can pass the value of your choice. Finally, save the encrypted PDF file using save(..) method of the Document object.

Please specify different user and owner passwords while encrypting the PDF file.

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

public static void EncryptPDFFile() {
   // Load a source PDF file
   Document document = new Document(_dataDir + "input.pdf");

   // Instantiate Document Privileges object
   // Apply restrictions on all privileges
   DocumentPrivilege documentPrivilege = DocumentPrivilege.getForbidAll();
   // Only allow screen reading
   documentPrivilege.setAllowScreenReaders(true);
   // Encrypt the file with User and Owner password.
   // Need to set the password, so that once the user views the file with user
   // password,
   // Only screen reading option is enabled
   document.encrypt("user", "owner", documentPrivilege, CryptoAlgorithm.AESx128, false);
   // Save updated document
   document.save(_dataDir + "SetPrivileges_out.pdf");
  }

Decrypt PDF File using Owner Password

In order to decrypt the PDF file, you first need to create a Document object and open the PDF using the owner’s password. After that, you need to call Decrypt method of the Document object. Finally, save the updated PDF file using Save method of the Document object. The following code snippet shows you how to decrypt the PDF file.

public static void DecryptPDFFile() {
   // Open document
   Document document = new Document(_dataDir + "Decrypt.pdf", "password");
   // Decrypt PDF
   document.decrypt();

   // Save updated PDF
   document.save(_dataDir + "Decrypt_out.pdf");
  }

Change Password of a PDF File

If you want to change the password of a PDF file, you first need to open the PDF file using owner password with the Document object. After that, you need to call the ChangePasswords method of the Document object. You need to pass the current owner password along with the new user password and new owner password to this method. Finally, save the updated PDF file using Save method of the Document object.

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

public static void ChangePassword_PDF_File() {
   // Open document
   Document document = new Document(_dataDir+ "ChangePassword.pdf", "owner");
   // Change password
   document.changePasswords("owner", "newuser", "newowner");
   // Save updated PDF
   document.save(_dataDir + "ChangePassword_out.pdf");
  }

How to - determine if the source PDF is password protected

Aspose.PDF for Java provides great capabilities of dealing with PDF documents. When using Document class of com.aspose.pdf package to open a PDF document which is password protected, we need to provide the password information as an argument to Document constructor and in case this information is not provided, an error message is generated. In fact when trying to open a PDF file with Document object, the constructor is trying to read the contents of PDF file and in case correct password is not provided, an error message is generated (it happens to prevent unauthorised access of document)

When dealing with encrypted PDF files, you may come across the scenario where you would be interested to detect if a PDF has an open password and/or an edit password. Sometimes there are documents which do not require password information while opening them, but they require information in order to edit the contents of file.. So in order to fulfil the above requirements, PdfFileInfo class present under com.aspose.pdf.facades package provides the methods which can help in determining the required information.

Get information about PDF document security

PdfFileInfo contains three methods to get information about PDF document security.

  1. getPasswordType() method returns PasswordType enumeration value:
    1. PasswordType.None - document is not password protected
    2. PasswordType.User - document was opened with user (or document open) password
    3. PasswordType.Owner - document was opened with owner (or permissions, edit) password
    4. PasswordType.Inaccessible - document is password protected but password is needed to open it while invalid password (or no password) was supplied.
  2. hasOpenPassword() method is used to determine if input file requires password, when opening it.
  3. hasEditPassword() method is used to determine if input file requires password to edit its contents.

Determine correct password from Array

Sometimes there is a requirement to determine the correct password from an array of passwords and open the document with the correct password. The following code snippet demonstrates the steps to iterate through the array of passwords and try opening the document with the correct password.

public static void DetermineCorrectPasswordFromArray() {
     // Load source PDF file
   PdfFileInfo info = new PdfFileInfo();
   info.bindPdf(_dataDir + "IsPasswordProtected.pdf");
   // Determine if the source PDF is encrypted
   System.out.println("File is password protected " + info.isEncrypted());
   String[] passwords = { "test", "test1", "test2", "test3", "sample" };
   for (int passwordcount = 0; passwordcount < passwords.length; passwordcount++)
   {
    try
    {
     Document doc = new Document(_dataDir + "IsPasswordProtected.pdf", passwords[passwordcount]);
     if (doc.getPages().size() > 0)
      System.out.println("Number of Page in document are = " + doc.getPages().size());
    }
    catch (InvalidPasswordException ex)
    {
     System.out.println("Password = " + passwords[passwordcount] + "  is not correct");
    }
   }