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

A PDF Label That Is Not on the Page: notFound or Ignore

See more PDF Text Replacement Examples

Demonstrates the "unmatchedValues" option. A label or placeholder given a value but not found on the selected pages is a problem with the reason notFound, which stops "apply", because a missing label usually means the wrong document or a typo in the label. With unmatchedValues "ignore" such a value is skipped and the rest is applied -- the right choice when one set of values serves several document layouts.

Chilkat Unicode C Downloads

Unicode C
#include <C_CkPdfW.h>
#include <C_CkJsonObjectW.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkPdfW pdf;
    HCkJsonObjectW json;
    HCkJsonObjectW jsonOut;

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

    pdf = CkPdfW_Create();
    success = CkPdfW_LoadFile(pdf,L"qa_data/pdf/invoice_labels.pdf");
    if (success == FALSE) {
        wprintf(L"%s\n",CkPdfW_lastErrorText(pdf));
        CkPdfW_Dispose(pdf);
        return;
    }

    json = CkJsonObjectW_Create();
    jsonOut = CkJsonObjectW_Create();

    //  "Tracking Number:" is not on this invoice.
    CkJsonObjectW_UpdateString(json,L"labels[0].label",L"Invoice Number:");
    CkJsonObjectW_UpdateString(json,L"labels[0].value",L"INV-2026-0500");
    CkJsonObjectW_UpdateString(json,L"labels[1].label",L"Tracking Number:");
    CkJsonObjectW_UpdateString(json,L"labels[1].value",L"1Z999AA10123456784");

    success = CkPdfW_TextReplace(pdf,L"check",json,jsonOut);
    if (success == FALSE) {
        wprintf(L"%s\n",CkPdfW_lastErrorText(pdf));
        CkPdfW_Dispose(pdf);
        CkJsonObjectW_Dispose(json);
        CkJsonObjectW_Dispose(jsonOut);
        return;
    }

    wprintf(L"default: ok = %d, problem: \"%s\" %s\n",CkJsonObjectW_BoolOf(jsonOut,L"ok"),CkJsonObjectW_stringOf(jsonOut,L"problems[0].label"),CkJsonObjectW_stringOf(jsonOut,L"problems[0].reason"));

    //  With "ignore", the label that is not there is skipped.
    CkJsonObjectW_UpdateString(json,L"unmatchedValues",L"ignore");
    success = CkPdfW_TextReplace(pdf,L"apply",json,jsonOut);
    if (success == FALSE) {
        wprintf(L"%s\n",CkPdfW_lastErrorText(pdf));
        CkPdfW_Dispose(pdf);
        CkJsonObjectW_Dispose(json);
        CkJsonObjectW_Dispose(jsonOut);
        return;
    }

    wprintf(L"ignore: applied = %d, replacements: %d\n",CkJsonObjectW_BoolOf(jsonOut,L"applied"),CkJsonObjectW_SizeOfArray(jsonOut,L"replacements"));

    success = CkPdfW_WriteFile(pdf,L"qa_output/invoice_renumbered.pdf");
    if (success == FALSE) {
        wprintf(L"%s\n",CkPdfW_lastErrorText(pdf));
        CkPdfW_Dispose(pdf);
        CkJsonObjectW_Dispose(json);
        CkJsonObjectW_Dispose(jsonOut);
        return;
    }

    //  Sample output:

    //  default: ok = false, problem: "Tracking Number:" notFound
    //  ignore: applied = true, replacements: 1


    CkPdfW_Dispose(pdf);
    CkJsonObjectW_Dispose(json);
    CkJsonObjectW_Dispose(jsonOut);

    }