Change Password of PDF File
Contents
[
Hide
]
Change Password of a PDF File
In order to change password of a PDF file, you need to create PdfFileSecurity object and then call the ChangePassword method. You need to pass existing owner password and new user and owner passwords to the ChangePassword method.
- The User password, if set, is what you need to provide in order to open a PDF. Acrobat/Reader will prompt a user to enter the user password. If it’s not correct, the document will not open.
- The Owner password, if set, controls permissions, such as printing, editing, extracting, commenting, etc. Acrobat/Reader will disallow these things based on the permission settings. Acrobat will require this password if you want to set/change permissions.
The following code snippet shows you how to change passwords of a PDF file.
// For complete examples and data files, check for https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void ChangePassword()
{
// The path to the documents directory
string dataDir = RunExamples.GetDataDir_AsposePdf_SecuritySignatures();
using (var pdfFileInfo = new Aspose.Pdf.Facades.PdfFileInfo(dataDir + "sample_encrypted.pdf"))
{
// Create PdfFileSecurity object if the document is encrypted
if (pdfFileInfo.IsEncrypted)
{
using (var fileSecurity = new Aspose.Pdf.Facades.PdfFileSecurity())
{
fileSecurity.BindPdf(dataDir + "sample_encrypted.pdf");
fileSecurity.ChangePassword("OwnerP@ssw0rd", "Pa$$w0rd1", "Pa$$w0rd2", Aspose.Pdf.Facades.DocumentPrivilege.Print, Aspose.Pdf.Facades.KeySize.x256);
// Save the document
fileSecurity.Save(dataDir + "sample_encrtypted1.pdf");
}
}
}
}