Modify Passwords of Encrypted PDF in C#/VB.NET

Modifying passwords of PDF file is really a rational choice especially when the passwords are known by someone and your PDF file is no longer safe. Spire.PDF for .NET enables you to modify passwords of your encrypted PDF file in C#, VB.NET. You can modify your owner password as well as user password and set user restrictions when access the PDF file. Now please see the process of modifying encrypted PDF passwords as below picture:

Modify PDF Passwords

From above picture, you can easily find that the first step is to decrypt PDF file by owner password. So the original owner password is necessary. You can decrypt it by this method: Spire.Pdf.PdfDocument(string filename, string password)

Then, modify passwords by resetting owner password and user password. PDFSecurity class which is in the namespace Spire.PDFDocument.Security can help you not only to set owner password and user password, but also can set user permissions to restrict user access.

Below shows the whole code of modifying passwords of encrypted PDF file, please download Spire.PDF for .NET and install it on system before following the code:

[C#]
using Spire.Pdf;
using Spire.Pdf.Security;

namespace modify_PDF_passwords
{
    class Program
    {
        static void Main(string[] args)
        {

            //load a encrypted file and decrypt it
            String encryptedPdf = @"..\Encrypt.pdf";
            PdfDocument doc = new PdfDocument(encryptedPdf, "e-iceblue");

            //reset PDF passwords and set user password permission
            doc.Security.OwnerPassword = "Spire.PDF";
            doc.Security.UserPassword = "pdfcomponent";
            doc.Security.Permissions = PdfPermissionsFlags.Print | PdfPermissionsFlags.FillFields;

            //Save pdf file.
            doc.SaveToFile("Encryption.pdf");
            doc.Close();
            //Launching the Pdf file.
            System.Diagnostics.Process.Start("Encryption.pdf");      
            
        }    
    }
}
[VB.NET]
Imports Spire.Pdf
Imports Spire.Pdf.Security

Namespace modify_PDF_passwords
	Class Program
		Private Shared Sub Main(args As String())

			'load a encrypted file and decrypt it
			Dim encryptedPdf As [String] = "..\Encrypt.pdf"
			Dim doc As New PdfDocument(encryptedPdf, "e-iceblue")

			'reset PDF passwords and set user password permission
			doc.Security.OwnerPassword = "Spire.PDF"
			doc.Security.UserPassword = "pdfcomponent"
			doc.Security.Permissions = PdfPermissionsFlags.Print Or PdfPermissionsFlags.FillFields

			'Save pdf file.
			doc.SaveToFile("Encryption.pdf")
			doc.Close()
			'Launching the Pdf file.
			System.Diagnostics.Process.Start("Encryption.pdf")

		End Sub
	End Class
End Namespace

Spire.PDF for .NET is a .NET PDF component that enables you to generate, read, edit and manipulate PDF files in C#, VB.NET.