How to Change PDF Password using C#

Learn how to change a PDF password using C# in this concise, step‑by‑step tutorial. We’ll guide you through setting up the development environment, walk you through every code change, and provide ready‑to‑run sample code that demonstrates changing a PDF file’s password with C#. Additionally, you’ll explore multiple options for customizing the encryption of the output PDF when applying new passwords to the target document.

Steps to Change PDF Password using C#

  1. Set the environment to use Conholdate.Total 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

This outline describes the method for changing the password of a PDF file using C#. The procedure begins with verifying the encryption of the target PDF file, which confirms its password protection. Upon verification, the PdfFileSecurity class is employed to associate with the target PDF file. The password is then altered by invoking the ChangePassword() method.

Code to Change Password of PDF using C#

This example shows how to change a PDF’s password using C#. First, it checks the IsEncrypted property of the PdfFileInfo class to see if the PDF is already protected. Then, using a PdfFileSecurity object, it applies a new password by providing the original owner password, a new owner password, a new user password, the desired document privileges, and the encryption key length (40, 128, or 256 bits) to secure the file.

By now you’ve walked through each step to change a PDF password with C#. When you’re ready to tackle the next enhancement, dive into our guide on adding a watermark to a PDF in C# — see how to add watermark to PDF in C#.

 English