Sample code for 30+ languages & platforms
AutoIt

Sign PDF in the Cloud using AWS KMS

See more Signing in the Cloud Examples

Demonstrates how to sign a PDF using the AWS Key Management Service. The signing of the hash happens in the Cloud on AWS KMS. 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 AutoIt Downloads

AutoIt
Local $bSuccess = False

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

$oPdf = ObjCreate("Chilkat.Pdf")

; Load a PDF to be signed.
$bSuccess = $oPdf.LoadFile("qa_data/pdf/helloWorld.pdf")
If ($bSuccess = False) Then
    ConsoleWrite($oPdf.LastErrorText & @CRLF)
    Exit
EndIf

; Options for signing are specified in JSON.
$oJson = ObjCreate("Chilkat.JsonObject")

; Optionally create an LTV-enabled signature
$oJson.UpdateBool("ltvOcsp",True)

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

; Load the certificate used for signing.  The certificate's private key is stored in AWS KMS
; However, we still need the certificate locally (without private key).
$oCert = ObjCreate("Chilkat.Cert")
$bSuccess = $oCert.LoadFromFile("qa_data/certs/myCert.cer")
If ($bSuccess = False) Then
    ConsoleWrite($oCert.LastErrorText & @CRLF)
    Exit
EndIf

; Here's a screenshot of our private key AWS KMS:
; (image:https://example-code.com/images/aws_kms.jpg/endImage)

; To sign using AWS KMS, 
; add the following lines of code to specify your AWS authentication credentials,
; and the ID of the KMS private key.
$oJsonAwsKms = ObjCreate("Chilkat.JsonObject")
; Set the "service" equal to "aws_kms" to tell Chilkat to use AWS KMS for signing.
$oJsonAwsKms.UpdateString("service","aws_kms")
$oJsonAwsKms.UpdateString("access_key","ACCESS_KEY")
$oJsonAwsKms.UpdateString("secret_key","SECRET_KEY")
; Make sure to specify the correct region for your case.
$oJsonAwsKms.UpdateString("region","us-west-2")
; In the above screenshot, our key ID is "187012e8-008f-4fc7-b100-5efe6146dff2".  You will use your key ID.
$oJsonAwsKms.UpdateString("key_id","187012e8-008f-4fc7-b100-5efe6146dff2")

$bSuccess = $oCert.SetCloudSigner($oJsonAwsKms)

; Tell the pdf object to use the certificate for signing (and for AWS KMS to do the actual signing of the hash).
$bSuccess = $oPdf.SetSigningCert($oCert)
If ($bSuccess = False) Then
    ConsoleWrite($oPdf.LastErrorText & @CRLF)
    Exit
EndIf

; Chilkat will build the signed PDF locally, but will (internally) use AWS KMS
; to do the RSA (or EC) sign hash operation.
$bSuccess = $oPdf.SignPdf($oJson,"qa_output/hello_ltv_signed.pdf")
If ($bSuccess = False) Then
    ConsoleWrite($oPdf.LastErrorText & @CRLF)
    Exit
EndIf

ConsoleWrite("PDF Cloud Signing using AWS KMS Successful." & @CRLF)