Skip to content

Instantly share code, notes, and snippets.

@aspose-com-kb
Last active March 12, 2023 16:41
How to Change PDF Password using C#. For more details: https://kb.aspose.com/pdf/net/how-to-change-pdf-password-using-csharp/
using System.Security.AccessControl;
using Aspose.Pdf;
using Aspose.Pdf.Facades;
namespace AsposeProjects
{
class Program
{
static void Main(string[] args)
{
// Initialize license
License lic = new License();
lic.SetLicense("Aspose.Total.lic");
// Instantiate the PdfFileInfo object
PdfFileInfo fileInfo = new PdfFileInfo("PasswordPDF.pdf");
// Check if the PDF file is encrypted using a password
if (fileInfo.IsEncrypted == true)
{
// Instantiate a PdfFileSecurity object
PdfFileSecurity security = new PdfFileSecurity();
// Bind the PDF
security.BindPdf("PasswordPDF.pdf");
// Change the password
security.ChangePassword("old_password", "new_user_password", "new_owner_password", DocumentPrivilege.Print, KeySize.x256);
// Save the PDF
security.Save("UpdatedPasswordPDF.pdf");
}
System.Console.WriteLine("Done");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment