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

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 Rust Downloads

Rust

// 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

let pdf = chilkat::Pdf::new();
if pdf.load_file("qa_data/pdf/invoice_signed.pdf").is_err() {
    println!("{}", pdf.last_error_text());
    return;
}

println!("Signatures: {}", pdf.num_signatures());

let json = chilkat::JsonObject::new();
let _ = json.update_string("labels[0].label", "Invoice Number:");
let _ = json.update_string("labels[0].value", "INV-2026-0500");

// "check" reads only, so it works on a signed PDF.
let json_out = chilkat::JsonObject::new();
if pdf.text_replace("check", &json, &json_out).is_err() {
    println!("{}", pdf.last_error_text());
    return;
}

println!("check: ok = {}", json_out.bool_of("ok"));

// "apply" is refused while PreserveSignatures is true (the default).
if pdf.text_replace("apply", &json, &json_out).is_err() {
    println!("apply refused: {}", pdf.last_error_text());
}

// Allowing the signature to be broken:
pdf.set_preserve_signatures(false);
if pdf.text_replace("apply", &json, &json_out).is_err() {
    println!("{}", pdf.last_error_text());
    return;
}

println!("applied: {}", json_out.bool_of("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.
if pdf.write_file("qa_output/invoice_reissued_broken_signature.pdf").is_err() {
    println!("{}", pdf.last_error_text());
    return;
}

println!("Signatures after writing: {}", pdf.num_signatures());
let sig_info = chilkat::JsonObject::new();
let validated = pdf.verify_signature(0, &sig_info).is_ok();
println!("Signature 0 verifies: {}", 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