PowerBuilder Requires Chilkat v11.7.0+
PowerBuilder
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 PowerBuilder Downloads
integer li_rc
long li_Success
oleobject loo_Cert
oleobject loo_Pdf
oleobject loo_Json
oleobject loo_JsonOut
oleobject loo_SigOpts
oleobject loo_Check
oleobject loo_Empty
oleobject loo_Info
oleobject loo_SigInfo
long li_Verified
oleobject loo_Owner
oleobject loo_Twice
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 signing certificate, with its private key.
loo_Cert = create oleobject
li_rc = loo_Cert.ConnectToNewObject("Chilkat.Cert")
if li_rc < 0 then
destroy loo_Cert
MessageBox("Error","Connecting to COM object failed")
return
end if
li_Success = loo_Cert.LoadPfxFile("qa_data/pfx/myPdfSigningCert.pfx","secret")
if li_Success = 0 then
Write-Debug loo_Cert.LastErrorText
destroy loo_Cert
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_Cert
destroy loo_Pdf
return
end if
// Encrypt with AES-256 and do not permit modifying.
loo_Pdf.UserPassword = "user-secret"
loo_Pdf.OwnerPassword = "owner-secret"
loo_Json = create oleobject
li_rc = loo_Json.ConnectToNewObject("Chilkat.JsonObject")
loo_Json.UpdateBool("permissions.modify",0)
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_Cert
destroy loo_Pdf
destroy loo_Json
destroy loo_JsonOut
return
end if
// The encryption is still pending. SignPdf applies it first and signs the encrypted result.
li_Success = loo_Pdf.SetSigningCert(loo_Cert)
if li_Success = 0 then
Write-Debug loo_Pdf.LastErrorText
destroy loo_Cert
destroy loo_Pdf
destroy loo_Json
destroy loo_JsonOut
return
end if
loo_SigOpts = create oleobject
li_rc = loo_SigOpts.ConnectToNewObject("Chilkat.JsonObject")
loo_SigOpts.UpdateBool("invisibleSignature",1)
loo_SigOpts.UpdateInt("signingCertificateV2",1)
loo_SigOpts.UpdateInt("signingTime",1)
li_Success = loo_Pdf.SignPdf(loo_SigOpts,"qa_output/memo_encrypted_signed.pdf")
if li_Success = 0 then
Write-Debug loo_Pdf.LastErrorText
destroy loo_Cert
destroy loo_Pdf
destroy loo_Json
destroy loo_JsonOut
destroy loo_SigOpts
return
end if
// The result is encrypted and signed.
loo_Check = create oleobject
li_rc = loo_Check.ConnectToNewObject("Chilkat.Pdf")
loo_Check.UserPassword = "user-secret"
li_Success = loo_Check.LoadFile("qa_output/memo_encrypted_signed.pdf")
if li_Success = 0 then
Write-Debug loo_Check.LastErrorText
destroy loo_Cert
destroy loo_Pdf
destroy loo_Json
destroy loo_JsonOut
destroy loo_SigOpts
destroy loo_Check
return
end if
loo_Empty = create oleobject
li_rc = loo_Empty.ConnectToNewObject("Chilkat.JsonObject")
loo_Info = create oleobject
li_rc = loo_Info.ConnectToNewObject("Chilkat.JsonObject")
li_Success = loo_Check.Protection("getInfo",loo_Empty,loo_Info)
if li_Success = 0 then
Write-Debug loo_Check.LastErrorText
destroy loo_Cert
destroy loo_Pdf
destroy loo_Json
destroy loo_JsonOut
destroy loo_SigOpts
destroy loo_Check
destroy loo_Empty
destroy loo_Info
return
end if
Write-Debug "algorithm: " + loo_Info.StringOf("loaded.algorithm")
Write-Debug "signatures: " + string(loo_Check.NumSignatures)
loo_SigInfo = create oleobject
li_rc = loo_SigInfo.ConnectToNewObject("Chilkat.JsonObject")
li_Verified = loo_Check.VerifySignature(0,loo_SigInfo)
Write-Debug "signature verifies: " + string(li_Verified)
// 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.
li_Success = loo_Check.SetSigningCert(loo_Cert)
if li_Success = 0 then
Write-Debug loo_Check.LastErrorText
destroy loo_Cert
destroy loo_Pdf
destroy loo_Json
destroy loo_JsonOut
destroy loo_SigOpts
destroy loo_Check
destroy loo_Empty
destroy loo_Info
destroy loo_SigInfo
return
end if
li_Success = loo_Check.SignPdf(loo_SigOpts,"qa_output/memo_signed_twice.pdf")
if li_Success = 0 then
Write-Debug "A second signature was refused: the permissions do not allow it."
end if
// 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.
loo_Owner = create oleobject
li_rc = loo_Owner.ConnectToNewObject("Chilkat.Pdf")
loo_Owner.UserPassword = "user-secret"
loo_Owner.OwnerPassword = "owner-secret"
li_Success = loo_Owner.LoadFile("qa_output/memo_encrypted_signed.pdf")
if li_Success = 0 then
Write-Debug loo_Owner.LastErrorText
destroy loo_Cert
destroy loo_Pdf
destroy loo_Json
destroy loo_JsonOut
destroy loo_SigOpts
destroy loo_Check
destroy loo_Empty
destroy loo_Info
destroy loo_SigInfo
destroy loo_Owner
return
end if
li_Success = loo_Owner.SetSigningCert(loo_Cert)
if li_Success = 0 then
Write-Debug loo_Owner.LastErrorText
destroy loo_Cert
destroy loo_Pdf
destroy loo_Json
destroy loo_JsonOut
destroy loo_SigOpts
destroy loo_Check
destroy loo_Empty
destroy loo_Info
destroy loo_SigInfo
destroy loo_Owner
return
end if
li_Success = loo_Owner.SignPdf(loo_SigOpts,"qa_output/memo_signed_twice.pdf")
if li_Success = 0 then
Write-Debug loo_Owner.LastErrorText
destroy loo_Cert
destroy loo_Pdf
destroy loo_Json
destroy loo_JsonOut
destroy loo_SigOpts
destroy loo_Check
destroy loo_Empty
destroy loo_Info
destroy loo_SigInfo
destroy loo_Owner
return
end if
loo_Twice = create oleobject
li_rc = loo_Twice.ConnectToNewObject("Chilkat.Pdf")
loo_Twice.UserPassword = "user-secret"
li_Success = loo_Twice.LoadFile("qa_output/memo_signed_twice.pdf")
if li_Success = 0 then
Write-Debug loo_Twice.LastErrorText
destroy loo_Cert
destroy loo_Pdf
destroy loo_Json
destroy loo_JsonOut
destroy loo_SigOpts
destroy loo_Check
destroy loo_Empty
destroy loo_Info
destroy loo_SigInfo
destroy loo_Owner
destroy loo_Twice
return
end if
Write-Debug "signatures after signing with owner rights: " + string(loo_Twice.NumSignatures)
li_Verified = loo_Twice.VerifySignature(0,loo_SigInfo)
Write-Debug "first signature still verifies: " + string(li_Verified)
li_Verified = loo_Twice.VerifySignature(1,loo_SigInfo)
Write-Debug "second signature verifies: " + string(li_Verified)
// 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
destroy loo_Cert
destroy loo_Pdf
destroy loo_Json
destroy loo_JsonOut
destroy loo_SigOpts
destroy loo_Check
destroy loo_Empty
destroy loo_Info
destroy loo_SigInfo
destroy loo_Owner
destroy loo_Twice