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

Replace PDF Text on Selected Pages Only

See more PDF Text Replacement Examples

Demonstrates the "pages" option, which limits the search for placeholders and labels to the given pages: "all" (the default), "first", "last", "odd", "even", or a list such as "1,3,5-9". The two-page order confirmation carries the same "Status:" label on both pages; the status is changed on page 2 only, then on both pages.

Chilkat C Downloads

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

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

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

    json = CkJsonObject_Create();
    jsonOut = CkJsonObject_Create();

    //  Both pages have "Status:   Pending".  With pages "2", only page 2 is searched.
    CkJsonObject_UpdateString(json,"pages","2");
    CkJsonObject_UpdateString(json,"labels[0].label","Status:");
    CkJsonObject_UpdateString(json,"labels[0].value","Shipped");
    success = CkPdf_TextReplace(pdf,"check",json,jsonOut);
    if (success == FALSE) {
        printf("%s\n",CkPdf_lastErrorText(pdf));
        CkPdf_Dispose(pdf);
        CkJsonObject_Dispose(json);
        CkJsonObject_Dispose(jsonOut);
        return;
    }

    printf("pages 2: found on page(s) %s\n",CkJsonObject_stringOf(jsonOut,"replacements[0].pages"));

    //  With every page searched, the default occurrence ("first") still replaces one -- the first in reading order.
    CkJsonObject_UpdateString(json,"pages","all");
    success = CkPdf_TextReplace(pdf,"check",json,jsonOut);
    if (success == FALSE) {
        printf("%s\n",CkPdf_lastErrorText(pdf));
        CkPdf_Dispose(pdf);
        CkJsonObject_Dispose(json);
        CkJsonObject_Dispose(jsonOut);
        return;
    }

    printf("pages all, first occurrence: found on page(s) %s\n",CkJsonObject_stringOf(jsonOut,"replacements[0].pages"));

    //  Every occurrence on every page.
    CkJsonObject_UpdateString(json,"labels[0].occurrence","all");
    success = CkPdf_TextReplace(pdf,"apply",json,jsonOut);
    if (success == FALSE) {
        printf("%s\n",CkPdf_lastErrorText(pdf));
        CkPdf_Dispose(pdf);
        CkJsonObject_Dispose(json);
        CkJsonObject_Dispose(jsonOut);
        return;
    }

    printf("pages all, every occurrence: %d replaced on page(s) %s\n",CkJsonObject_IntOf(jsonOut,"replacements[0].count"),CkJsonObject_stringOf(jsonOut,"replacements[0].pages"));

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

    //  Sample output:

    //  pages 2: found on page(s) 2
    //  pages all, first occurrence: found on page(s) 1
    //  pages all, every occurrence: 2 replaced on page(s) 1,2


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

    }