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

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Variant vRecipient
    Handle hoRecipient
    Handle hoPdf
    Variant vJson
    Handle hoJson
    Variant vJsonOut
    Handle hoJsonOut
    Handle hoReader
    Variant vInfo
    Handle hoInfo
    Variant vMyCert
    Handle hoMyCert
    Variant vEmpty
    Handle hoEmpty
    Variant vLoadedInfo
    Handle hoLoadedInfo
    String sTemp1
    Integer iTemp1

    Move False To iSuccess

    //  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.
    Get Create (RefClass(cComChilkatCert)) To hoRecipient
    If (Not(IsComObjectCreated(hoRecipient))) Begin
        Send CreateComObject of hoRecipient
    End
    Get ComLoadFromFile Of hoRecipient "qa_data/certs/recipient.cer" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoRecipient To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatPdf)) To hoPdf
    If (Not(IsComObjectCreated(hoPdf))) Begin
        Send CreateComObject of hoPdf
    End
    Get ComLoadFile Of hoPdf "qa_data/pdf/two_page_memo.pdf" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoPdf To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  Call AddEncryptCert once for each recipient.
    Get pvComObject of hoRecipient to vRecipient
    Get ComAddEncryptCert Of hoPdf vRecipient To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoPdf To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  recipientPermissions[0] belongs to the first certificate added.  changeSecurity gives that
    //  recipient owner rights, so the file can be managed later.
    Get Create (RefClass(cComChilkatJsonObject)) To hoJson
    If (Not(IsComObjectCreated(hoJson))) Begin
        Send CreateComObject of hoJson
    End
    Get ComUpdateString Of hoJson "method" "certificate" To iSuccess
    Get ComUpdateBool Of hoJson "recipientPermissions[0].changeSecurity" True To iSuccess

    Get Create (RefClass(cComChilkatJsonObject)) To hoJsonOut
    If (Not(IsComObjectCreated(hoJsonOut))) Begin
        Send CreateComObject of hoJsonOut
    End
    Get pvComObject of hoJson to vJson
    Get pvComObject of hoJsonOut to vJsonOut
    Get ComProtection Of hoPdf "encrypt" vJson vJsonOut To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoPdf To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComStringOf Of hoJsonOut "pending.method" To sTemp1
    Showln "method: " sTemp1
    Get ComStringOf Of hoJsonOut "pending.algorithm" To sTemp1
    Showln "algorithm: " sTemp1
    Get ComSizeOfArray Of hoJsonOut "pending.recipients" To iTemp1
    Showln "recipients: " iTemp1

    Get ComWriteFile Of hoPdf "qa_output/memo_for_recipient.pdf" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoPdf To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  PreLoadCheck says how the file is protected, without loading it.
    Get Create (RefClass(cComChilkatPdf)) To hoReader
    If (Not(IsComObjectCreated(hoReader))) Begin
        Send CreateComObject of hoReader
    End
    Get Create (RefClass(cComChilkatJsonObject)) To hoInfo
    If (Not(IsComObjectCreated(hoInfo))) Begin
        Send CreateComObject of hoInfo
    End
    Get pvComObject of hoInfo to vInfo
    Get ComPreLoadCheck Of hoReader "qa_output/memo_for_recipient.pdf" vInfo To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoReader To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComStringOf Of hoInfo "method" To sTemp1
    Showln "saved file, method: " sTemp1
    Get ComSizeOfArray Of hoInfo "recipients" To iTemp1
    Showln "saved file, recipients: " iTemp1

    //  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.)
    Get Create (RefClass(cComChilkatCert)) To hoMyCert
    If (Not(IsComObjectCreated(hoMyCert))) Begin
        Send CreateComObject of hoMyCert
    End
    Get ComLoadPfxFile Of hoMyCert "qa_data/pfx/recipient.pfx" "pfx-password" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoMyCert To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get pvComObject of hoMyCert to vMyCert
    Get ComSetDecryptCert Of hoReader vMyCert To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoReader To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComLoadFile Of hoReader "qa_output/memo_for_recipient.pdf" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoReader To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatJsonObject)) To hoEmpty
    If (Not(IsComObjectCreated(hoEmpty))) Begin
        Send CreateComObject of hoEmpty
    End
    Get Create (RefClass(cComChilkatJsonObject)) To hoLoadedInfo
    If (Not(IsComObjectCreated(hoLoadedInfo))) Begin
        Send CreateComObject of hoLoadedInfo
    End
    Get pvComObject of hoEmpty to vEmpty
    Get pvComObject of hoLoadedInfo to vLoadedInfo
    Get ComProtection Of hoReader "getInfo" vEmpty vLoadedInfo To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoReader To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComStringOf Of hoLoadedInfo "loaded.openedWith" To sTemp1
    Showln "opened with: " sTemp1
    Get ComNumPages Of hoReader To iTemp1
    Showln "pages: " iTemp1

    //  Sample output:

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


End_Procedure