Visual FoxPro Requires Chilkat v11.7.0+
Visual FoxPro
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 Visual FoxPro Downloads
LOCAL lnSuccess
LOCAL loRecipient
LOCAL loPdf
LOCAL loJson
LOCAL loJsonOut
LOCAL loReader
LOCAL loInfo
LOCAL loMyCert
LOCAL loEmpty
LOCAL loLoadedInfo
lnSuccess = 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.
loRecipient = CreateObject('Chilkat.Cert')
lnSuccess = loRecipient.LoadFromFile("qa_data/certs/recipient.cer")
IF (lnSuccess = 0) THEN
? loRecipient.LastErrorText
RELEASE loRecipient
CANCEL
ENDIF
loPdf = CreateObject('Chilkat.Pdf')
lnSuccess = loPdf.LoadFile("qa_data/pdf/two_page_memo.pdf")
IF (lnSuccess = 0) THEN
? loPdf.LastErrorText
RELEASE loRecipient
RELEASE loPdf
CANCEL
ENDIF
* Call AddEncryptCert once for each recipient.
lnSuccess = loPdf.AddEncryptCert(loRecipient)
IF (lnSuccess = 0) THEN
? loPdf.LastErrorText
RELEASE loRecipient
RELEASE loPdf
CANCEL
ENDIF
* recipientPermissions[0] belongs to the first certificate added. changeSecurity gives that
* recipient owner rights, so the file can be managed later.
loJson = CreateObject('Chilkat.JsonObject')
loJson.UpdateString("method","certificate")
loJson.UpdateBool("recipientPermissions[0].changeSecurity",1)
loJsonOut = CreateObject('Chilkat.JsonObject')
lnSuccess = loPdf.Protection("encrypt",loJson,loJsonOut)
IF (lnSuccess = 0) THEN
? loPdf.LastErrorText
RELEASE loRecipient
RELEASE loPdf
RELEASE loJson
RELEASE loJsonOut
CANCEL
ENDIF
? "method: " + loJsonOut.StringOf("pending.method")
? "algorithm: " + loJsonOut.StringOf("pending.algorithm")
? "recipients: " + STR(loJsonOut.SizeOfArray("pending.recipients"))
lnSuccess = loPdf.WriteFile("qa_output/memo_for_recipient.pdf")
IF (lnSuccess = 0) THEN
? loPdf.LastErrorText
RELEASE loRecipient
RELEASE loPdf
RELEASE loJson
RELEASE loJsonOut
CANCEL
ENDIF
* PreLoadCheck says how the file is protected, without loading it.
loReader = CreateObject('Chilkat.Pdf')
loInfo = CreateObject('Chilkat.JsonObject')
lnSuccess = loReader.PreLoadCheck("qa_output/memo_for_recipient.pdf",loInfo)
IF (lnSuccess = 0) THEN
? loReader.LastErrorText
RELEASE loRecipient
RELEASE loPdf
RELEASE loJson
RELEASE loJsonOut
RELEASE loReader
RELEASE loInfo
CANCEL
ENDIF
? "saved file, method: " + loInfo.StringOf("method")
? "saved file, recipients: " + STR(loInfo.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.)
loMyCert = CreateObject('Chilkat.Cert')
lnSuccess = loMyCert.LoadPfxFile("qa_data/pfx/recipient.pfx","pfx-password")
IF (lnSuccess = 0) THEN
? loMyCert.LastErrorText
RELEASE loRecipient
RELEASE loPdf
RELEASE loJson
RELEASE loJsonOut
RELEASE loReader
RELEASE loInfo
RELEASE loMyCert
CANCEL
ENDIF
lnSuccess = loReader.SetDecryptCert(loMyCert)
IF (lnSuccess = 0) THEN
? loReader.LastErrorText
RELEASE loRecipient
RELEASE loPdf
RELEASE loJson
RELEASE loJsonOut
RELEASE loReader
RELEASE loInfo
RELEASE loMyCert
CANCEL
ENDIF
lnSuccess = loReader.LoadFile("qa_output/memo_for_recipient.pdf")
IF (lnSuccess = 0) THEN
? loReader.LastErrorText
RELEASE loRecipient
RELEASE loPdf
RELEASE loJson
RELEASE loJsonOut
RELEASE loReader
RELEASE loInfo
RELEASE loMyCert
CANCEL
ENDIF
loEmpty = CreateObject('Chilkat.JsonObject')
loLoadedInfo = CreateObject('Chilkat.JsonObject')
lnSuccess = loReader.Protection("getInfo",loEmpty,loLoadedInfo)
IF (lnSuccess = 0) THEN
? loReader.LastErrorText
RELEASE loRecipient
RELEASE loPdf
RELEASE loJson
RELEASE loJsonOut
RELEASE loReader
RELEASE loInfo
RELEASE loMyCert
RELEASE loEmpty
RELEASE loLoadedInfo
CANCEL
ENDIF
? "opened with: " + loLoadedInfo.StringOf("loaded.openedWith")
? "pages: " + STR(loReader.NumPages)
* Sample output:
* method: certificate
* algorithm: aes-256
* recipients: 1
* saved file, method: certificate
* saved file, recipients: 1
* opened with: certificate
* pages: 2
RELEASE loRecipient
RELEASE loPdf
RELEASE loJson
RELEASE loJsonOut
RELEASE loReader
RELEASE loInfo
RELEASE loMyCert
RELEASE loEmpty
RELEASE loLoadedInfo