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

Encrypt and Sign a PDF, and Sign an Encrypted PDF

See more PDF Protection Examples

Demonstrates signing together with encryption. A pending encrypt is applied by SignPdf before it signs, so one call produces a PDF that is both encrypted and signed. A PDF that is already encrypted can be signed too, and stays encrypted. When it was opened with only the user password its permissions decide: adding a signature field needs both the modify and the annotate permission. Loading the PDF with its owner password lifts that.

Chilkat Unicode C++ Downloads

Unicode C++
#include <CkCertW.h>
#include <CkPdfW.h>
#include <CkJsonObjectW.h>

void ChilkatSample(void)
    {
    bool success = 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 signing certificate, with its private key.
    CkCertW cert;
    success = cert.LoadPfxFile(L"qa_data/pfx/myPdfSigningCert.pfx",L"secret");
    if (success == false) {
        wprintf(L"%s\n",cert.lastErrorText());
        return;
    }

    CkPdfW pdf;
    success = pdf.LoadFile(L"qa_data/pdf/two_page_memo.pdf");
    if (success == false) {
        wprintf(L"%s\n",pdf.lastErrorText());
        return;
    }

    //  Encrypt with AES-256 and do not permit modifying.
    pdf.put_UserPassword(L"user-secret");
    pdf.put_OwnerPassword(L"owner-secret");
    CkJsonObjectW json;
    json.UpdateBool(L"permissions.modify",false);
    CkJsonObjectW jsonOut;
    success = pdf.Protection(L"encrypt",json,jsonOut);
    if (success == false) {
        wprintf(L"%s\n",pdf.lastErrorText());
        return;
    }

    //  The encryption is still pending.  SignPdf applies it first and signs the encrypted result.
    success = pdf.SetSigningCert(cert);
    if (success == false) {
        wprintf(L"%s\n",pdf.lastErrorText());
        return;
    }

    CkJsonObjectW sigOpts;
    sigOpts.UpdateBool(L"invisibleSignature",true);
    sigOpts.UpdateInt(L"signingCertificateV2",1);
    sigOpts.UpdateInt(L"signingTime",1);
    success = pdf.SignPdf(sigOpts,L"qa_output/memo_encrypted_signed.pdf");
    if (success == false) {
        wprintf(L"%s\n",pdf.lastErrorText());
        return;
    }

    //  The result is encrypted and signed.
    CkPdfW check;
    check.put_UserPassword(L"user-secret");
    success = check.LoadFile(L"qa_output/memo_encrypted_signed.pdf");
    if (success == false) {
        wprintf(L"%s\n",check.lastErrorText());
        return;
    }

    CkJsonObjectW empty;
    CkJsonObjectW info;
    success = check.Protection(L"getInfo",empty,info);
    if (success == false) {
        wprintf(L"%s\n",check.lastErrorText());
        return;
    }

    wprintf(L"algorithm: %s\n",info.stringOf(L"loaded.algorithm"));
    wprintf(L"signatures: %d\n",check.get_NumSignatures());
    CkJsonObjectW sigInfo;
    bool verified = check.VerifySignature(0,sigInfo);
    wprintf(L"signature verifies: %d\n",verified);

    //  Signing an encrypted PDF.  This one was opened with only the user password, and its
    //  permissions do not allow modifying, so another signature is refused.
    success = check.SetSigningCert(cert);
    if (success == false) {
        wprintf(L"%s\n",check.lastErrorText());
        return;
    }

    success = check.SignPdf(sigOpts,L"qa_output/memo_signed_twice.pdf");
    if (success == false) {
        wprintf(L"A second signature was refused: the permissions do not allow it.\n");
    }

    //  Loaded with the owner password, the same PDF can be signed.  It stays encrypted, and the
    //  first signature stays valid because the new one is added as an incremental update.
    CkPdfW owner;
    owner.put_UserPassword(L"user-secret");
    owner.put_OwnerPassword(L"owner-secret");
    success = owner.LoadFile(L"qa_output/memo_encrypted_signed.pdf");
    if (success == false) {
        wprintf(L"%s\n",owner.lastErrorText());
        return;
    }

    success = owner.SetSigningCert(cert);
    if (success == false) {
        wprintf(L"%s\n",owner.lastErrorText());
        return;
    }

    success = owner.SignPdf(sigOpts,L"qa_output/memo_signed_twice.pdf");
    if (success == false) {
        wprintf(L"%s\n",owner.lastErrorText());
        return;
    }

    CkPdfW twice;
    twice.put_UserPassword(L"user-secret");
    success = twice.LoadFile(L"qa_output/memo_signed_twice.pdf");
    if (success == false) {
        wprintf(L"%s\n",twice.lastErrorText());
        return;
    }

    wprintf(L"signatures after signing with owner rights: %d\n",twice.get_NumSignatures());
    verified = twice.VerifySignature(0,sigInfo);
    wprintf(L"first signature still verifies: %d\n",verified);
    verified = twice.VerifySignature(1,sigInfo);
    wprintf(L"second signature verifies: %d\n",verified);

    //  Sample output:

    //  algorithm: aes-256
    //  signatures: 1
    //  signature verifies: True
    //  A second signature was refused: the permissions do not allow it.
    //  signatures after signing with owner rights: 2
    //  first signature still verifies: True
    //  second signature verifies: True
    }