C Requires Chilkat v11.7.0+
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 C Downloads
#include <C_CkPdf.h>
#include <C_CkJsonObject.h>
void ChilkatSample(void)
{
BOOL success;
HCkPdf pdf;
HCkJsonObject json;
HCkJsonObject jsonOut;
int i;
int count_i;
HCkJsonObject jsonRead;
HCkJsonObject 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 = CkPdf_Create();
success = CkPdf_LoadFile(pdf,"qa_data/pdf/invoice_labels.pdf");
if (success == FALSE) {
printf("%s\n",CkPdf_lastErrorText(pdf));
CkPdf_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 = CkJsonObject_Create();
CkJsonObject_UpdateString(json,"labels[0].label","Invoice Number:");
CkJsonObject_UpdateString(json,"labels[0].value","INV-2026-0500");
CkJsonObject_UpdateString(json,"labels[1].label","Invoice Date:");
CkJsonObject_UpdateString(json,"labels[1].value","April 2, 2026");
CkJsonObject_UpdateString(json,"labels[2].label","customer");
CkJsonObject_UpdateString(json,"labels[2].value","Contoso Ltd");
CkJsonObject_UpdateString(json,"labels[3].label","Total Due:");
CkJsonObject_UpdateString(json,"labels[3].value","$1,999.99");
jsonOut = CkJsonObject_Create();
success = CkPdf_TextReplace(pdf,"apply",json,jsonOut);
if (success == FALSE) {
printf("Not applied: %s\n",CkJsonObject_emit(jsonOut));
if (success == FALSE) {
printf("%s\n",CkPdf_lastErrorText(pdf));
CkPdf_Dispose(pdf);
CkJsonObject_Dispose(json);
CkJsonObject_Dispose(jsonOut);
return;
}
}
i = 0;
count_i = CkJsonObject_SizeOfArray(jsonOut,"replacements");
while (i < count_i) {
CkJsonObject_putI(jsonOut,i);
printf("Replaced the value of \"%s\" on page %s\n",CkJsonObject_stringOf(jsonOut,"replacements[i].label"),CkJsonObject_stringOf(jsonOut,"replacements[i].pages"));
i = i + 1;
}
success = CkPdf_WriteFile(pdf,"qa_output/invoice_reissued.pdf");
if (success == FALSE) {
printf("%s\n",CkPdf_lastErrorText(pdf));
CkPdf_Dispose(pdf);
CkJsonObject_Dispose(json);
CkJsonObject_Dispose(jsonOut);
return;
}
// Read the new values back with ExtractText's label option.
jsonRead = CkJsonObject_Create();
jsonText = CkJsonObject_Create();
CkJsonObject_UpdateString(jsonRead,"label","Total Due");
CkJsonObject_UpdateString(jsonRead,"labelValue","right");
success = CkPdf_ExtractText(pdf,"text",jsonRead,jsonText);
if (success == FALSE) {
printf("%s\n",CkPdf_lastErrorText(pdf));
CkPdf_Dispose(pdf);
CkJsonObject_Dispose(json);
CkJsonObject_Dispose(jsonOut);
CkJsonObject_Dispose(jsonRead);
CkJsonObject_Dispose(jsonText);
return;
}
printf("Total Due is now: %s\n",CkJsonObject_stringOf(jsonText,"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
CkPdf_Dispose(pdf);
CkJsonObject_Dispose(json);
CkJsonObject_Dispose(jsonOut);
CkJsonObject_Dispose(jsonRead);
CkJsonObject_Dispose(jsonText);
}