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

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

Tcl

load ./chilkat.dll

set 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.
set cert [new_CkCert]

set success [CkCert_LoadPfxFile $cert "qa_data/pfx/myPdfSigningCert.pfx" "secret"]
if {$success == 0} then {
    puts [CkCert_lastErrorText $cert]
    delete_CkCert $cert
    exit
}

set pdf [new_CkPdf]

set success [CkPdf_LoadFile $pdf "qa_data/pdf/two_page_memo.pdf"]
if {$success == 0} then {
    puts [CkPdf_lastErrorText $pdf]
    delete_CkCert $cert
    delete_CkPdf $pdf
    exit
}

#  Encrypt with AES-256 and do not permit modifying.
CkPdf_put_UserPassword $pdf "user-secret"
CkPdf_put_OwnerPassword $pdf "owner-secret"
set json [new_CkJsonObject]

CkJsonObject_UpdateBool $json "permissions.modify" 0
set jsonOut [new_CkJsonObject]

set success [CkPdf_Protection $pdf "encrypt" $json $jsonOut]
if {$success == 0} then {
    puts [CkPdf_lastErrorText $pdf]
    delete_CkCert $cert
    delete_CkPdf $pdf
    delete_CkJsonObject $json
    delete_CkJsonObject $jsonOut
    exit
}

#  The encryption is still pending.  SignPdf applies it first and signs the encrypted result.
set success [CkPdf_SetSigningCert $pdf $cert]
if {$success == 0} then {
    puts [CkPdf_lastErrorText $pdf]
    delete_CkCert $cert
    delete_CkPdf $pdf
    delete_CkJsonObject $json
    delete_CkJsonObject $jsonOut
    exit
}

set sigOpts [new_CkJsonObject]

CkJsonObject_UpdateBool $sigOpts "invisibleSignature" 1
CkJsonObject_UpdateInt $sigOpts "signingCertificateV2" 1
CkJsonObject_UpdateInt $sigOpts "signingTime" 1
set success [CkPdf_SignPdf $pdf $sigOpts "qa_output/memo_encrypted_signed.pdf"]
if {$success == 0} then {
    puts [CkPdf_lastErrorText $pdf]
    delete_CkCert $cert
    delete_CkPdf $pdf
    delete_CkJsonObject $json
    delete_CkJsonObject $jsonOut
    delete_CkJsonObject $sigOpts
    exit
}

#  The result is encrypted and signed.
set check [new_CkPdf]

CkPdf_put_UserPassword $check "user-secret"
set success [CkPdf_LoadFile $check "qa_output/memo_encrypted_signed.pdf"]
if {$success == 0} then {
    puts [CkPdf_lastErrorText $check]
    delete_CkCert $cert
    delete_CkPdf $pdf
    delete_CkJsonObject $json
    delete_CkJsonObject $jsonOut
    delete_CkJsonObject $sigOpts
    delete_CkPdf $check
    exit
}

set empty [new_CkJsonObject]

set info [new_CkJsonObject]

set success [CkPdf_Protection $check "getInfo" $empty $info]
if {$success == 0} then {
    puts [CkPdf_lastErrorText $check]
    delete_CkCert $cert
    delete_CkPdf $pdf
    delete_CkJsonObject $json
    delete_CkJsonObject $jsonOut
    delete_CkJsonObject $sigOpts
    delete_CkPdf $check
    delete_CkJsonObject $empty
    delete_CkJsonObject $info
    exit
}

puts "algorithm: [CkJsonObject_stringOf $info loaded.algorithm]"
puts "signatures: [CkPdf_get_NumSignatures $check]"
set sigInfo [new_CkJsonObject]

set verified [CkPdf_VerifySignature $check 0 $sigInfo]
puts "signature verifies: $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.
set success [CkPdf_SetSigningCert $check $cert]
if {$success == 0} then {
    puts [CkPdf_lastErrorText $check]
    delete_CkCert $cert
    delete_CkPdf $pdf
    delete_CkJsonObject $json
    delete_CkJsonObject $jsonOut
    delete_CkJsonObject $sigOpts
    delete_CkPdf $check
    delete_CkJsonObject $empty
    delete_CkJsonObject $info
    delete_CkJsonObject $sigInfo
    exit
}

set success [CkPdf_SignPdf $check $sigOpts "qa_output/memo_signed_twice.pdf"]
if {$success == 0} then {
    puts "A second signature was refused: the permissions do not allow it."
}

#  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.
set owner [new_CkPdf]

CkPdf_put_UserPassword $owner "user-secret"
CkPdf_put_OwnerPassword $owner "owner-secret"
set success [CkPdf_LoadFile $owner "qa_output/memo_encrypted_signed.pdf"]
if {$success == 0} then {
    puts [CkPdf_lastErrorText $owner]
    delete_CkCert $cert
    delete_CkPdf $pdf
    delete_CkJsonObject $json
    delete_CkJsonObject $jsonOut
    delete_CkJsonObject $sigOpts
    delete_CkPdf $check
    delete_CkJsonObject $empty
    delete_CkJsonObject $info
    delete_CkJsonObject $sigInfo
    delete_CkPdf $owner
    exit
}

set success [CkPdf_SetSigningCert $owner $cert]
if {$success == 0} then {
    puts [CkPdf_lastErrorText $owner]
    delete_CkCert $cert
    delete_CkPdf $pdf
    delete_CkJsonObject $json
    delete_CkJsonObject $jsonOut
    delete_CkJsonObject $sigOpts
    delete_CkPdf $check
    delete_CkJsonObject $empty
    delete_CkJsonObject $info
    delete_CkJsonObject $sigInfo
    delete_CkPdf $owner
    exit
}

set success [CkPdf_SignPdf $owner $sigOpts "qa_output/memo_signed_twice.pdf"]
if {$success == 0} then {
    puts [CkPdf_lastErrorText $owner]
    delete_CkCert $cert
    delete_CkPdf $pdf
    delete_CkJsonObject $json
    delete_CkJsonObject $jsonOut
    delete_CkJsonObject $sigOpts
    delete_CkPdf $check
    delete_CkJsonObject $empty
    delete_CkJsonObject $info
    delete_CkJsonObject $sigInfo
    delete_CkPdf $owner
    exit
}

set twice [new_CkPdf]

CkPdf_put_UserPassword $twice "user-secret"
set success [CkPdf_LoadFile $twice "qa_output/memo_signed_twice.pdf"]
if {$success == 0} then {
    puts [CkPdf_lastErrorText $twice]
    delete_CkCert $cert
    delete_CkPdf $pdf
    delete_CkJsonObject $json
    delete_CkJsonObject $jsonOut
    delete_CkJsonObject $sigOpts
    delete_CkPdf $check
    delete_CkJsonObject $empty
    delete_CkJsonObject $info
    delete_CkJsonObject $sigInfo
    delete_CkPdf $owner
    delete_CkPdf $twice
    exit
}

puts "signatures after signing with owner rights: [CkPdf_get_NumSignatures $twice]"
set verified [CkPdf_VerifySignature $twice 0 $sigInfo]
puts "first signature still verifies: $verified"
set verified [CkPdf_VerifySignature $twice 1 $sigInfo]
puts "second signature verifies: $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

delete_CkCert $cert
delete_CkPdf $pdf
delete_CkJsonObject $json
delete_CkJsonObject $jsonOut
delete_CkJsonObject $sigOpts
delete_CkPdf $check
delete_CkJsonObject $empty
delete_CkJsonObject $info
delete_CkJsonObject $sigInfo
delete_CkPdf $owner
delete_CkPdf $twice