Android™ Requires Chilkat v11.7.0+
Android™
Replace Text in a Signed PDF (PreserveSignatures)
See more PDF Text Replacement Examples
Applying a text replacement rewrites the page content, which invalidates any digital signature on the PDF. While the PreserveSignatures property is true (the default), "apply" is refused on a signed PDF; "check" only reads and is always allowed. Setting PreserveSignatures to false allows the replacement; the signature field stays in the written PDF but no longer verifies, so the document should be signed again afterwards if a signature is wanted.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_signed.pdf
CkPdf pdf = new CkPdf();
success = pdf.LoadFile("qa_data/pdf/invoice_signed.pdf");
if (success == false) {
Log.i(TAG, pdf.lastErrorText());
return;
}
Log.i(TAG, "Signatures: " + String.valueOf(pdf.get_NumSignatures()));
CkJsonObject json = new CkJsonObject();
json.UpdateString("labels[0].label","Invoice Number:");
json.UpdateString("labels[0].value","INV-2026-0500");
// "check" reads only, so it works on a signed PDF.
CkJsonObject jsonOut = new CkJsonObject();
success = pdf.TextReplace("check",json,jsonOut);
if (success == false) {
Log.i(TAG, pdf.lastErrorText());
return;
}
Log.i(TAG, "check: ok = " + String.valueOf(jsonOut.BoolOf("ok")));
// "apply" is refused while PreserveSignatures is true (the default).
success = pdf.TextReplace("apply",json,jsonOut);
if (success == false) {
Log.i(TAG, "apply refused: " + pdf.lastErrorText());
}
// Allowing the signature to be broken:
pdf.put_PreserveSignatures(false);
success = pdf.TextReplace("apply",json,jsonOut);
if (success == false) {
Log.i(TAG, pdf.lastErrorText());
return;
}
Log.i(TAG, "applied: " + String.valueOf(jsonOut.BoolOf("applied")));
// The written PDF is a full rewrite with the replacement. The signature field is still in the document,
// but the bytes it signed are gone, so it no longer verifies.
success = pdf.WriteFile("qa_output/invoice_reissued_broken_signature.pdf");
if (success == false) {
Log.i(TAG, pdf.lastErrorText());
return;
}
Log.i(TAG, "Signatures after writing: " + String.valueOf(pdf.get_NumSignatures()));
CkJsonObject sigInfo = new CkJsonObject();
boolean validated = pdf.VerifySignature(0,sigInfo);
Log.i(TAG, "Signature 0 verifies: " + String.valueOf(validated));
// Sample output:
// Signatures: 1
// check: ok = true
// apply refused: ChilkatLog:
// TextReplace:
// DllDate: Nov 7 2026
// ChilkatVersion: 11.7.0
// UnlockPrefix: CHLKAT
// UnlockStatus: 2
// Architecture: Little Endian; 64-bit
// Language: Visual C++ 11.0 / x64
// VerboseLogging: 0
// replaceTextOp:
// op: apply
// The PDF is signed: replacing text rewrites the content and invalidates the signatures (set PreserveSignatures = false to allow it).
// --replaceTextOp
// Failed.
// --TextReplace
// --ChilkatLog
//
// applied: true
// Signatures after writing: 1
// Signature 0 verifies: false
}
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."
}
}