Android™ Requires Chilkat v11.7.0+
Android™
Replace PDF Text on Selected Pages Only
See more PDF Text Replacement Examples
Demonstrates the "pages" option, which limits the search for placeholders and labels to the given pages: "all" (the default), "first", "last", "odd", "even", or a list such as "1,3,5-9". The two-page order confirmation carries the same "Status:" label on both pages; the status is changed on page 2 only, then on both pages.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 order_confirmation.pdf
CkPdf pdf = new CkPdf();
success = pdf.LoadFile("qa_data/pdf/order_confirmation.pdf");
if (success == false) {
Log.i(TAG, pdf.lastErrorText());
return;
}
CkJsonObject json = new CkJsonObject();
CkJsonObject jsonOut = new CkJsonObject();
// Both pages have "Status: Pending". With pages "2", only page 2 is searched.
json.UpdateString("pages","2");
json.UpdateString("labels[0].label","Status:");
json.UpdateString("labels[0].value","Shipped");
success = pdf.TextReplace("check",json,jsonOut);
if (success == false) {
Log.i(TAG, pdf.lastErrorText());
return;
}
Log.i(TAG, "pages 2: found on page(s) " + jsonOut.stringOf("replacements[0].pages"));
// With every page searched, the default occurrence ("first") still replaces one -- the first in reading order.
json.UpdateString("pages","all");
success = pdf.TextReplace("check",json,jsonOut);
if (success == false) {
Log.i(TAG, pdf.lastErrorText());
return;
}
Log.i(TAG, "pages all, first occurrence: found on page(s) " + jsonOut.stringOf("replacements[0].pages"));
// Every occurrence on every page.
json.UpdateString("labels[0].occurrence","all");
success = pdf.TextReplace("apply",json,jsonOut);
if (success == false) {
Log.i(TAG, pdf.lastErrorText());
return;
}
Log.i(TAG, "pages all, every occurrence: " + String.valueOf(jsonOut.IntOf("replacements[0].count")) + " replaced on page(s) " + jsonOut.stringOf("replacements[0].pages"));
success = pdf.WriteFile("qa_output/order_shipped.pdf");
if (success == false) {
Log.i(TAG, pdf.lastErrorText());
return;
}
// Sample output:
// pages 2: found on page(s) 2
// pages all, first occurrence: found on page(s) 1
// pages all, every occurrence: 2 replaced on page(s) 1,2
}
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."
}
}