Xbase++ Requires Chilkat v11.7.0+
Xbase++
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 Xbase++ Downloads
LOCAL nSuccess
LOCAL oPdf
LOCAL oJson
LOCAL oJsonOut
LOCAL oChecker
LOCAL oInfo
LOCAL oReader
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
oPdf := CreateObject("Chilkat.Pdf")
nSuccess := oPdf:LoadFile("qa_data/pdf/two_page_memo.pdf")
IF (nSuccess == 0)
? oPdf:LastErrorText
oPdf:destroy()
RETURN
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.
oPdf:UserPassword := "user-secret"
oPdf:OwnerPassword := "owner-secret"
// An empty JSON asks for the defaults: AES-256, everything encrypted, every permission granted.
oJson := CreateObject("Chilkat.JsonObject")
oJsonOut := CreateObject("Chilkat.JsonObject")
nSuccess := oPdf:Protection("encrypt", oJson, oJsonOut)
IF (nSuccess == 0)
? oPdf:LastErrorText
oPdf:destroy()
oJson:destroy()
oJsonOut:destroy()
RETURN
ENDIF
// Nothing has been encrypted yet. "pending" describes the security the PDF will be saved with.
? "algorithm: " + oJsonOut:StringOf("pending.algorithm")
? "PDF version: " + oJsonOut:StringOf("pending.pdfVersion")
? "password needed to open: " + Str(oJsonOut:BoolOf("pending.userPasswordRequired"))
// Writing the file applies it.
nSuccess := oPdf:WriteFile("qa_output/memo_encrypted.pdf")
IF (nSuccess == 0)
? oPdf:LastErrorText
oPdf:destroy()
oJson:destroy()
oJsonOut:destroy()
RETURN
ENDIF
// PreLoadCheck reports a file's encryption without loading it, and needs no password.
oChecker := CreateObject("Chilkat.Pdf")
oInfo := CreateObject("Chilkat.JsonObject")
nSuccess := oChecker:PreLoadCheck("qa_output/memo_encrypted.pdf", oInfo)
IF (nSuccess == 0)
? oChecker:LastErrorText
oPdf:destroy()
oJson:destroy()
oJsonOut:destroy()
oChecker:destroy()
oInfo:destroy()
RETURN
ENDIF
? "encrypted: " + Str(oInfo:BoolOf("encrypted"))
? "openPasswordRequired: " + Str(oInfo:BoolOf("openPasswordRequired"))
// With the password, the PDF opens normally.
oReader := CreateObject("Chilkat.Pdf")
oReader:UserPassword := "user-secret"
nSuccess := oReader:LoadFile("qa_output/memo_encrypted.pdf")
IF (nSuccess == 0)
? oReader:LastErrorText
oPdf:destroy()
oJson:destroy()
oJsonOut:destroy()
oChecker:destroy()
oInfo:destroy()
oReader:destroy()
RETURN
ENDIF
? "pages: " + Str(oReader:NumPages)
// Sample output:
// algorithm: aes-256
// PDF version: 1.7
// password needed to open: True
// encrypted: True
// openPasswordRequired: True
// pages: 2
oPdf:destroy()
oJson:destroy()
oJsonOut:destroy()
oChecker:destroy()
oInfo:destroy()
oReader:destroy()