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/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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