Method Encrypt
- Namespace
- SoftZenPdf
- Assembly
- SoftZenPdf.dll
Encrypt(string?, string?, PdfPermissions, PdfEncryptionAlgorithm)
Encrypts the document with the Standard Security Handler when it is saved.
public void Encrypt(string? userPassword, string? ownerPassword, PdfPermissions permissions = PdfPermissions.All, PdfEncryptionAlgorithm algorithm = PdfEncryptionAlgorithm.Aes256)
Parameters
userPasswordstringPassword needed to open the document; null or empty lets anyone open it while the permissions still apply.
ownerPasswordstringPassword that grants full access. If null or empty, a random one is generated so the user password can never grant owner rights.
permissionsPdfPermissionsWhat a user who opens the document with the user password may do.
algorithmPdfEncryptionAlgorithmThe cipher. Aes256 (the default) is recommended; the others exist only for old viewers, and RC4 is cryptographically broken.
Remarks
Permissions are advisory: viewers enforce them, but anyone who has the user password can decrypt the content. Calling this again replaces the previous settings.
Exceptions
- ArgumentException
A password contains characters the algorithm cannot encode (RC4 and AES-128 accept printable ASCII only).
- InvalidOperationException
The document was created for PDF/A, which forbids encryption.