Visual FoxPro Requires Chilkat v11.7.0+
Visual FoxPro
Password-Protect a PDF with AES-256 Encryption
See more PDF Protection Examples
Demonstrates the simplest way to encrypt a PDF so that it needs a password to open. The passwords come from the UserPassword and OwnerPassword properties, and Protection with the encrypt operation and an empty JSON uses the defaults: AES-256, everything encrypted, every permission granted. Protection only records the security the PDF will be saved with. Nothing is encrypted until WriteFile is called.Chilkat Visual FoxPro Downloads
LOCAL lnSuccess
LOCAL loPdf
LOCAL loJson
LOCAL loJsonOut
LOCAL loChecker
LOCAL loInfo
LOCAL loReader
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
loPdf = CreateObject('Chilkat.Pdf')
lnSuccess = loPdf.LoadFile("qa_data/pdf/two_page_memo.pdf")
IF (lnSuccess = 0) THEN
? loPdf.LastErrorText
RELEASE loPdf
CANCEL
ENDIF
* The passwords are taken from these two properties at the time Protection is called.
* The user password opens the PDF. The owner password opens it too, and is the one needed
* later to change or remove the protection.
loPdf.UserPassword = "user-secret"
loPdf.OwnerPassword = "owner-secret"
* An empty JSON asks for the defaults: AES-256, everything encrypted, every permission granted.
loJson = CreateObject('Chilkat.JsonObject')
loJsonOut = CreateObject('Chilkat.JsonObject')
lnSuccess = loPdf.Protection("encrypt",loJson,loJsonOut)
IF (lnSuccess = 0) THEN
? loPdf.LastErrorText
RELEASE loPdf
RELEASE loJson
RELEASE loJsonOut
CANCEL
ENDIF
* Nothing has been encrypted yet. "pending" describes the security the PDF will be saved with.
? "algorithm: " + loJsonOut.StringOf("pending.algorithm")
? "PDF version: " + loJsonOut.StringOf("pending.pdfVersion")
? "password needed to open: " + STR(loJsonOut.BoolOf("pending.userPasswordRequired"))
* Writing the file applies it.
lnSuccess = loPdf.WriteFile("qa_output/memo_encrypted.pdf")
IF (lnSuccess = 0) THEN
? loPdf.LastErrorText
RELEASE loPdf
RELEASE loJson
RELEASE loJsonOut
CANCEL
ENDIF
* PreLoadCheck reports a file's encryption without loading it, and needs no password.
loChecker = CreateObject('Chilkat.Pdf')
loInfo = CreateObject('Chilkat.JsonObject')
lnSuccess = loChecker.PreLoadCheck("qa_output/memo_encrypted.pdf",loInfo)
IF (lnSuccess = 0) THEN
? loChecker.LastErrorText
RELEASE loPdf
RELEASE loJson
RELEASE loJsonOut
RELEASE loChecker
RELEASE loInfo
CANCEL
ENDIF
? "encrypted: " + STR(loInfo.BoolOf("encrypted"))
? "openPasswordRequired: " + STR(loInfo.BoolOf("openPasswordRequired"))
* With the password, the PDF opens normally.
loReader = CreateObject('Chilkat.Pdf')
loReader.UserPassword = "user-secret"
lnSuccess = loReader.LoadFile("qa_output/memo_encrypted.pdf")
IF (lnSuccess = 0) THEN
? loReader.LastErrorText
RELEASE loPdf
RELEASE loJson
RELEASE loJsonOut
RELEASE loChecker
RELEASE loInfo
RELEASE loReader
CANCEL
ENDIF
? "pages: " + STR(loReader.NumPages)
* Sample output:
* algorithm: aes-256
* PDF version: 1.7
* password needed to open: True
* encrypted: True
* openPasswordRequired: True
* pages: 2
RELEASE loPdf
RELEASE loJson
RELEASE loJsonOut
RELEASE loChecker
RELEASE loInfo
RELEASE loReader