Sample code for 30+ languages & platforms
PureBasic 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 PureBasic Downloads

PureBasic
IncludeFile "CkPdf.pb"
IncludeFile "CkJsonObject.pb"

Procedure ChilkatExample()

    success.i = 0

    ;  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

    pdf.i = CkPdf::ckCreate()
    If pdf.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    ;  Removing the encryption needs owner rights: load with the owner password.
    CkPdf::setCkOwnerPassword(pdf, "owner-secret")
    success = CkPdf::ckLoadFile(pdf,"qa_data/pdf/encrypted_user_password.pdf")
    If success = 0
        Debug CkPdf::ckLastErrorText(pdf)
        CkPdf::ckDispose(pdf)
        ProcedureReturn
    EndIf

    json.i = CkJsonObject::ckCreate()
    If json.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    jsonOut.i = CkJsonObject::ckCreate()
    If jsonOut.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkPdf::ckProtection(pdf,"getInfo",json,jsonOut)
    If success = 0
        Debug CkPdf::ckLastErrorText(pdf)
        CkPdf::ckDispose(pdf)
        CkJsonObject::ckDispose(json)
        CkJsonObject::ckDispose(jsonOut)
        ProcedureReturn
    EndIf

    Debug "encrypted, as loaded: " + Str(CkJsonObject::ckBoolOf(jsonOut,"loaded.encrypted"))
    Debug "algorithm: " + CkJsonObject::ckStringOf(jsonOut,"loaded.algorithm")

    success = CkPdf::ckProtection(pdf,"removeEncryption",json,jsonOut)
    If success = 0
        Debug CkPdf::ckLastErrorText(pdf)
        CkPdf::ckDispose(pdf)
        CkJsonObject::ckDispose(json)
        CkJsonObject::ckDispose(jsonOut)
        ProcedureReturn
    EndIf

    Debug "will be saved encrypted: " + Str(CkJsonObject::ckBoolOf(jsonOut,"pending.encrypted"))

    success = CkPdf::ckWriteFile(pdf,"qa_output/minutes_unprotected.pdf")
    If success = 0
        Debug CkPdf::ckLastErrorText(pdf)
        CkPdf::ckDispose(pdf)
        CkJsonObject::ckDispose(json)
        CkJsonObject::ckDispose(jsonOut)
        ProcedureReturn
    EndIf

    ;  The result is an ordinary PDF that opens without a password.
    reader.i = CkPdf::ckCreate()
    If reader.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    info.i = CkJsonObject::ckCreate()
    If info.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkPdf::ckPreLoadCheck(reader,"qa_output/minutes_unprotected.pdf",info)
    If success = 0
        Debug CkPdf::ckLastErrorText(reader)
        CkPdf::ckDispose(pdf)
        CkJsonObject::ckDispose(json)
        CkJsonObject::ckDispose(jsonOut)
        CkPdf::ckDispose(reader)
        CkJsonObject::ckDispose(info)
        ProcedureReturn
    EndIf

    Debug "encrypted: " + Str(CkJsonObject::ckBoolOf(info,"encrypted"))
    success = CkPdf::ckLoadFile(reader,"qa_output/minutes_unprotected.pdf")
    If success = 0
        Debug CkPdf::ckLastErrorText(reader)
        CkPdf::ckDispose(pdf)
        CkJsonObject::ckDispose(json)
        CkJsonObject::ckDispose(jsonOut)
        CkPdf::ckDispose(reader)
        CkJsonObject::ckDispose(info)
        ProcedureReturn
    EndIf

    Debug "pages: " + Str(CkPdf::ckNumPages(reader))

    ;  Sample output:

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


    CkPdf::ckDispose(pdf)
    CkJsonObject::ckDispose(json)
    CkJsonObject::ckDispose(jsonOut)
    CkPdf::ckDispose(reader)
    CkJsonObject::ckDispose(info)


    ProcedureReturn
EndProcedure