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

What PDF Text Replacement Does Not Change: Metadata, Links, Form Fields

See more PDF Text Replacement Examples

TextReplace changes the page content only. A placeholder that also appears in the document's metadata (the Title, Author, Subject, Keywords, Creator or Producer), in a link's target URL, or in a form field's value is left as it is and reported in "notReplaced" with "where" saying which. The invoice template's title is "Invoice for {{customer}}" and its "View this invoice online" link points at a URL that holds {{customer}}.

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_template.pdf

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

    CkJsonObject json = new CkJsonObject();
    json.UpdateString("placeholders.customer","Acme Corp");
    //  (The other placeholders are left as they are for this example.)
    json.UpdateString("unusedPlaceholders","keep");

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

    Log.i(TAG, "In the page content, customer was replaced " + String.valueOf(jsonOut.IntOf("replacements[0].count")) + " times.");

    //  Where else the placeholder occurs, untouched:
    int i = 0;
    int count_i = jsonOut.SizeOfArray("notReplaced");
    while (i < count_i) {
        jsonOut.put_I(i);
        Log.i(TAG, "not replaced: {{" + jsonOut.stringOf("notReplaced[i].field") + "}} in " + jsonOut.stringOf("notReplaced[i].where"));
        i = i + 1;
        }

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

    //  Sample output:

    //  In the page content, customer was replaced 2 times.
    //  not replaced: {{customer}} in metadata
    //  not replaced: {{customer}} in linkTarget

  }

  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."
  }
}