Right PDF SDK provides functions that enable file protection with either passwords or certificates. Call ZSSecurity_CheckPassword to check if the file is protected with an open password or a permissions password. Here are some common APIs for document protection listed in Table 3.12. For a complete list of APIs, please refer to ZSSecurity.h.

 


Table 3.12

  API Name Description
1 ZSSecurity_AddCertificateEncryptionRecipient Add Certificate Encryption Recipients.
2 ZSSecurity_RemoveCertificateEncryptionRecipient Remove Certificate Encryption Recipient.
3 ZSSecurity_NumCertificateEncryptionRecipient Get the number of Certificate Encryption Recipient.
4 ZSSecurity_StartCertificateEncryption Start Certificate Encryption.
5 ZSSecurity_CheckPassword Detect the type of password used to encrypt the PDF file.
6 ZSSecurity_Certificate_Add Add a certificate.
7 ZSSecurity_Certificate_Remove Remove a certificate.
8 ZSSecurity_SetCertificateHandler Set the callback for certificate encryption.
9 ZSSecurity_StartPasswordEncryption Start Password Encryption.

 


This is an example of using password encryption to protect a PDF document.

Example 1: Encrypt a PDF document using a user password 456 and an owner password 123.

PasswordEncryptionUIDataRec secData = {0};
secData.compatibility = ZPDF_PASSWORD_ENCRYPTION_COMPATIBILITY_ACROBAT7;
secData.encryptContent = ZPDF_ENCRYPTION_CONTENT_ALL_EXCEPT_METADATA;

secData.hasOwnerPW = ZS_TRUE;
memcpy(secData.ownerPW, “123”, 3);

secData.hasUserPW = ZS_TRUE;
memcpy(secData.userPW, “456”, 3);

secData.PrintAllowed = ZPDF_PRINT_NONE;
secData.ChangeAllowed = ZPDF_DOCUMENT_CHANGE_ALLOW_NONE;
secData.EnableAccess = ZS_TURE;
secData.EnableCopy = ZS_TRUE;
ret = ZSSecurity_StartPasswordEncryption(doc, &secData);

 

Example 2: Encrypt a PDF file with a certificate.

void *certificate;
ZSSecurity_CreateCertificateEncryptionData(ZSPDF_CIPHER_AES_128,ZPDF_ENCRYPTION_CONTE NT_ALL_EXCEPT_METADATA, &certificate);
ZSBStr publicKeyFile;
ZSBStr_InitConstString(publicKeyFile, “./cert.p7b”);

RecipientPermission permission = {ZS_TRUE, ZS_TRUE,ZPDF_DOCUMENT_CHANGE_ALLOW_MODIFY_PAGE, ZPDF_PRINT_LOW_RESOLUTION};
ZSSecurity_AddCertificateEncryptionRecipient(certificate, &publicKeyFile, permission);

ZSSecurity_StartCertificateEncryption(doc, certificate);

ZSDoc_Save(doc, &filename, ZSDocSaveFull, NULL);
ZSDoc_Close(doc);
ZSSecurity_CertificateEncryptionDataFree(certificate);