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

Replace the Value After a Label in a PDF (Re-issue an Invoice)

See more PDF Text Replacement Examples

Demonstrates replacing the values that follow labels such as "Invoice Number:" on a PDF that has no placeholders -- an ordinary invoice. Each entry of the "labels" array names a label as it appears on the page and the new value; the value replaces the rest of the label's line. Label matching ignores case, extra spaces and a trailing colon. The replaced text is read back with ExtractText to confirm it.

Chilkat Unicode C++ Downloads

Unicode C++
#include <CkPdfW.h>
#include <CkJsonObjectW.h>

void ChilkatSample(void)
    {
    bool 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

    CkPdfW pdf;
    success = pdf.LoadFile(L"qa_data/pdf/invoice_labels.pdf");
    if (success == false) {
        wprintf(L"%s\n",pdf.lastErrorText());
        return;
    }

    //  The invoice has lines such as "Invoice Number:   INV-2026-0412" and "Total Due:  $1,234.53".
    //  Each label's new value replaces whatever follows the label on its line.
    CkJsonObjectW json;
    json.UpdateString(L"labels[0].label",L"Invoice Number:");
    json.UpdateString(L"labels[0].value",L"INV-2026-0500");
    json.UpdateString(L"labels[1].label",L"Invoice Date:");
    json.UpdateString(L"labels[1].value",L"April 2, 2026");
    json.UpdateString(L"labels[2].label",L"customer");
    json.UpdateString(L"labels[2].value",L"Contoso Ltd");
    json.UpdateString(L"labels[3].label",L"Total Due:");
    json.UpdateString(L"labels[3].value",L"$1,999.99");

    CkJsonObjectW jsonOut;
    success = pdf.TextReplace(L"apply",json,jsonOut);
    if (success == false) {
        wprintf(L"Not applied: %s\n",jsonOut.emit());
        if (success == false) {
            wprintf(L"%s\n",pdf.lastErrorText());
            return;
        }

    }

    int i = 0;
    int count_i = jsonOut.SizeOfArray(L"replacements");
    while (i < count_i) {
        jsonOut.put_I(i);
        wprintf(L"Replaced the value of \"%s\" on page %s\n",jsonOut.stringOf(L"replacements[i].label"),jsonOut.stringOf(L"replacements[i].pages"));
        i = i + 1;
    }

    success = pdf.WriteFile(L"qa_output/invoice_reissued.pdf");
    if (success == false) {
        wprintf(L"%s\n",pdf.lastErrorText());
        return;
    }

    //  Read the new values back with ExtractText's label option.
    CkJsonObjectW jsonRead;
    CkJsonObjectW jsonText;
    jsonRead.UpdateString(L"label",L"Total Due");
    jsonRead.UpdateString(L"labelValue",L"right");
    success = pdf.ExtractText(L"text",jsonRead,jsonText);
    if (success == false) {
        wprintf(L"%s\n",pdf.lastErrorText());
        return;
    }

    wprintf(L"Total Due is now: %s\n",jsonText.stringOf(L"text"));

    //  Sample output:

    //  Replaced the value of "Invoice Number:" on page 1
    //  Replaced the value of "Invoice Date:" on page 1
    //  Replaced the value of "customer" on page 1
    //  Replaced the value of "Total Due:" on page 1
    //  Total Due is now: $1,999.99
    }