Sample code for 30+ languages & platforms
Android™ Requires Chilkat v11.7.0+

A PDF Label That Is Not on the Page: notFound or Ignore

See more PDF Text Replacement Examples

Demonstrates the "unmatchedValues" option. A label or placeholder given a value but not found on the selected pages is a problem with the reason notFound, which stops "apply", because a missing label usually means the wrong document or a typo in the label. With unmatchedValues "ignore" such a value is skipped and the rest is applied -- the right choice when one set of values serves several document layouts.

Chilkat Android™ Downloads

Android™
// Important: Don't forget to include the call to System.loadLibrary
// as shown at the bottom of this code sample.
package com.test;

import android.app.Activity;
import com.chilkatsoft.*;

import android.widget.TextView;
import android.os.Bundle;

public class SimpleActivity extends Activity {

  private static final String TAG = "Chilkat";

  // Called when the activity is first created.
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    boolean 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

    CkPdf pdf = new CkPdf();
    success = pdf.LoadFile("qa_data/pdf/invoice_labels.pdf");
    if (success == false) {
        Log.i(TAG, pdf.lastErrorText());
        return;
        }

    CkJsonObject json = new CkJsonObject();
    CkJsonObject jsonOut = new CkJsonObject();

    //  "Tracking Number:" is not on this invoice.
    json.UpdateString("labels[0].label","Invoice Number:");
    json.UpdateString("labels[0].value","INV-2026-0500");
    json.UpdateString("labels[1].label","Tracking Number:");
    json.UpdateString("labels[1].value","1Z999AA10123456784");

    success = pdf.TextReplace("check",json,jsonOut);
    if (success == false) {
        Log.i(TAG, pdf.lastErrorText());
        return;
        }

    Log.i(TAG, "default: ok = " + String.valueOf(jsonOut.BoolOf("ok")) + ", problem: \"" + jsonOut.stringOf("problems[0].label") + "\" " + jsonOut.stringOf("problems[0].reason"));

    //  With "ignore", the label that is not there is skipped.
    json.UpdateString("unmatchedValues","ignore");
    success = pdf.TextReplace("apply",json,jsonOut);
    if (success == false) {
        Log.i(TAG, pdf.lastErrorText());
        return;
        }

    Log.i(TAG, "ignore: applied = " + String.valueOf(jsonOut.BoolOf("applied")) + ", replacements: " + String.valueOf(jsonOut.SizeOfArray("replacements")));

    success = pdf.WriteFile("qa_output/invoice_renumbered.pdf");
    if (success == false) {
        Log.i(TAG, pdf.lastErrorText());
        return;
        }

    //  Sample output:

    //  default: ok = false, problem: "Tracking Number:" notFound
    //  ignore: applied = true, replacements: 1

  }

  static {
      System.loadLibrary("chilkat");

      // Note: If the incorrect library name is passed to System.loadLibrary,
      // then you will see the following error message at application startup:
      //"The application <your-application-name> has stopped unexpectedly. Please try again."
  }
}