We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

How to set password on PDF using PDFFileSecurity object

I had posted last week asking for help but the answer I was given did not fully answer my question. I posted a follow up but no one has responded to that.


I can set various security settings on a PDF using PDFFileSecurity. How do I set a password on it for opening it up? I tried calling ChangePassword, but that does not do it. In fact, I’m not sure what it does, but after the call the PDF does not have a password set on it.

I tried calling ChangePassword(String.Empty, password, password) on it, passing in String.Empty since the file did not previously have a password on it. After the call, and also calling Save to save the file to disk, opening the file does not prompt for a password.

Hi there,


Sorry for the inconvenience faced. We’ve answered your question regarding password setting here. Hopefully it will help you to set password on exiting PDF file. Please check following code snippet to decrypt PDF file. If issue exist then please share your sample document here. So we will investigate it and will provide you information accordingly.


//open document
PdfFileSecurity fileSecurity = new PdfFileSecurity();
fileSecurity.BindPdf(myDir + “input.pdf”);
//decrypt PDF document
fileSecurity.DecryptFile(“xxx”);
fileSecurity.Save(myDir + “output.pdf”);

Please feel free to contact us for any further assitance.

Best Regards,

Hi, sorry, this was not my question.


I need Adobe to prompt the user for a password when they open the file. Calling ChangePassword and then Save as I described above does not do this. Adobe opens the file without prompting for a password.

I did post in the other thread that I did finally get it working, but it took quite a while and I wish the documentation was more detailed about the proper use of these methods and parameters.

Thanks!

Hi Marina,


Thanks for using our products and sorry for the confusion & inconvenience which you have faced.

As per your requirement, you need to encrypt the source PDF file so that whenever you open the PDF file in Adobe Reader, it prompts for supplying the password information. You may consider using the following code snippet to accomplish your requirements.

I have tested the scenario using Aspose.Pdf for .NET 8.2.0 and one of my sample PDF files and as per my observations, Adobe Reader prompts for password when I try to view it. Also please visit the following link for further information on Encrypt PDF File using Different Encryption Types and Algorithms

[C#]

//open document<o:p></o:p>

Document document = new Document("c:/pdftest/SimpelPDFfile.pdf");

//encrypt PDF

document.Encrypt("user", "owner", 0, CryptoAlgorithm.RC4x128);

//save updated PDF

document.Save(“c:/pdftest/Password_Protected.pdf”);