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

Encrypt a PDF for a Certificate Instead of a Password

See more PDF Protection Examples

Demonstrates certificate security, where a PDF is encrypted for one or more recipients and each opens it with their own certificate and private key. No password is involved. Only the public certificate of a recipient is needed to encrypt. Each recipient has its own permissions, and the changeSecurity permission gives a recipient owner rights. PreLoadCheck tells an application that a file is certificate-encrypted and which certificates can open it. A PDF that cannot be opened, because no matching certificate and private key were provided, fails to load.

Chilkat Swift Downloads

Swift

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 recipient's certificate.  Only the public certificate is needed to encrypt.
    let recipient = CkoCert()!
    success = recipient.load(fromFile: "qa_data/certs/recipient.cer")
    if success == false {
        print("\(recipient.lastErrorText!)")
        return
    }

    let pdf = CkoPdf()!
    success = pdf.loadFile(filePath: "qa_data/pdf/two_page_memo.pdf")
    if success == false {
        print("\(pdf.lastErrorText!)")
        return
    }

    //  Call AddEncryptCert once for each recipient.
    success = pdf.addEncryptCert(cert: recipient)
    if success == false {
        print("\(pdf.lastErrorText!)")
        return
    }

    //  recipientPermissions[0] belongs to the first certificate added.  changeSecurity gives that
    //  recipient owner rights, so the file can be managed later.
    let json = CkoJsonObject()!
    json.updateString(jsonPath: "method", value: "certificate")
    json.updateBool(jsonPath: "recipientPermissions[0].changeSecurity", value: true)

    let jsonOut = CkoJsonObject()!
    success = pdf.protection(op: "encrypt", jsonIn: json, jsonOut: jsonOut)
    if success == false {
        print("\(pdf.lastErrorText!)")
        return
    }

    print("method: \(jsonOut.string(of: "pending.method")!)")
    print("algorithm: \(jsonOut.string(of: "pending.algorithm")!)")
    print("recipients: \(jsonOut.size(ofArray: "pending.recipients").intValue)")

    success = pdf.writeFile(path: "qa_output/memo_for_recipient.pdf")
    if success == false {
        print("\(pdf.lastErrorText!)")
        return
    }

    //  PreLoadCheck says how the file is protected, without loading it.
    let reader = CkoPdf()!
    let info = CkoJsonObject()!
    success = reader.preLoadCheck(path: "qa_output/memo_for_recipient.pdf", jsonOut: info)
    if success == false {
        print("\(reader.lastErrorText!)")
        return
    }

    print("saved file, method: \(info.string(of: "method")!)")
    print("saved file, recipients: \(info.size(ofArray: "recipients").intValue)")

    //  The recipient opens it with the certificate and its private key, here from a .pfx file.
    //  (On Windows, a certificate that is installed in the Windows certificate store with its private
    //  key is found automatically when the PDF is loaded, and SetDecryptCert is not needed for it.)
    let myCert = CkoCert()!
    success = myCert.loadPfxFile(path: "qa_data/pfx/recipient.pfx", password: "pfx-password")
    if success == false {
        print("\(myCert.lastErrorText!)")
        return
    }

    success = reader.setDecryptCert(cert: myCert)
    if success == false {
        print("\(reader.lastErrorText!)")
        return
    }

    success = reader.loadFile(filePath: "qa_output/memo_for_recipient.pdf")
    if success == false {
        print("\(reader.lastErrorText!)")
        return
    }

    let empty = CkoJsonObject()!
    let loadedInfo = CkoJsonObject()!
    success = reader.protection(op: "getInfo", jsonIn: empty, jsonOut: loadedInfo)
    if success == false {
        print("\(reader.lastErrorText!)")
        return
    }

    print("opened with: \(loadedInfo.string(of: "loaded.openedWith")!)")
    print("pages: \(reader.numPages.intValue)")

    //  Sample output:

    //  method: certificate
    //  algorithm: aes-256
    //  recipients: 1
    //  saved file, method: certificate
    //  saved file, recipients: 1
    //  opened with: certificate
    //  pages: 2

}