Swift Requires Chilkat v11.7.0+
Swift
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 Swift Downloads
func chilkatTest() {
var success: Bool = 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.
let cert = CkoCert()!
success = cert.loadPfxFile(path: "qa_data/pfx/myPdfSigningCert.pfx", password: "secret")
if success == false {
print("\(cert.lastErrorText!)")
return
}
let pdf = CkoPdf()!
success = pdf.loadFile(filePath: "qa_data/pdf/two_page_memo.pdf")
if success == false {
print("\(pdf.lastErrorText!)")
return
}
// Encrypt with AES-256 and do not permit modifying.
pdf.userPassword = "user-secret"
pdf.ownerPassword = "owner-secret"
let json = CkoJsonObject()!
json.updateBool(jsonPath: "permissions.modify", value: false)
let jsonOut = CkoJsonObject()!
success = pdf.protection(op: "encrypt", jsonIn: json, jsonOut: jsonOut)
if success == false {
print("\(pdf.lastErrorText!)")
return
}
// The encryption is still pending. SignPdf applies it first and signs the encrypted result.
success = pdf.setSigningCert(cert: cert)
if success == false {
print("\(pdf.lastErrorText!)")
return
}
let sigOpts = CkoJsonObject()!
sigOpts.updateBool(jsonPath: "invisibleSignature", value: true)
sigOpts.updateInt(jsonPath: "signingCertificateV2", value: 1)
sigOpts.updateInt(jsonPath: "signingTime", value: 1)
success = pdf.sign(jsonOptions: sigOpts, outFilePath: "qa_output/memo_encrypted_signed.pdf")
if success == false {
print("\(pdf.lastErrorText!)")
return
}
// The result is encrypted and signed.
let check = CkoPdf()!
check.userPassword = "user-secret"
success = check.loadFile(filePath: "qa_output/memo_encrypted_signed.pdf")
if success == false {
print("\(check.lastErrorText!)")
return
}
let empty = CkoJsonObject()!
let info = CkoJsonObject()!
success = check.protection(op: "getInfo", jsonIn: empty, jsonOut: info)
if success == false {
print("\(check.lastErrorText!)")
return
}
print("algorithm: \(info.string(of: "loaded.algorithm")!)")
print("signatures: \(check.numSignatures.intValue)")
let sigInfo = CkoJsonObject()!
var verified: Bool = check.verifySignature(index: 0, sigInfo: sigInfo)
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: cert)
if success == false {
print("\(check.lastErrorText!)")
return
}
success = check.sign(jsonOptions: sigOpts, outFilePath: "qa_output/memo_signed_twice.pdf")
if success == false {
print("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.
let owner = CkoPdf()!
owner.userPassword = "user-secret"
owner.ownerPassword = "owner-secret"
success = owner.loadFile(filePath: "qa_output/memo_encrypted_signed.pdf")
if success == false {
print("\(owner.lastErrorText!)")
return
}
success = owner.setSigningCert(cert: cert)
if success == false {
print("\(owner.lastErrorText!)")
return
}
success = owner.sign(jsonOptions: sigOpts, outFilePath: "qa_output/memo_signed_twice.pdf")
if success == false {
print("\(owner.lastErrorText!)")
return
}
let twice = CkoPdf()!
twice.userPassword = "user-secret"
success = twice.loadFile(filePath: "qa_output/memo_signed_twice.pdf")
if success == false {
print("\(twice.lastErrorText!)")
return
}
print("signatures after signing with owner rights: \(twice.numSignatures.intValue)")
verified = twice.verifySignature(index: 0, sigInfo: sigInfo)
print("first signature still verifies: \(verified)")
verified = twice.verifySignature(index: 1, sigInfo: sigInfo)
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
}