Unicode C Requires Chilkat v11.7.0+
Unicode C
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
#include <C_CkPdfW.h>
#include <C_CkJsonObjectW.h>
void ChilkatSample(void)
{
BOOL success;
HCkPdfW pdf;
HCkJsonObjectW json;
HCkJsonObjectW jsonOut;
int i;
int count_i;
HCkJsonObjectW jsonRead;
HCkJsonObjectW jsonText;
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;
}
// 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.
json = CkJsonObjectW_Create();
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"Invoice Date:");
CkJsonObjectW_UpdateString(json,L"labels[1].value",L"April 2, 2026");
CkJsonObjectW_UpdateString(json,L"labels[2].label",L"customer");
CkJsonObjectW_UpdateString(json,L"labels[2].value",L"Contoso Ltd");
CkJsonObjectW_UpdateString(json,L"labels[3].label",L"Total Due:");
CkJsonObjectW_UpdateString(json,L"labels[3].value",L"$1,999.99");
jsonOut = CkJsonObjectW_Create();
success = CkPdfW_TextReplace(pdf,L"apply",json,jsonOut);
if (success == FALSE) {
wprintf(L"Not applied: %s\n",CkJsonObjectW_emit(jsonOut));
if (success == FALSE) {
wprintf(L"%s\n",CkPdfW_lastErrorText(pdf));
CkPdfW_Dispose(pdf);
CkJsonObjectW_Dispose(json);
CkJsonObjectW_Dispose(jsonOut);
return;
}
}
i = 0;
count_i = CkJsonObjectW_SizeOfArray(jsonOut,L"replacements");
while (i < count_i) {
CkJsonObjectW_putI(jsonOut,i);
wprintf(L"Replaced the value of \"%s\" on page %s\n",CkJsonObjectW_stringOf(jsonOut,L"replacements[i].label"),CkJsonObjectW_stringOf(jsonOut,L"replacements[i].pages"));
i = i + 1;
}
success = CkPdfW_WriteFile(pdf,L"qa_output/invoice_reissued.pdf");
if (success == FALSE) {
wprintf(L"%s\n",CkPdfW_lastErrorText(pdf));
CkPdfW_Dispose(pdf);
CkJsonObjectW_Dispose(json);
CkJsonObjectW_Dispose(jsonOut);
return;
}
// Read the new values back with ExtractText's label option.
jsonRead = CkJsonObjectW_Create();
jsonText = CkJsonObjectW_Create();
CkJsonObjectW_UpdateString(jsonRead,L"label",L"Total Due");
CkJsonObjectW_UpdateString(jsonRead,L"labelValue",L"right");
success = CkPdfW_ExtractText(pdf,L"text",jsonRead,jsonText);
if (success == FALSE) {
wprintf(L"%s\n",CkPdfW_lastErrorText(pdf));
CkPdfW_Dispose(pdf);
CkJsonObjectW_Dispose(json);
CkJsonObjectW_Dispose(jsonOut);
CkJsonObjectW_Dispose(jsonRead);
CkJsonObjectW_Dispose(jsonText);
return;
}
wprintf(L"Total Due is now: %s\n",CkJsonObjectW_stringOf(jsonText,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
CkPdfW_Dispose(pdf);
CkJsonObjectW_Dispose(json);
CkJsonObjectW_Dispose(jsonOut);
CkJsonObjectW_Dispose(jsonRead);
CkJsonObjectW_Dispose(jsonText);
}