Visual Basic 6.0 Requires Chilkat v11.7.0+
Visual Basic 6.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 Visual Basic 6.0 Downloads
Dim success As Long
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.
Dim cert As New ChilkatCert
success = cert.LoadPfxFile("qa_data/pfx/myPdfSigningCert.pfx","secret")
If (success = 0) Then
Debug.Print cert.LastErrorText
Exit Sub
End If
Dim pdf As New ChilkatPdf
success = pdf.LoadFile("qa_data/pdf/two_page_memo.pdf")
If (success = 0) Then
Debug.Print pdf.LastErrorText
Exit Sub
End If
' Encrypt with AES-256 and do not permit modifying.
pdf.UserPassword = "user-secret"
pdf.OwnerPassword = "owner-secret"
Dim json As New ChilkatJsonObject
success = json.UpdateBool("permissions.modify",0)
Dim jsonOut As New ChilkatJsonObject
success = pdf.Protection("encrypt",json,jsonOut)
If (success = 0) Then
Debug.Print pdf.LastErrorText
Exit Sub
End If
' The encryption is still pending. SignPdf applies it first and signs the encrypted result.
success = pdf.SetSigningCert(cert)
If (success = 0) Then
Debug.Print pdf.LastErrorText
Exit Sub
End If
Dim sigOpts As New ChilkatJsonObject
success = sigOpts.UpdateBool("invisibleSignature",1)
success = sigOpts.UpdateInt("signingCertificateV2",1)
success = sigOpts.UpdateInt("signingTime",1)
success = pdf.SignPdf(sigOpts,"qa_output/memo_encrypted_signed.pdf")
If (success = 0) Then
Debug.Print pdf.LastErrorText
Exit Sub
End If
' The result is encrypted and signed.
Dim check As New ChilkatPdf
check.UserPassword = "user-secret"
success = check.LoadFile("qa_output/memo_encrypted_signed.pdf")
If (success = 0) Then
Debug.Print check.LastErrorText
Exit Sub
End If
Dim empty As New ChilkatJsonObject
Dim info As New ChilkatJsonObject
success = check.Protection("getInfo",empty,info)
If (success = 0) Then
Debug.Print check.LastErrorText
Exit Sub
End If
Debug.Print "algorithm: " & info.StringOf("loaded.algorithm")
Debug.Print "signatures: " & check.NumSignatures
Dim sigInfo As New ChilkatJsonObject
Dim verified As Long
verified = check.VerifySignature(0,sigInfo)
Debug.Print "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.
success = check.SetSigningCert(cert)
If (success = 0) Then
Debug.Print check.LastErrorText
Exit Sub
End If
success = check.SignPdf(sigOpts,"qa_output/memo_signed_twice.pdf")
If (success = 0) Then
Debug.Print "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.
Dim owner As New ChilkatPdf
owner.UserPassword = "user-secret"
owner.OwnerPassword = "owner-secret"
success = owner.LoadFile("qa_output/memo_encrypted_signed.pdf")
If (success = 0) Then
Debug.Print owner.LastErrorText
Exit Sub
End If
success = owner.SetSigningCert(cert)
If (success = 0) Then
Debug.Print owner.LastErrorText
Exit Sub
End If
success = owner.SignPdf(sigOpts,"qa_output/memo_signed_twice.pdf")
If (success = 0) Then
Debug.Print owner.LastErrorText
Exit Sub
End If
Dim twice As New ChilkatPdf
twice.UserPassword = "user-secret"
success = twice.LoadFile("qa_output/memo_signed_twice.pdf")
If (success = 0) Then
Debug.Print twice.LastErrorText
Exit Sub
End If
Debug.Print "signatures after signing with owner rights: " & twice.NumSignatures
verified = twice.VerifySignature(0,sigInfo)
Debug.Print "first signature still verifies: " & verified
verified = twice.VerifySignature(1,sigInfo)
Debug.Print "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