PureBasic Requires Chilkat v11.7.0+
PureBasic
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 PureBasic Downloads
IncludeFile "CkPdf.pb"
IncludeFile "CkJsonObject.pb"
Procedure ChilkatExample()
success.i = 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
pdf.i = CkPdf::ckCreate()
If pdf.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
success = CkPdf::ckLoadFile(pdf,"qa_data/pdf/two_page_memo.pdf")
If success = 0
Debug CkPdf::ckLastErrorText(pdf)
CkPdf::ckDispose(pdf)
ProcedureReturn
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.
CkPdf::setCkUserPassword(pdf, "user-secret")
CkPdf::setCkOwnerPassword(pdf, "owner-secret")
; An empty JSON asks for the defaults: AES-256, everything encrypted, every permission granted.
json.i = CkJsonObject::ckCreate()
If json.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
jsonOut.i = CkJsonObject::ckCreate()
If jsonOut.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
success = CkPdf::ckProtection(pdf,"encrypt",json,jsonOut)
If success = 0
Debug CkPdf::ckLastErrorText(pdf)
CkPdf::ckDispose(pdf)
CkJsonObject::ckDispose(json)
CkJsonObject::ckDispose(jsonOut)
ProcedureReturn
EndIf
; Nothing has been encrypted yet. "pending" describes the security the PDF will be saved with.
Debug "algorithm: " + CkJsonObject::ckStringOf(jsonOut,"pending.algorithm")
Debug "PDF version: " + CkJsonObject::ckStringOf(jsonOut,"pending.pdfVersion")
Debug "password needed to open: " + Str(CkJsonObject::ckBoolOf(jsonOut,"pending.userPasswordRequired"))
; Writing the file applies it.
success = CkPdf::ckWriteFile(pdf,"qa_output/memo_encrypted.pdf")
If success = 0
Debug CkPdf::ckLastErrorText(pdf)
CkPdf::ckDispose(pdf)
CkJsonObject::ckDispose(json)
CkJsonObject::ckDispose(jsonOut)
ProcedureReturn
EndIf
; PreLoadCheck reports a file's encryption without loading it, and needs no password.
checker.i = CkPdf::ckCreate()
If checker.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
info.i = CkJsonObject::ckCreate()
If info.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
success = CkPdf::ckPreLoadCheck(checker,"qa_output/memo_encrypted.pdf",info)
If success = 0
Debug CkPdf::ckLastErrorText(checker)
CkPdf::ckDispose(pdf)
CkJsonObject::ckDispose(json)
CkJsonObject::ckDispose(jsonOut)
CkPdf::ckDispose(checker)
CkJsonObject::ckDispose(info)
ProcedureReturn
EndIf
Debug "encrypted: " + Str(CkJsonObject::ckBoolOf(info,"encrypted"))
Debug "openPasswordRequired: " + Str(CkJsonObject::ckBoolOf(info,"openPasswordRequired"))
; With the password, the PDF opens normally.
reader.i = CkPdf::ckCreate()
If reader.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkPdf::setCkUserPassword(reader, "user-secret")
success = CkPdf::ckLoadFile(reader,"qa_output/memo_encrypted.pdf")
If success = 0
Debug CkPdf::ckLastErrorText(reader)
CkPdf::ckDispose(pdf)
CkJsonObject::ckDispose(json)
CkJsonObject::ckDispose(jsonOut)
CkPdf::ckDispose(checker)
CkJsonObject::ckDispose(info)
CkPdf::ckDispose(reader)
ProcedureReturn
EndIf
Debug "pages: " + Str(CkPdf::ckNumPages(reader))
; Sample output:
; algorithm: aes-256
; PDF version: 1.7
; password needed to open: True
; encrypted: True
; openPasswordRequired: True
; pages: 2
CkPdf::ckDispose(pdf)
CkJsonObject::ckDispose(json)
CkJsonObject::ckDispose(jsonOut)
CkPdf::ckDispose(checker)
CkJsonObject::ckDispose(info)
CkPdf::ckDispose(reader)
ProcedureReturn
EndProcedure