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

PDF Placeholders with Other Delimiters: [[name]]

See more PDF Text Replacement Examples

Demonstrates the "placeholderSyntax" option. Placeholders are {{name}} by default; a template written with other delimiters, such as [[name]] or <>, says so with placeholderSyntax, written around the word "name".

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

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

    //  The letter reads: "Dear [[name]]," ... "expires on [[expiry]]" ... "[[amount]] per year" ... "[[agent]]".
    CkJsonObject json = new CkJsonObject();
    json.UpdateString("placeholderSyntax","[[name]]");
    json.UpdateString("placeholders.date","October 18, 2026");
    json.UpdateString("placeholders.name","Ms. Rivera");
    json.UpdateString("placeholders.expiry","December 31, 2026");
    json.UpdateString("placeholders.amount","$299.00");
    json.UpdateString("placeholders.agent","Dana Whitfield, Account Manager");

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

        }

    int i = 0;
    int count_i = jsonOut.SizeOfArray("replacements");
    while (i < count_i) {
        jsonOut.put_I(i);
        Log.i(TAG, "[[" + jsonOut.stringOf("replacements[i].field") + "]] replaced");
        i = i + 1;
        }

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

    //  Sample output:

    //  [[date]] replaced
    //  [[name]] replaced
    //  [[expiry]] replaced
    //  [[amount]] replaced
    //  [[agent]] replaced

  }

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