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

Optimizing a Signed PDF (PreserveSignatures)

See more PDF Optimization Examples

Demonstrates what happens when a signed PDF is optimized. Optimizing rewrites the document, and a rewrite breaks every existing digital signature, so with PreserveSignatures true (the default) Optimize refuses and LastErrorText says why. Setting PreserveSignatures to false allows it, and the signature that was in the file no longer verifies afterwards. "audit" is always allowed, because it changes nothing.

Chilkat C Downloads

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

void ChilkatSample(void)
    {
    BOOL success;
    HCkPdf pdf;
    HCkJsonObject json;
    HCkJsonObject jsonOut;
    HCkPdf pdf2;
    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();

    //  An audit changes nothing, so it is allowed on a signed PDF.
    success = CkPdf_Optimize(pdf,"audit",json,jsonOut);
    if (success == FALSE) {
        printf("%s\n",CkPdf_lastErrorText(pdf));
        CkPdf_Dispose(pdf);
        CkJsonObject_Dispose(json);
        CkJsonObject_Dispose(jsonOut);
        return;
    }

    printf("audit ok, estimated savings: %d\n",CkJsonObject_IntOf(jsonOut,"estimatedSavings.total"));

    //  Optimizing is refused while PreserveSignatures is true.
    success = CkPdf_Optimize(pdf,"optimize",json,jsonOut);
    if (success == FALSE) {
        printf("Refused while PreserveSignatures is true.\n");
    }

    //  Allow the rewrite.  Do this only when the signatures no longer matter -- an invoice being
    //  archived, say, where the signed original is kept elsewhere.
    CkPdf_putPreserveSignatures(pdf,FALSE);
    success = CkPdf_Optimize(pdf,"optimize",json,jsonOut);
    if (success == FALSE) {
        printf("%s\n",CkPdf_lastErrorText(pdf));
        CkPdf_Dispose(pdf);
        CkJsonObject_Dispose(json);
        CkJsonObject_Dispose(jsonOut);
        return;
    }

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

    printf("optimized, bytesSaved: %d\n",CkJsonObject_IntOf(jsonOut,"bytesSaved"));

    //  The signature is still in the file, but it no longer verifies: the bytes it covered have changed.
    pdf2 = CkPdf_Create();
    success = CkPdf_LoadFile(pdf2,"qa_output/invoice_optimized.pdf");
    if (success == FALSE) {
        printf("%s\n",CkPdf_lastErrorText(pdf2));
        CkPdf_Dispose(pdf);
        CkJsonObject_Dispose(json);
        CkJsonObject_Dispose(jsonOut);
        CkPdf_Dispose(pdf2);
        return;
    }

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

    sigInfo = CkJsonObject_Create();
    verified = CkPdf_VerifySignature(pdf2,0,sigInfo);
    printf("verifies after optimizing: %d\n",verified);


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

    }