C Requires Chilkat v11.7.0+
C
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 C Downloads
#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 invoice_template.pdf
pdf = CkPdf_Create();
success = CkPdf_LoadFile(pdf,"qa_data/pdf/invoice_template.pdf");
if (success == FALSE) {
printf("%s\n",CkPdf_lastErrorText(pdf));
CkPdf_Dispose(pdf);
return;
}
json = CkJsonObject_Create();
jsonOut = CkJsonObject_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).
CkJsonObject_UpdateString(json,"unusedPlaceholders","keep");
// "Ref:" appears twice on the page (A-1 near the top, A-2 below the totals).
// The default replaces the first occurrence only.
CkJsonObject_UpdateString(json,"labels[0].label","Ref:");
CkJsonObject_UpdateString(json,"labels[0].value","B-7");
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("default: %d occurrence replaced\n",CkJsonObject_IntOf(jsonOut,"replacements[0].count"));
// The second occurrence only.
CkJsonObject_UpdateString(json,"labels[0].occurrence","2");
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("occurrence 2: %d occurrence replaced\n",CkJsonObject_IntOf(jsonOut,"replacements[0].count"));
// Every occurrence.
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("occurrence all: %d occurrences replaced\n",CkJsonObject_IntOf(jsonOut,"replacements[0].count"));
success = CkPdf_WriteFile(pdf,"qa_output/invoice_refs.pdf");
if (success == FALSE) {
printf("%s\n",CkPdf_lastErrorText(pdf));
CkPdf_Dispose(pdf);
CkJsonObject_Dispose(json);
CkJsonObject_Dispose(jsonOut);
return;
}
// Sample output:
// default: 1 occurrence replaced
// occurrence 2: 1 occurrence replaced
// occurrence all: 2 occurrences replaced
CkPdf_Dispose(pdf);
CkJsonObject_Dispose(json);
CkJsonObject_Dispose(jsonOut);
}