Go Requires Chilkat v11.7.0+
Go
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 Go Downloads
success := false
// 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.
cert := chilkat.NewCert()
success = cert.LoadPfxFile("qa_data/pfx/myPdfSigningCert.pfx","secret")
if success == false {
fmt.Println(cert.LastErrorText())
cert.DisposeCert()
return
}
pdf := chilkat.NewPdf()
success = pdf.LoadFile("qa_data/pdf/two_page_memo.pdf")
if success == false {
fmt.Println(pdf.LastErrorText())
cert.DisposeCert()
pdf.DisposePdf()
return
}
// Encrypt with AES-256 and do not permit modifying.
pdf.SetUserPassword("user-secret")
pdf.SetOwnerPassword("owner-secret")
json := chilkat.NewJsonObject()
json.UpdateBool("permissions.modify",false)
jsonOut := chilkat.NewJsonObject()
success = pdf.Protection("encrypt",json,jsonOut)
if success == false {
fmt.Println(pdf.LastErrorText())
cert.DisposeCert()
pdf.DisposePdf()
json.DisposeJsonObject()
jsonOut.DisposeJsonObject()
return
}
// The encryption is still pending. SignPdf applies it first and signs the encrypted result.
success = pdf.SetSigningCert(cert)
if success == false {
fmt.Println(pdf.LastErrorText())
cert.DisposeCert()
pdf.DisposePdf()
json.DisposeJsonObject()
jsonOut.DisposeJsonObject()
return
}
sigOpts := chilkat.NewJsonObject()
sigOpts.UpdateBool("invisibleSignature",true)
sigOpts.UpdateInt("signingCertificateV2",1)
sigOpts.UpdateInt("signingTime",1)
success = pdf.SignPdf(sigOpts,"qa_output/memo_encrypted_signed.pdf")
if success == false {
fmt.Println(pdf.LastErrorText())
cert.DisposeCert()
pdf.DisposePdf()
json.DisposeJsonObject()
jsonOut.DisposeJsonObject()
sigOpts.DisposeJsonObject()
return
}
// The result is encrypted and signed.
check := chilkat.NewPdf()
check.SetUserPassword("user-secret")
success = check.LoadFile("qa_output/memo_encrypted_signed.pdf")
if success == false {
fmt.Println(check.LastErrorText())
cert.DisposeCert()
pdf.DisposePdf()
json.DisposeJsonObject()
jsonOut.DisposeJsonObject()
sigOpts.DisposeJsonObject()
check.DisposePdf()
return
}
empty := chilkat.NewJsonObject()
info := chilkat.NewJsonObject()
success = check.Protection("getInfo",empty,info)
if success == false {
fmt.Println(check.LastErrorText())
cert.DisposeCert()
pdf.DisposePdf()
json.DisposeJsonObject()
jsonOut.DisposeJsonObject()
sigOpts.DisposeJsonObject()
check.DisposePdf()
empty.DisposeJsonObject()
info.DisposeJsonObject()
return
}
fmt.Println("algorithm: ", *info.StringOf("loaded.algorithm"))
fmt.Println("signatures: ", check.NumSignatures())
sigInfo := chilkat.NewJsonObject()
verified := check.VerifySignature(0,sigInfo)
fmt.Println("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 == false {
fmt.Println(check.LastErrorText())
cert.DisposeCert()
pdf.DisposePdf()
json.DisposeJsonObject()
jsonOut.DisposeJsonObject()
sigOpts.DisposeJsonObject()
check.DisposePdf()
empty.DisposeJsonObject()
info.DisposeJsonObject()
sigInfo.DisposeJsonObject()
return
}
success = check.SignPdf(sigOpts,"qa_output/memo_signed_twice.pdf")
if success == false {
fmt.Println("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.
owner := chilkat.NewPdf()
owner.SetUserPassword("user-secret")
owner.SetOwnerPassword("owner-secret")
success = owner.LoadFile("qa_output/memo_encrypted_signed.pdf")
if success == false {
fmt.Println(owner.LastErrorText())
cert.DisposeCert()
pdf.DisposePdf()
json.DisposeJsonObject()
jsonOut.DisposeJsonObject()
sigOpts.DisposeJsonObject()
check.DisposePdf()
empty.DisposeJsonObject()
info.DisposeJsonObject()
sigInfo.DisposeJsonObject()
owner.DisposePdf()
return
}
success = owner.SetSigningCert(cert)
if success == false {
fmt.Println(owner.LastErrorText())
cert.DisposeCert()
pdf.DisposePdf()
json.DisposeJsonObject()
jsonOut.DisposeJsonObject()
sigOpts.DisposeJsonObject()
check.DisposePdf()
empty.DisposeJsonObject()
info.DisposeJsonObject()
sigInfo.DisposeJsonObject()
owner.DisposePdf()
return
}
success = owner.SignPdf(sigOpts,"qa_output/memo_signed_twice.pdf")
if success == false {
fmt.Println(owner.LastErrorText())
cert.DisposeCert()
pdf.DisposePdf()
json.DisposeJsonObject()
jsonOut.DisposeJsonObject()
sigOpts.DisposeJsonObject()
check.DisposePdf()
empty.DisposeJsonObject()
info.DisposeJsonObject()
sigInfo.DisposeJsonObject()
owner.DisposePdf()
return
}
twice := chilkat.NewPdf()
twice.SetUserPassword("user-secret")
success = twice.LoadFile("qa_output/memo_signed_twice.pdf")
if success == false {
fmt.Println(twice.LastErrorText())
cert.DisposeCert()
pdf.DisposePdf()
json.DisposeJsonObject()
jsonOut.DisposeJsonObject()
sigOpts.DisposeJsonObject()
check.DisposePdf()
empty.DisposeJsonObject()
info.DisposeJsonObject()
sigInfo.DisposeJsonObject()
owner.DisposePdf()
twice.DisposePdf()
return
}
fmt.Println("signatures after signing with owner rights: ", twice.NumSignatures())
verified = twice.VerifySignature(0,sigInfo)
fmt.Println("first signature still verifies: ", verified)
verified = twice.VerifySignature(1,sigInfo)
fmt.Println("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
cert.DisposeCert()
pdf.DisposePdf()
json.DisposeJsonObject()
jsonOut.DisposeJsonObject()
sigOpts.DisposeJsonObject()
check.DisposePdf()
empty.DisposeJsonObject()
info.DisposeJsonObject()
sigInfo.DisposeJsonObject()
owner.DisposePdf()
twice.DisposePdf()