Let’s learn to secure our documents from unauthorized access. Previously we discussed adding text and image watermarks to the documents to avoid any illegal use. In this article, we will see how to add password protection to PDF documents to get them locked using C#. Additionally, we will change the existing password and also learn to remove the password to make the PDF unlocked.
The following topics are discussed below:
- .NET API for Password Protection of PDF Files
- Lock PDF Files by Adding Password
- Change PDF Password in C#
- How to Remove PDF Password – Unlock PDF
.NET API to Lock and Unlock PDF Files
To lock and unlock documents, we will use GroupDocs.Merger for .NET. This API enables adding, changing, and removing password security features for the documents within the .NET applications. Along with protecting and unprotecting PDF documents, the API provides many more features like merging and splitting that are mentioned in the documentation.
PM> Install-Package GroupDocs.Merger
Add Password to PDF in C# – Lock PDF
Let’s start by adding protection to the file by locking it with the password. The following steps show how to add password security to the PDF documents using C#.
- Define the password using the AddPasswordOptions class.
- Load the PDF file using Merger class.
- Lock the file by adding password using AddPassword method.
- Save the protected file using the Save method.
The following C# code adds the password to the PDF file for security.
Here is the output of the above code. When you try to open the PDF file, the editor or viewer will ask for the password to prove your authority.
Update Existing Password of PDF Files in C#
Oops! your password is probably exposed. Let’s quickly change it programmatically with the new and difficult one. The following steps allow you to change the current password of your PDF files within your .NET application in C#.
- Prepare the loading options using the current password.
- Prepare the update options using the new password.
- Load the PDF document using Merger class and the loading options.
- Change the existing password using UpdatePassword method.
- Save the locked file having changed password using the Save method.
Here is the code snippet that changes the current password of the PDF document.
Remove PDF Files’ Password in C# – Unlock PDF
Now, I think you do not need security, that’s why you want to remove the password. Let’s open the file first and then remove its password so that everyone can access it easily. The following steps show how to unlock the PDF file by removing its password using C#.
- Prepare the loading options using the file’s password.
- Load the PDF document using Merger class and the load options.
- Remove the existing password using RemovePassword method.
- Save the unlocked file using the Save method.
The following C# code snippet unlocks the PDF file by removing its existing password, hence anyone can access it without authorization.
Let’s sum up what we learned today. We started with the simple PDF document and added password protection. Then we changed the existing password of that PDF file. In the end, we learned how to remove the password of our PDF documents. Now you can jump to build your own password protector or password remover application using the .NET API.
Get a Free API License
You can get a free temporary license to use the API without the evaluation limitations.