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

Change the Permissions of an Encrypted PDF

See more PDF Protection Examples

Demonstrates the changePermissions operation, which changes what an encrypted PDF permits and keeps its passwords, algorithm and scope. The JSON takes the same permissions member as the encrypt operation, and a permission that is not mentioned is granted. The operation needs owner rights. For an AES-256 PDF, load with both passwords, because the user password cannot be recovered from the owner password alone.

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 encrypted_user_password.pdf

    let pdf = CkoPdf()!

    //  Changing the permissions needs owner rights.  Both passwords are given here, which is what
    //  an AES-256 PDF requires for this operation.
    pdf.userPassword = "secret"
    pdf.ownerPassword = "owner-secret"
    success = pdf.loadFile(filePath: "qa_data/pdf/encrypted_user_password.pdf")
    if success == false {
        print("\(pdf.lastErrorText!)")
        return
    }

    //  No printing and no copying.  Everything not mentioned is granted.
    let json = CkoJsonObject()!
    json.updateString(jsonPath: "permissions.print", value: "none")
    json.updateBool(jsonPath: "permissions.copy", value: false)

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

    print("print: \(jsonOut.string(of: "pending.permissions.print")!)")
    print("copy: \(jsonOut.bool(of: "pending.permissions.copy"))")
    print("modify: \(jsonOut.bool(of: "pending.permissions.modify"))")
    print("password still needed to open: \(jsonOut.bool(of: "pending.userPasswordRequired"))")

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

    //  The same user password opens the result, now with the new permissions.
    let reader = CkoPdf()!
    reader.userPassword = "secret"
    success = reader.loadFile(filePath: "qa_output/minutes_no_print.pdf")
    if success == false {
        print("\(reader.lastErrorText!)")
        return
    }

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

    print("saved file, print: \(info.string(of: "loaded.permissions.print")!)")
    print("saved file, copy: \(info.bool(of: "loaded.permissions.copy"))")

    //  Sample output:

    //  print: none
    //  copy: False
    //  modify: True
    //  password still needed to open: True
    //  saved file, print: none
    //  saved file, copy: False

}