Sample code for 30+ languages & platforms
PowerBuilder Requires Chilkat v11.7.0+

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 PowerBuilder Downloads

PowerBuilder
integer li_rc
long li_Success
oleobject loo_Recipient
oleobject loo_Pdf
oleobject loo_Json
oleobject loo_JsonOut
oleobject loo_Reader
oleobject loo_Info
oleobject loo_MyCert
oleobject loo_Empty
oleobject loo_LoadedInfo

li_Success = 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.
loo_Recipient = create oleobject
li_rc = loo_Recipient.ConnectToNewObject("Chilkat.Cert")
if li_rc < 0 then
    destroy loo_Recipient
    MessageBox("Error","Connecting to COM object failed")
    return
end if
li_Success = loo_Recipient.LoadFromFile("qa_data/certs/recipient.cer")
if li_Success = 0 then
    Write-Debug loo_Recipient.LastErrorText
    destroy loo_Recipient
    return
end if

loo_Pdf = create oleobject
li_rc = loo_Pdf.ConnectToNewObject("Chilkat.Pdf")

li_Success = loo_Pdf.LoadFile("qa_data/pdf/two_page_memo.pdf")
if li_Success = 0 then
    Write-Debug loo_Pdf.LastErrorText
    destroy loo_Recipient
    destroy loo_Pdf
    return
end if

//  Call AddEncryptCert once for each recipient.
li_Success = loo_Pdf.AddEncryptCert(loo_Recipient)
if li_Success = 0 then
    Write-Debug loo_Pdf.LastErrorText
    destroy loo_Recipient
    destroy loo_Pdf
    return
end if

//  recipientPermissions[0] belongs to the first certificate added.  changeSecurity gives that
//  recipient owner rights, so the file can be managed later.
loo_Json = create oleobject
li_rc = loo_Json.ConnectToNewObject("Chilkat.JsonObject")

loo_Json.UpdateString("method","certificate")
loo_Json.UpdateBool("recipientPermissions[0].changeSecurity",1)

loo_JsonOut = create oleobject
li_rc = loo_JsonOut.ConnectToNewObject("Chilkat.JsonObject")

li_Success = loo_Pdf.Protection("encrypt",loo_Json,loo_JsonOut)
if li_Success = 0 then
    Write-Debug loo_Pdf.LastErrorText
    destroy loo_Recipient
    destroy loo_Pdf
    destroy loo_Json
    destroy loo_JsonOut
    return
end if

Write-Debug "method: " + loo_JsonOut.StringOf("pending.method")
Write-Debug "algorithm: " + loo_JsonOut.StringOf("pending.algorithm")
Write-Debug "recipients: " + string(loo_JsonOut.SizeOfArray("pending.recipients"))

li_Success = loo_Pdf.WriteFile("qa_output/memo_for_recipient.pdf")
if li_Success = 0 then
    Write-Debug loo_Pdf.LastErrorText
    destroy loo_Recipient
    destroy loo_Pdf
    destroy loo_Json
    destroy loo_JsonOut
    return
end if

//  PreLoadCheck says how the file is protected, without loading it.
loo_Reader = create oleobject
li_rc = loo_Reader.ConnectToNewObject("Chilkat.Pdf")

loo_Info = create oleobject
li_rc = loo_Info.ConnectToNewObject("Chilkat.JsonObject")

li_Success = loo_Reader.PreLoadCheck("qa_output/memo_for_recipient.pdf",loo_Info)
if li_Success = 0 then
    Write-Debug loo_Reader.LastErrorText
    destroy loo_Recipient
    destroy loo_Pdf
    destroy loo_Json
    destroy loo_JsonOut
    destroy loo_Reader
    destroy loo_Info
    return
end if

Write-Debug "saved file, method: " + loo_Info.StringOf("method")
Write-Debug "saved file, recipients: " + string(loo_Info.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.)
loo_MyCert = create oleobject
li_rc = loo_MyCert.ConnectToNewObject("Chilkat.Cert")

li_Success = loo_MyCert.LoadPfxFile("qa_data/pfx/recipient.pfx","pfx-password")
if li_Success = 0 then
    Write-Debug loo_MyCert.LastErrorText
    destroy loo_Recipient
    destroy loo_Pdf
    destroy loo_Json
    destroy loo_JsonOut
    destroy loo_Reader
    destroy loo_Info
    destroy loo_MyCert
    return
end if

li_Success = loo_Reader.SetDecryptCert(loo_MyCert)
if li_Success = 0 then
    Write-Debug loo_Reader.LastErrorText
    destroy loo_Recipient
    destroy loo_Pdf
    destroy loo_Json
    destroy loo_JsonOut
    destroy loo_Reader
    destroy loo_Info
    destroy loo_MyCert
    return
end if

li_Success = loo_Reader.LoadFile("qa_output/memo_for_recipient.pdf")
if li_Success = 0 then
    Write-Debug loo_Reader.LastErrorText
    destroy loo_Recipient
    destroy loo_Pdf
    destroy loo_Json
    destroy loo_JsonOut
    destroy loo_Reader
    destroy loo_Info
    destroy loo_MyCert
    return
end if

loo_Empty = create oleobject
li_rc = loo_Empty.ConnectToNewObject("Chilkat.JsonObject")

loo_LoadedInfo = create oleobject
li_rc = loo_LoadedInfo.ConnectToNewObject("Chilkat.JsonObject")

li_Success = loo_Reader.Protection("getInfo",loo_Empty,loo_LoadedInfo)
if li_Success = 0 then
    Write-Debug loo_Reader.LastErrorText
    destroy loo_Recipient
    destroy loo_Pdf
    destroy loo_Json
    destroy loo_JsonOut
    destroy loo_Reader
    destroy loo_Info
    destroy loo_MyCert
    destroy loo_Empty
    destroy loo_LoadedInfo
    return
end if

Write-Debug "opened with: " + loo_LoadedInfo.StringOf("loaded.openedWith")
Write-Debug "pages: " + string(loo_Reader.NumPages)

//  Sample output:

//  method: certificate
//  algorithm: aes-256
//  recipients: 1
//  saved file, method: certificate
//  saved file, recipients: 1
//  opened with: certificate
//  pages: 2


destroy loo_Recipient
destroy loo_Pdf
destroy loo_Json
destroy loo_JsonOut
destroy loo_Reader
destroy loo_Info
destroy loo_MyCert
destroy loo_Empty
destroy loo_LoadedInfo