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

Watermark a Signed PDF Without Breaking the Signature

See more PDF Watermark Examples

Demonstrates the one approach a signed PDF accepts while its signatures are being preserved. Adding a watermark to the page content rewrites the page, which invalidates every existing signature, so with PreserveSignatures true (the default) only the annotation approach is allowed: an annotation is added as an incremental update that leaves the signed bytes untouched. Removing anything is refused for the same reason. Set PreserveSignatures to false to allow the other approaches, and the signatures in the file stop verifying.

Chilkat C Downloads

C
#include <C_CkPdf.h>
#include <C_CkJsonObject.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkPdf pdf;
    HCkJsonObject json;
    HCkJsonObject jsonOut;
    HCkPdf check;
    HCkJsonObject sigInfo;
    BOOL verified;

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

    pdf = CkPdf_Create();
    success = CkPdf_LoadFile(pdf,"qa_data/pdf/invoice_signed.pdf");
    if (success == FALSE) {
        printf("%s\n",CkPdf_lastErrorText(pdf));
        CkPdf_Dispose(pdf);
        return;
    }

    printf("signatures: %d\n",CkPdf_getNumSignatures(pdf));

    json = CkJsonObject_Create();
    jsonOut = CkJsonObject_Create();
    CkJsonObject_UpdateString(json,"type","text");
    CkJsonObject_UpdateString(json,"text","PAID");
    CkJsonObject_UpdateString(json,"id","paid");
    CkJsonObject_UpdateString(json,"color","#2E7D32");
    CkJsonObject_UpdateString(json,"position.align","topRight");
    CkJsonObject_UpdateNumber(json,"position.margin","36");

    //  An artifact watermark changes the page content, so it is refused while the signatures matter.
    CkJsonObject_UpdateString(json,"approach","artifact");
    success = CkPdf_Watermark(pdf,"add",json,jsonOut);
    if (success == FALSE) {
        printf("The artifact approach was refused on a signed PDF, as it should be.\n");
    }

    //  An annotation is added without touching the signed bytes.
    CkJsonObject_UpdateString(json,"approach","annotation");
    success = CkPdf_Watermark(pdf,"add",json,jsonOut);
    if (success == FALSE) {
        printf("%s\n",CkPdf_lastErrorText(pdf));
        CkPdf_Dispose(pdf);
        CkJsonObject_Dispose(json);
        CkJsonObject_Dispose(jsonOut);
        return;
    }

    printf("annotation added, pages: %s\n",CkJsonObject_stringOf(jsonOut,"pages"));

    success = CkPdf_WriteFile(pdf,"qa_output/invoice_paid.pdf");
    if (success == FALSE) {
        printf("%s\n",CkPdf_lastErrorText(pdf));
        CkPdf_Dispose(pdf);
        CkJsonObject_Dispose(json);
        CkJsonObject_Dispose(jsonOut);
        return;
    }

    //  The signature still verifies: the original bytes are unchanged and the annotation was appended.
    check = CkPdf_Create();
    success = CkPdf_LoadFile(check,"qa_output/invoice_paid.pdf");
    if (success == FALSE) {
        printf("%s\n",CkPdf_lastErrorText(check));
        CkPdf_Dispose(pdf);
        CkJsonObject_Dispose(json);
        CkJsonObject_Dispose(jsonOut);
        CkPdf_Dispose(check);
        return;
    }

    sigInfo = CkJsonObject_Create();
    verified = CkPdf_VerifySignature(check,0,sigInfo);
    printf("signature still verifies: %d\n",verified);

    //  Sample output:

    //  signatures: 1
    //  The artifact approach was refused on a signed PDF, as it should be.
    //  annotation added, pages: 1
    //  signature still verifies: True


    CkPdf_Dispose(pdf);
    CkJsonObject_Dispose(json);
    CkJsonObject_Dispose(jsonOut);
    CkPdf_Dispose(check);
    CkJsonObject_Dispose(sigInfo);

    }