![]() | Working with Security |
This topic contains the following sections:
If the PDF documents contain sensitive information, they can be encrypted so that only authorized users can access them. Patagames PDF SDK allows you to protect the PDF document using encryption and set permission to the PDF document operations like printing, editing, copy content etc. using user password and owner password.
To set a password, use the PdfDocument.SetPasswordProtection(string openPassword, string permissionsPassword = null, PdfUserAccessPermission permissions= PdfUserAccessPermission.PermitAll, bool encryptMetadata = true, EncriptionAlgorithm algorithm = EncriptionAlgorithm.AES128) method. This method takes five parameters, some of which are optional.
openPassword (also known as User Password): prevents people from opening or viewing a PDF document. Once the User Password is set, to open the PDF document, Adobe Acrobat/Reader will prompt a user to enter this password. If it is not correct, the document will not open. By setting a PDF User Password, you can secure the PDF document.
permissionsPassword (Owner Password): sets PDF document restrictions, which can include printing, content copying, editing, page extracting, commenting, and more. Once the owner password is set, Acrobat will require this password to make any changes to the PDF document. It further secures the PDF document to set a PDF Owner Password.
permissions - The permission flags such as printing, content copying, editing etc.
encryptMetadata Indicates whether the document-level metadata stream is to be encrypted.
algorithm - The encryption algorithm. Two types of encryption algorithms are available:
Advanced Encryption Standard (AES)
Alleged Rivest Cipher 4 (RC4)
The following code snippet illustrates how to encrypt the PDF document with the User Password.
PdfDocument doc = ... //Open an existing PDF document or create new doc.SetPasswordProtection("UserPassword"); //Security settings will not be applied to the document until you save the document. doc.Save(@"sample-secured.pdf", SaveFlags.NoIncremental);
You can protect the PDF document from printing, editing, copying with the Owner Password by using the following code snippet.
doc.SetPasswordProtection(null, "OwnerPassword");
doc.SetPasswordProtection("UserPassword", "OwnerPassword");
![]() |
---|
While using both user and owner passwords, please specify different user and owner password while encrypting the PDF document for better security. |
doc.SetPasswordProtection(null, "OwnerPassword", PdfUserAccessPermission.NoPermissions);
In the above code the PdfUserAccessPermission.NoPermissions flag is passed through 3rd parameter (permissions). This flag means that everything is prohibited.
The following code sets the permissions to print the document in high and low quality.
doc.SetPasswordProtection(null, "OwnerPassword", PdfUserAccessPermission.PermitPrint | PdfUserAccessPermission.PermitFullQualityPrint | PdfUserAccessPermission.Reserved);
![]() |
---|
The PDF specification also requires the setting of reserved bits. For this purpose, you can use the Reserved flag, which is a combination of reserved bits. |
doc.SetPasswordProtection("UserPassword", null, PdfUserAccessPermission.PermitAll, false);
![]() |
---|
When you use User Password, the entire document content is encrypted. To exclude the document information (metadata) from encryption, pass FALSE in the 4th parameter. |
You can open an existing encrypted PDF document from either the file system or the stream or the byte array using the following overloads as shown below
using (var doc = PdfDocument.Load(@"c:\sample.pdf", null, "password")) { //... } using (var doc = PdfDocument.Load(stream, null, "password")) { //... } using (var doc = PdfDocument.Load(byteArray, null, "password")) { //... }
![]() |
---|
As a "password", both the User Password and the Owner Password can be specified. |
You can change the UserPassword of the existing PDF document by using following code snippet.
using (var doc = PdfDocument.Load("secured.pdf", null, "UserOrOwnerPassword")) { doc.SetPasswordProtection("NewUserPassword", "NewOwnerPassword", doc.Permission); doc.Save(@"secured-newpwd.pdf", SaveFlags.NoIncremental); }
You can remove all passwords from an encrypted PDF document by using the RemoveSecurity flag.
using (var doc = PdfDocument.Load("secured.pdf", null, "UserOrOwnerPassword")) { doc.Save(@"secured-newpwd.pdf", SaveFlags.NoIncremental | SaveFlags.RemoveSecurity); }
You can determine whether the existing PDF document is password protected or not by catching the InvalidPasswordException as shown below.
... try { using (var doc = PdfDocument.Load("secured.pdf")) { ProcessDocument(doc); } } catch (InvalidPasswordException) { using (var doc = PdfDocument.Load("secured.pdf", null, "UserOrOwnerPassword")) { ProcessDocument(doc); } } ... private void ProcessDocument(PdfDocument doc) { //...process the Pdf document }