Xbase++ Requires Chilkat v11.7.0+
Xbase++
Encrypt a PDF for a Certificate Instead of a Password
See more PDF Protection Examples
Demonstrates certificate security, where a PDF is encrypted for one or more recipients and each opens it with their own certificate and private key. No password is involved. Only the public certificate of a recipient is needed to encrypt. Each recipient has its own permissions, and the changeSecurity permission gives a recipient owner rights. PreLoadCheck tells an application that a file is certificate-encrypted and which certificates can open it. A PDF that cannot be opened, because no matching certificate and private key were provided, fails to load.Chilkat Xbase++ Downloads
LOCAL nSuccess
LOCAL oRecipient
LOCAL oPdf
LOCAL oJson
LOCAL oJsonOut
LOCAL oReader
LOCAL oInfo
LOCAL oMyCert
LOCAL oEmpty
LOCAL oLoadedInfo
nSuccess := 0
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// The PDF used in this example is available at two_page_memo.pdf
// The recipient's certificate. Only the public certificate is needed to encrypt.
oRecipient := CreateObject("Chilkat.Cert")
nSuccess := oRecipient:LoadFromFile("qa_data/certs/recipient.cer")
IF (nSuccess == 0)
? oRecipient:LastErrorText
oRecipient:destroy()
RETURN
ENDIF
oPdf := CreateObject("Chilkat.Pdf")
nSuccess := oPdf:LoadFile("qa_data/pdf/two_page_memo.pdf")
IF (nSuccess == 0)
? oPdf:LastErrorText
oRecipient:destroy()
oPdf:destroy()
RETURN
ENDIF
// Call AddEncryptCert once for each recipient.
nSuccess := oPdf:AddEncryptCert(oRecipient)
IF (nSuccess == 0)
? oPdf:LastErrorText
oRecipient:destroy()
oPdf:destroy()
RETURN
ENDIF
// recipientPermissions[0] belongs to the first certificate added. changeSecurity gives that
// recipient owner rights, so the file can be managed later.
oJson := CreateObject("Chilkat.JsonObject")
oJson:UpdateString("method", "certificate")
oJson:UpdateBool("recipientPermissions[0].changeSecurity", 1)
oJsonOut := CreateObject("Chilkat.JsonObject")
nSuccess := oPdf:Protection("encrypt", oJson, oJsonOut)
IF (nSuccess == 0)
? oPdf:LastErrorText
oRecipient:destroy()
oPdf:destroy()
oJson:destroy()
oJsonOut:destroy()
RETURN
ENDIF
? "method: " + oJsonOut:StringOf("pending.method")
? "algorithm: " + oJsonOut:StringOf("pending.algorithm")
? "recipients: " + Str(oJsonOut:SizeOfArray("pending.recipients"))
nSuccess := oPdf:WriteFile("qa_output/memo_for_recipient.pdf")
IF (nSuccess == 0)
? oPdf:LastErrorText
oRecipient:destroy()
oPdf:destroy()
oJson:destroy()
oJsonOut:destroy()
RETURN
ENDIF
// PreLoadCheck says how the file is protected, without loading it.
oReader := CreateObject("Chilkat.Pdf")
oInfo := CreateObject("Chilkat.JsonObject")
nSuccess := oReader:PreLoadCheck("qa_output/memo_for_recipient.pdf", oInfo)
IF (nSuccess == 0)
? oReader:LastErrorText
oRecipient:destroy()
oPdf:destroy()
oJson:destroy()
oJsonOut:destroy()
oReader:destroy()
oInfo:destroy()
RETURN
ENDIF
? "saved file, method: " + oInfo:StringOf("method")
? "saved file, recipients: " + Str(oInfo:SizeOfArray("recipients"))
// The recipient opens it with the certificate and its private key, here from a .pfx file.
// (On Windows, a certificate that is installed in the Windows certificate store with its private
// key is found automatically when the PDF is loaded, and SetDecryptCert is not needed for it.)
oMyCert := CreateObject("Chilkat.Cert")
nSuccess := oMyCert:LoadPfxFile("qa_data/pfx/recipient.pfx", "pfx-password")
IF (nSuccess == 0)
? oMyCert:LastErrorText
oRecipient:destroy()
oPdf:destroy()
oJson:destroy()
oJsonOut:destroy()
oReader:destroy()
oInfo:destroy()
oMyCert:destroy()
RETURN
ENDIF
nSuccess := oReader:SetDecryptCert(oMyCert)
IF (nSuccess == 0)
? oReader:LastErrorText
oRecipient:destroy()
oPdf:destroy()
oJson:destroy()
oJsonOut:destroy()
oReader:destroy()
oInfo:destroy()
oMyCert:destroy()
RETURN
ENDIF
nSuccess := oReader:LoadFile("qa_output/memo_for_recipient.pdf")
IF (nSuccess == 0)
? oReader:LastErrorText
oRecipient:destroy()
oPdf:destroy()
oJson:destroy()
oJsonOut:destroy()
oReader:destroy()
oInfo:destroy()
oMyCert:destroy()
RETURN
ENDIF
oEmpty := CreateObject("Chilkat.JsonObject")
oLoadedInfo := CreateObject("Chilkat.JsonObject")
nSuccess := oReader:Protection("getInfo", oEmpty, oLoadedInfo)
IF (nSuccess == 0)
? oReader:LastErrorText
oRecipient:destroy()
oPdf:destroy()
oJson:destroy()
oJsonOut:destroy()
oReader:destroy()
oInfo:destroy()
oMyCert:destroy()
oEmpty:destroy()
oLoadedInfo:destroy()
RETURN
ENDIF
? "opened with: " + oLoadedInfo:StringOf("loaded.openedWith")
? "pages: " + Str(oReader:NumPages)
// Sample output:
// method: certificate
// algorithm: aes-256
// recipients: 1
// saved file, method: certificate
// saved file, recipients: 1
// opened with: certificate
// pages: 2
oRecipient:destroy()
oPdf:destroy()
oJson:destroy()
oJsonOut:destroy()
oReader:destroy()
oInfo:destroy()
oMyCert:destroy()
oEmpty:destroy()
oLoadedInfo:destroy()