Android™ Requires Chilkat v11.7.0+
Android™
Fill a PDF Template: Replace {{placeholders}} with Values
See more PDF Text Replacement Examples
Demonstrates the TextReplace method on an invoice template whose values are written as placeholders such as {{customer}} and {{total}}. The "placeholders" object names each placeholder and the text to put in; every occurrence on the page is replaced, including a placeholder inside a sentence or inside a label such as "Tax ({{taxRate}}):". The text is redrawn with the font the template used, so the result looks like the template, and a value that is right-aligned in the template stays right-aligned. The changes are written by WriteFile.Chilkat Android™ Downloads
// 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;
}
// The template has these placeholders in its page text:
// {{customer}} (twice: "Bill To:" and the closing sentence), {{date}}, {{po}},
// two item rows: {{qty1}} {{desc1}} {{price1}} {{amount1}} and {{qty2}} {{desc2}} {{price2}} {{amount2}},
// {{subtotal}}, "Tax ({{taxRate}}):" with {{tax}}, and {{total}}.
CkJsonObject json = new CkJsonObject();
json.UpdateString("placeholders.customer","Acme Corp");
json.UpdateString("placeholders.date","2026-10-18");
json.UpdateString("placeholders.po","PO-88213");
json.UpdateString("placeholders.qty1","2");
json.UpdateString("placeholders.desc1","Cordless drill, 18 V");
json.UpdateString("placeholders.price1","$460.00");
json.UpdateString("placeholders.amount1","$920.00");
json.UpdateString("placeholders.qty2","6");
json.UpdateString("placeholders.desc2","Galvanized hinge, 4 inch");
json.UpdateString("placeholders.price2","$12.50");
json.UpdateString("placeholders.amount2","$75.00");
json.UpdateString("placeholders.subtotal","$995.00");
json.UpdateString("placeholders.taxRate","7.35%");
json.UpdateString("placeholders.tax","$73.13");
json.UpdateString("placeholders.total","$1,068.13");
// "apply" makes every replacement, or none: if any value cannot be drawn with the
// template's font, the method returns false, the PDF is unchanged, and jsonOut says why.
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;
}
}
// One entry per placeholder: the pages it was found on and how many times it was replaced.
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") + ": " + String.valueOf(jsonOut.IntOf("replacements[i].count")) + " on page(s) " + jsonOut.stringOf("replacements[i].pages"));
i = i + 1;
}
// The replacements stay in memory until the PDF is written.
success = pdf.WriteFile("qa_output/invoice_filled.pdf");
if (success == false) {
Log.i(TAG, pdf.lastErrorText());
return;
}
Log.i(TAG, "Wrote qa_output/invoice_filled.pdf");
// Sample output:
// customer: 2 on page(s) 1
// date: 1 on page(s) 1
// po: 1 on page(s) 1
// qty1: 1 on page(s) 1
// desc1: 1 on page(s) 1
// price1: 1 on page(s) 1
// amount1: 1 on page(s) 1
// qty2: 1 on page(s) 1
// desc2: 1 on page(s) 1
// price2: 1 on page(s) 1
// amount2: 1 on page(s) 1
// subtotal: 1 on page(s) 1
// taxRate: 1 on page(s) 1
// tax: 1 on page(s) 1
// total: 1 on page(s) 1
// Wrote qa_output/invoice_filled.pdf
}
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."
}
}