Sample code for 30+ languages & platforms
PowerBuilder

Sign PDF in the Cloud using Azure Key Vault

See more Signing in the Cloud Examples

Demonstrates how to sign a PDF using Azure Key Vault. The signing of the hash happens in the Cloud on Azure Key Vault. Everything else involving the updating the PDF to add the signature happens locally within Chilkat.

Note: This example requires Chilkat v9.5.0.96 or greater.

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Pdf
oleobject loo_Json
oleobject loo_Cert
oleobject loo_JsonAzure

li_Success = 0

// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.

loo_Pdf = create oleobject
li_rc = loo_Pdf.ConnectToNewObject("Chilkat.Pdf")
if li_rc < 0 then
    destroy loo_Pdf
    MessageBox("Error","Connecting to COM object failed")
    return
end if

// Load a PDF to be signed.
li_Success = loo_Pdf.LoadFile("qa_data/pdf/helloWorld.pdf")
if li_Success = 0 then
    Write-Debug loo_Pdf.LastErrorText
    destroy loo_Pdf
    return
end if

// Options for signing are specified in JSON.
loo_Json = create oleobject
li_rc = loo_Json.ConnectToNewObject("Chilkat.JsonObject")

// Optionally create an LTV-enabled signature
loo_Json.UpdateBool("ltvOcsp",1)

// Define the appearance of the signature.
loo_Json.UpdateInt("page",1)
loo_Json.UpdateString("appearance.y","top")
loo_Json.UpdateString("appearance.x","left")
loo_Json.UpdateString("appearance.fontScale","10.0")
loo_Json.UpdateString("appearance.text[0]","Digitally signed by: cert_cn")
loo_Json.UpdateString("appearance.text[1]","current_dt")
loo_Json.UpdateString("appearance.text[2]","This is an LTV-enabled signature.")

// Load the certificate used for signing.  The certificate's private key is stored in 
// the Azure Key Vault.
// However, we still need the certificate locally (without private key).
loo_Cert = create oleobject
li_rc = loo_Cert.ConnectToNewObject("Chilkat.Cert")

li_Success = loo_Cert.LoadFromFile("qa_data/certs/myCert.cer")
if li_Success = 0 then
    Write-Debug loo_Cert.LastErrorText
    destroy loo_Pdf
    destroy loo_Json
    destroy loo_Cert
    return
end if

// Here's a screenshot of our certificate with private key on Azure Key Vault:
// (image:https://example-code.com/images/azure_key_vault.jpg/endImage)

// To sign using the Azure Key Vault, 
// add the following lines of code to specify your authentication credentials,
// and the name of the certificate w/ private key on the Azure server to be used.
loo_JsonAzure = create oleobject
li_rc = loo_JsonAzure.ConnectToNewObject("Chilkat.JsonObject")

// Set the "service" equal to "azure_keyvault" to tell Chilkat to use Azure Key Vault for signing.
loo_JsonAzure.UpdateString("service","azure_keyvault")
loo_JsonAzure.UpdateString("client_id","APP_ID")
loo_JsonAzure.UpdateString("client_secret","APP_PASSWORD")
loo_JsonAzure.UpdateString("tenant_id","TENANT_ID")
// In the above screenshot, our vault name is "kvchilkat".  You will use your vault name.
loo_JsonAzure.UpdateString("vault_name","VAULT_NAME")
// In the above screenshot, our cert name is "ChilkatTest1".  You will use your cert name.
loo_JsonAzure.UpdateString("cert_name","CERT_NAME")
// In the above screenshot, our cert version is "63b94a23389546ecbc8eb6208a1bef37".  You will use your cert version.
loo_JsonAzure.UpdateString("cert_version","CERT_VERSION")
li_Success = loo_Cert.SetCloudSigner(loo_JsonAzure)

// Tell the pdf object to use the certificate for signing (and for Azure Key Vault to do the actual signing of the hash).
li_Success = loo_Pdf.SetSigningCert(loo_Cert)
if li_Success = 0 then
    Write-Debug loo_Pdf.LastErrorText
    destroy loo_Pdf
    destroy loo_Json
    destroy loo_Cert
    destroy loo_JsonAzure
    return
end if

// Chilkat will build the signed PDF locally, but will (internally) use Azure Key Vault
// to do the RSA (or EC) sign hash operation.
li_Success = loo_Pdf.SignPdf(loo_Json,"qa_output/hello_ltv_signed.pdf")
if li_Success = 0 then
    Write-Debug loo_Pdf.LastErrorText
    destroy loo_Pdf
    destroy loo_Json
    destroy loo_Cert
    destroy loo_JsonAzure
    return
end if

Write-Debug "PDF Cloud Signing using Azure Key Vault Successful."


destroy loo_Pdf
destroy loo_Json
destroy loo_Cert
destroy loo_JsonAzure