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

Free Support Forum - aspose.com

Change security options of a PDF document

I have an existing PDF document that is in a stream. What I need to do is to set security options on it and save to disk.


The Document class does not have any security options.

The Pdf class in the Generator namespace does, but I don’t see how to initialize it with an existing document. The Stream constructor is the stream to save to, not the input stream. And I don’t see how to create a Pdf that gets its input from a Document - otherwise I could create the Document from a stream, create a Pdf for that Document, set the security options and save.

How to do this?

Thanks

Hi Marina,

Thanks for contacting support. Please follow the instructions specified over Set Privileges on an Existing PDF File (Facades)

Thanks. However, I cannot set encryption or a password on this, but I can with the other Security object. Specifically, I need the equivalent of the Is128BitsEncrypted that is in the security object, and to set a password for the file.

Hi there,


Thanks for the additional information. Please check following code snippet and documentation link for the the encryption of an existing PDF document. You can load/save either to disk or stream. Hopefully it will serve the purpose.

PdfFileSecurity fileSecurity = new PdfFileSecurity();
fileSecurity.BindPdf(myDir + “Form.pdf”);
//encrypt file using 128-bit encryption
fileSecurity.EncryptFile(“userpassword”, “ownerpassword”, DocumentPrivilege.Print, KeySize.x128, Algorithm.AES);
fileSecurity.Save(myDir + “outputsec.pdf”);


Please feel free to contact us for any further assistance.

Best Regards,

Thank you.


What I realized, and I wish I saw this in the documentation because it is not at all obvious (unless it’s there and I missed it):

EncryptFile needs to be the place where you set the password. Do not call ChangePassword to try to do this. Trying to do a ChangePassword after EncryptFile doesn’t work.

Pass in the password you need as the ‘userpassword’ and leave ‘ownerpassword’ blank. They cannot be the same value as that throws the exception.

I think it would be helpful for the documentation to also include more info about what the difference between the two passwords is.

Thanks for your help.

marinka:
EncryptFile needs to be the place where you set the password. Do not call ChangePassword to try to do this. Trying to do a ChangePassword after EncryptFile doesn’t work.
Hi Marina,

EncryptFile(…) method is used to encrypt the input PDF document and set user/owner password so that whenever user/anyone tries to open the PDF file, it prompts for password through which the file is encrypted. However the ChangePassword(…) method is used to change an existing password applied over the PDF file and if you wish to encrypt the PDF file with new password key.

marinka:
I think it would be helpful for the documentation to also include more info about what the difference between the two passwords is.
Thanks for the intimation. I have updated the information regarding difference between two password over Encrypt PDF File using Different Encryption Types and Algorithms and other related links. In the event of any further query, please feel free to contact.

We are sorry for the delay and inconvenience.