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

Replace the Second (or Every) Occurrence of a Label in a PDF

See more PDF Text Replacement Examples

Demonstrates the "occurrence" member of a label entry. By default the first occurrence of a label, in reading order across the selected pages, is replaced; "occurrence" can be a number for the nth, or "all" for every occurrence. The invoice template has "Ref:" twice.

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

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

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

    //  This template's {{placeholders}} are not being filled here: "keep" leaves them as they are
    //  (the default, "error", would refuse to apply while a placeholder on the page has no value).
    CkJsonObjectW_UpdateString(json,L"unusedPlaceholders",L"keep");

    //  "Ref:" appears twice on the page (A-1 near the top, A-2 below the totals).
    //  The default replaces the first occurrence only.
    CkJsonObjectW_UpdateString(json,L"labels[0].label",L"Ref:");
    CkJsonObjectW_UpdateString(json,L"labels[0].value",L"B-7");
    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: %d occurrence replaced\n",CkJsonObjectW_IntOf(jsonOut,L"replacements[0].count"));

    //  The second occurrence only.
    CkJsonObjectW_UpdateString(json,L"labels[0].occurrence",L"2");
    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"occurrence 2: %d occurrence replaced\n",CkJsonObjectW_IntOf(jsonOut,L"replacements[0].count"));

    //  Every occurrence.
    CkJsonObjectW_UpdateString(json,L"labels[0].occurrence",L"all");
    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"occurrence all: %d occurrences replaced\n",CkJsonObjectW_IntOf(jsonOut,L"replacements[0].count"));

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

    //  Sample output:

    //  default: 1 occurrence replaced
    //  occurrence 2: 1 occurrence replaced
    //  occurrence all: 2 occurrences replaced


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

    }