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

Remove the Password and Encryption from a PDF

See more PDF Protection Examples

Demonstrates the removeEncryption operation, which saves the PDF with no encryption at all: no password, no restrictions. It needs owner rights, so the PDF is loaded with its owner password. As with every Protection operation, the change is applied when the file is written.

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()!

    //  Removing the encryption needs owner rights: load with the owner password.
    pdf.ownerPassword = "owner-secret"
    success = pdf.loadFile(filePath: "qa_data/pdf/encrypted_user_password.pdf")
    if success == false {
        print("\(pdf.lastErrorText!)")
        return
    }

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

    print("encrypted, as loaded: \(jsonOut.bool(of: "loaded.encrypted"))")
    print("algorithm: \(jsonOut.string(of: "loaded.algorithm")!)")

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

    print("will be saved encrypted: \(jsonOut.bool(of: "pending.encrypted"))")

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

    //  The result is an ordinary PDF that opens without a password.
    let reader = CkoPdf()!
    let info = CkoJsonObject()!
    success = reader.preLoadCheck(path: "qa_output/minutes_unprotected.pdf", jsonOut: info)
    if success == false {
        print("\(reader.lastErrorText!)")
        return
    }

    print("encrypted: \(info.bool(of: "encrypted"))")
    success = reader.loadFile(filePath: "qa_output/minutes_unprotected.pdf")
    if success == false {
        print("\(reader.lastErrorText!)")
        return
    }

    print("pages: \(reader.numPages.intValue)")

    //  Sample output:

    //  encrypted, as loaded: True
    //  algorithm: rc4-128
    //  will be saved encrypted: False
    //  encrypted: False
    //  pages: 1

}