Visual FoxPro Requires Chilkat v11.7.0+
Visual FoxPro
Encrypt and Sign a PDF, and Sign an Encrypted PDF
See more PDF Protection Examples
Demonstrates signing together with encryption. A pending encrypt is applied by SignPdf before it signs, so one call produces a PDF that is both encrypted and signed. A PDF that is already encrypted can be signed too, and stays encrypted. When it was opened with only the user password its permissions decide: adding a signature field needs both the modify and the annotate permission. Loading the PDF with its owner password lifts that.Chilkat Visual FoxPro Downloads
LOCAL lnSuccess
LOCAL loCert
LOCAL loPdf
LOCAL loJson
LOCAL loJsonOut
LOCAL loSigOpts
LOCAL loCheck
LOCAL loEmpty
LOCAL loInfo
LOCAL loSigInfo
LOCAL lnVerified
LOCAL loOwner
LOCAL loTwice
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 signing certificate, with its private key.
loCert = CreateObject('Chilkat.Cert')
lnSuccess = loCert.LoadPfxFile("qa_data/pfx/myPdfSigningCert.pfx","secret")
IF (lnSuccess = 0) THEN
? loCert.LastErrorText
RELEASE loCert
CANCEL
ENDIF
loPdf = CreateObject('Chilkat.Pdf')
lnSuccess = loPdf.LoadFile("qa_data/pdf/two_page_memo.pdf")
IF (lnSuccess = 0) THEN
? loPdf.LastErrorText
RELEASE loCert
RELEASE loPdf
CANCEL
ENDIF
* Encrypt with AES-256 and do not permit modifying.
loPdf.UserPassword = "user-secret"
loPdf.OwnerPassword = "owner-secret"
loJson = CreateObject('Chilkat.JsonObject')
loJson.UpdateBool("permissions.modify",0)
loJsonOut = CreateObject('Chilkat.JsonObject')
lnSuccess = loPdf.Protection("encrypt",loJson,loJsonOut)
IF (lnSuccess = 0) THEN
? loPdf.LastErrorText
RELEASE loCert
RELEASE loPdf
RELEASE loJson
RELEASE loJsonOut
CANCEL
ENDIF
* The encryption is still pending. SignPdf applies it first and signs the encrypted result.
lnSuccess = loPdf.SetSigningCert(loCert)
IF (lnSuccess = 0) THEN
? loPdf.LastErrorText
RELEASE loCert
RELEASE loPdf
RELEASE loJson
RELEASE loJsonOut
CANCEL
ENDIF
loSigOpts = CreateObject('Chilkat.JsonObject')
loSigOpts.UpdateBool("invisibleSignature",1)
loSigOpts.UpdateInt("signingCertificateV2",1)
loSigOpts.UpdateInt("signingTime",1)
lnSuccess = loPdf.SignPdf(loSigOpts,"qa_output/memo_encrypted_signed.pdf")
IF (lnSuccess = 0) THEN
? loPdf.LastErrorText
RELEASE loCert
RELEASE loPdf
RELEASE loJson
RELEASE loJsonOut
RELEASE loSigOpts
CANCEL
ENDIF
* The result is encrypted and signed.
loCheck = CreateObject('Chilkat.Pdf')
loCheck.UserPassword = "user-secret"
lnSuccess = loCheck.LoadFile("qa_output/memo_encrypted_signed.pdf")
IF (lnSuccess = 0) THEN
? loCheck.LastErrorText
RELEASE loCert
RELEASE loPdf
RELEASE loJson
RELEASE loJsonOut
RELEASE loSigOpts
RELEASE loCheck
CANCEL
ENDIF
loEmpty = CreateObject('Chilkat.JsonObject')
loInfo = CreateObject('Chilkat.JsonObject')
lnSuccess = loCheck.Protection("getInfo",loEmpty,loInfo)
IF (lnSuccess = 0) THEN
? loCheck.LastErrorText
RELEASE loCert
RELEASE loPdf
RELEASE loJson
RELEASE loJsonOut
RELEASE loSigOpts
RELEASE loCheck
RELEASE loEmpty
RELEASE loInfo
CANCEL
ENDIF
? "algorithm: " + loInfo.StringOf("loaded.algorithm")
? "signatures: " + STR(loCheck.NumSignatures)
loSigInfo = CreateObject('Chilkat.JsonObject')
lnVerified = loCheck.VerifySignature(0,loSigInfo)
? "signature verifies: " + STR(lnVerified)
* Signing an encrypted PDF. This one was opened with only the user password, and its
* permissions do not allow modifying, so another signature is refused.
lnSuccess = loCheck.SetSigningCert(loCert)
IF (lnSuccess = 0) THEN
? loCheck.LastErrorText
RELEASE loCert
RELEASE loPdf
RELEASE loJson
RELEASE loJsonOut
RELEASE loSigOpts
RELEASE loCheck
RELEASE loEmpty
RELEASE loInfo
RELEASE loSigInfo
CANCEL
ENDIF
lnSuccess = loCheck.SignPdf(loSigOpts,"qa_output/memo_signed_twice.pdf")
IF (lnSuccess = 0) THEN
? "A second signature was refused: the permissions do not allow it."
ENDIF
* Loaded with the owner password, the same PDF can be signed. It stays encrypted, and the
* first signature stays valid because the new one is added as an incremental update.
loOwner = CreateObject('Chilkat.Pdf')
loOwner.UserPassword = "user-secret"
loOwner.OwnerPassword = "owner-secret"
lnSuccess = loOwner.LoadFile("qa_output/memo_encrypted_signed.pdf")
IF (lnSuccess = 0) THEN
? loOwner.LastErrorText
RELEASE loCert
RELEASE loPdf
RELEASE loJson
RELEASE loJsonOut
RELEASE loSigOpts
RELEASE loCheck
RELEASE loEmpty
RELEASE loInfo
RELEASE loSigInfo
RELEASE loOwner
CANCEL
ENDIF
lnSuccess = loOwner.SetSigningCert(loCert)
IF (lnSuccess = 0) THEN
? loOwner.LastErrorText
RELEASE loCert
RELEASE loPdf
RELEASE loJson
RELEASE loJsonOut
RELEASE loSigOpts
RELEASE loCheck
RELEASE loEmpty
RELEASE loInfo
RELEASE loSigInfo
RELEASE loOwner
CANCEL
ENDIF
lnSuccess = loOwner.SignPdf(loSigOpts,"qa_output/memo_signed_twice.pdf")
IF (lnSuccess = 0) THEN
? loOwner.LastErrorText
RELEASE loCert
RELEASE loPdf
RELEASE loJson
RELEASE loJsonOut
RELEASE loSigOpts
RELEASE loCheck
RELEASE loEmpty
RELEASE loInfo
RELEASE loSigInfo
RELEASE loOwner
CANCEL
ENDIF
loTwice = CreateObject('Chilkat.Pdf')
loTwice.UserPassword = "user-secret"
lnSuccess = loTwice.LoadFile("qa_output/memo_signed_twice.pdf")
IF (lnSuccess = 0) THEN
? loTwice.LastErrorText
RELEASE loCert
RELEASE loPdf
RELEASE loJson
RELEASE loJsonOut
RELEASE loSigOpts
RELEASE loCheck
RELEASE loEmpty
RELEASE loInfo
RELEASE loSigInfo
RELEASE loOwner
RELEASE loTwice
CANCEL
ENDIF
? "signatures after signing with owner rights: " + STR(loTwice.NumSignatures)
lnVerified = loTwice.VerifySignature(0,loSigInfo)
? "first signature still verifies: " + STR(lnVerified)
lnVerified = loTwice.VerifySignature(1,loSigInfo)
? "second signature verifies: " + STR(lnVerified)
* Sample output:
* algorithm: aes-256
* signatures: 1
* signature verifies: True
* A second signature was refused: the permissions do not allow it.
* signatures after signing with owner rights: 2
* first signature still verifies: True
* second signature verifies: True
RELEASE loCert
RELEASE loPdf
RELEASE loJson
RELEASE loJsonOut
RELEASE loSigOpts
RELEASE loCheck
RELEASE loEmpty
RELEASE loInfo
RELEASE loSigInfo
RELEASE loOwner
RELEASE loTwice