Adding PDF Encryption using PDFSignDll SDK
In order to encrypt a PDF file, the document must be opened first. If the document is already protected by a password, it must be specified before loading the document
PDFEncrypt crypt = new PDFEncrypt();
//password if the document is already encrypted
crypt.LoadedDocumentPassword ="D0cument P@ssword";
//load the PDF document
crypt.LoadPDFDocumentFromArray(File.ReadAllBytes("c:\\source.pdf"));
Password Security
Owner password is set for changing the rights of the PDF file. The owner password is not requested when opening the PDF file but it is requesting when you want to change something in the password protected PDF file that was restricted from modifying.
To set the owner password you can use:
//owner password is used to set the permissions crypt.OwnerPassword ="owner P@ssw0rD";
User password is for opening (and reading) the PDF file. If you define an user password before generating the PDF file, when someone will want to open the resulting PDF file it will have to enter the user password to open it. So only the ones that know this password can read/open the password protected PDF file.
To set the user password you can use:
//optionally, user password is used to open the document with a password crypt.UserPassword = "user P@ssw0rD";
Certificate Security
Encrypts a PDF document by using a certificate. The certificate can be loaded from Microsoft Store, PFX, CER, PKCS#7, or X509Certificate2 object.
//load the encryption certificate from .CER file
crypt.EncryptionCertificate = crypt.LoadCertificateFromCER("c:\\encryption_cert.cer");
//load the encryption certificate from Microsoft Store
crypt.EncryptionCertificate =
crypt.LoadCertificateFromStore(false, "", "Encryption Certificates",
"Set the encryption certificate", DigitalCertificateScope.ForEncryption);
Encryption Algorithms
The library can use Standard Encryption RC4 (40 bit), Standard Encryption RC4 (128 bit), and Enhanced Encryption AES (128 bit) – default.
crypt.EncryptionAlgorithm = PDFEncryptionAlgorithm.EnhancedEncryption128BitAES;
Document Restrictions
Setting document restrictions allows you to customize if a user can copy content, filling form fields, edit content, or print the PDF file.
The library can allow content copying, content copying for accessibility, filling of form fields, printing, or none of them.
crypt.DocumentRestrictions =
PDFDocumentRestrictions.AllowPrinting | PDFDocumentRestrictions.AllowContentCopying;
Encrypt a PDF Using Passwords
PDFEncrypt crypt = new PDFEncrypt();
//load the PDF document
crypt.LoadPDFDocumentFromArray(File.ReadAllBytes("c:\\source.pdf"));
//set the encryption method
crypt.EncryptionMethod = PDFEncryptionMethod.PasswordSecurity;
//set the owner password - for changing the rights of the PDF file.
crypt.OwnerPassword = "1234567";
//set the user password - required for opening (and reading) the PDF file
crypt.UserPassword = "User Password";
//set the encryption algorithm
crypt.EncryptionAlgorithm = PDFEncryptionAlgorithm.StandardEncryption128BitRC4;
//set the document restrictions
crypt.DocumentRestrictions = PDFDocumentRestrictions.AllowNone;
//save the encrypted PDF document on a file
File.WriteAllBytes("c:\\encrypted.pdf", crypt.EncryptPDFFile());
Encrypt a PDF Using Digital Certificates
PDFEncrypt crypt = new PDFEncrypt();
//load the PDF document
crypt.LoadPDFDocumentFromFile("c:\\source.pdf");
//set the encryption method
crypt.EncryptionMethod = PDFEncryptionMethod.CertificateSecurity;
//set the encryption algorithm
crypt.EncryptionAlgorithm = PDFEncryptionAlgorithm.StandardEncryption40BitRC4;
//set the document restrictions
crypt.DocumentRestrictions = PDFDocumentRestrictions.AllowPrinting;
//save the encrypted PDF document on a file
File.WriteAllBytes("c:\\encrypted.pdf", crypt.EncryptPDFFile());
Encryption methods are a free part of PDFSignDll SDK.
If you want to digitally sign a PDF document, please remember that the encryption must be done before the signing operation.
