Sample code for 30+ languages & platforms
React Native 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 React Native Downloads

React Native
import { JsonObject, Pdf } from '@chilkat/react-native'

function chilkatExample() {
  let 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

  const pdf = new Pdf();
  success = true;
  try {
    pdf.loadFile('qa_data/pdf/invoice_labels.pdf');
  } catch {
    success = false;
  }
  if (!success) {
    console.log(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.
  const json = new JsonObject();
  json.updateString('labels[0].label', 'Invoice Number:');
  json.updateString('labels[0].value', 'INV-2026-0500');
  json.updateString('labels[1].label', 'Invoice Date:');
  json.updateString('labels[1].value', 'April 2, 2026');
  json.updateString('labels[2].label', 'customer');
  json.updateString('labels[2].value', 'Contoso Ltd');
  json.updateString('labels[3].label', 'Total Due:');
  json.updateString('labels[3].value', '$1,999.99');

  const jsonOut = new JsonObject();
  success = true;
  try {
    pdf.textReplace('apply', json, jsonOut);
  } catch {
    success = false;
  }
  if (!success) {
    console.log(`Not applied: ${jsonOut.emit()}`);
    if (!success) {
      console.log(pdf.lastErrorText);
      return;
    }
  }

  let i = 0;
  const countI = jsonOut.sizeOfArray('replacements');
  while (i < countI) {
    jsonOut.i = i;
    console.log(`Replaced the value of "${jsonOut.stringOf('replacements[i].label')}" on page ${jsonOut.stringOf('replacements[i].pages')}`);
    i++;
  }

  success = true;
  try {
    pdf.writeFile('qa_output/invoice_reissued.pdf');
  } catch {
    success = false;
  }
  if (!success) {
    console.log(pdf.lastErrorText);
    return;
  }

  // Read the new values back with ExtractText's label option.
  const jsonRead = new JsonObject();
  const jsonText = new JsonObject();
  jsonRead.updateString('label', 'Total Due');
  jsonRead.updateString('labelValue', 'right');
  success = true;
  try {
    pdf.extractText('text', jsonRead, jsonText);
  } catch {
    success = false;
  }
  if (!success) {
    console.log(pdf.lastErrorText);
    return;
  }

  console.log(`Total Due is now: ${jsonText.stringOf('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
}