How to Change PDF Password using C#

This short tutorial describes the process of how to change PDF password using C#. It contains the references to the resources required to establish the environment, a list of steps to be performed while writing the application and a runnable sample code that can be used to change password of PDF file using C#. Different options are also discussed to customize the encryption of the resultant PDF file while setting the new passwords for the target PDF file.

Steps to Change PDF Password using C#

  1. Set the environment to use Aspose.PDF for .NET for changing the password
  2. Instantiate an object of the PdfFileInfo class by providing the file name of the password protected PDF
  3. Check if the target PDF file is encrypted by a password and perform the rest of the steps
  4. Create an object of PdfFileSecurity class to change the password
  5. Bind the target PDF file with the PdfFileSecurity object
  6. Call the ChangePassword() method to change the owner and user passwords
  7. Save the resultant PDF file having changed passwords

These steps summarize the process on how to change password of PDF file using C#. The process is commenced by the confirmation of the target PDF file encryption as it indicates that the PDF file is password protected. Once the protection is confirmed, the PdfFileSecurity class is used to bind the target PDF file that actually changes the password by calling the ChangePassword() method.

Code to Change Password of PDF using C#

This code demonstrates the process of how to change password for PDF using C#. It uses the IsEncrypted property of the PdfFileInfo class that is true only if the target PDF file is encrypted by a password. The PdfFileSecurity object is used to change the password where owner’s old password, new password for the owner, new password for the user, the document privileges that are to be allowed and the key size of the key like 40, 128, or 256 that is to be used to encrypt the PDF file.

In this topic we have learned the process of how to change a PDF password using C#. If you want to learn the basic process of setting a password of a plane PDF file, refer to the article on how to protect PDF file with password in C#.

 English