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

Get a PDF Field Value by its Label (Value to the Right)

See more PDF Text Extraction Examples

Demonstrates the "label" option, which captures the text that belongs to a caption such as "Invoice Number:" without needing coordinates. With labelValue "right", the value is the rest of the label's line. Label matching ignores case, extra spaces and a trailing colon.

Chilkat Rust Downloads

Rust

// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.

// The invoice used in this example is available at invoice_labels.pdf

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

let json = chilkat::JsonObject::new();
let json_out = chilkat::JsonObject::new();

// The invoice has lines such as "Invoice Number:   INV-2026-0412".
// The label option restricts the "text" operation to the text belonging to the label.
let _ = json.update_string("label", "Invoice Number");

// "right" means the value is the rest of the label's line.
// (The default, "auto", uses the rest of the line if there is text there, otherwise the block below.)
let _ = json.update_string("labelValue", "right");

if pdf.extract_text("text", &json, &json_out).is_err() {
    println!("{}", pdf.last_error_text());
    return;
}

// The value is returned as the text.
println!("Invoice Number: {}", json_out.string_of("text").unwrap_or_default());

// The labelMatches array tells where the label was found and how it appeared in the PDF.
println!("Found on page {} as \"{}\"", json_out.int_of("labelMatches[0].page"), json_out.string_of("labelMatches[0].label").unwrap_or_default());

// Matching ignores case and the trailing colon, so these all work:
let _ = json.update_string("label", "CUSTOMER:");
if pdf.extract_text("text", &json, &json_out).is_err() {
    println!("{}", pdf.last_error_text());
    return;
}

println!("Customer: {}", json_out.string_of("text").unwrap_or_default());

let _ = json.update_string("label", "due date");
if pdf.extract_text("text", &json, &json_out).is_err() {
    println!("{}", pdf.last_error_text());
    return;
}

println!("Due Date: {}", json_out.string_of("text").unwrap_or_default());

// The totals at the foot of the invoice are right-aligned labels with the amount beside them.
let _ = json.update_string("label", "Total Due");
if pdf.extract_text("text", &json, &json_out).is_err() {
    println!("{}", pdf.last_error_text());
    return;
}

println!("Total Due: {}", json_out.string_of("text").unwrap_or_default());

// A label that is not found is not an error.  The text is empty, labelMatches is empty,
// and the quality report has an issue with the code "labelNotFound".
let _ = json.update_string("label", "Tracking Number");
if pdf.extract_text("text", &json, &json_out).is_err() {
    println!("{}", pdf.last_error_text());
    return;
}

if json_out.size_of_array("labelMatches") == 0 {
    println!("Tracking Number: (not found) {}", json_out.string_of("quality.pages[0].issues[0].code").unwrap_or_default());
}

// Sample output:

// Invoice Number: INV-2026-0412
// Found on page 1 as "Invoice Number:"
// Customer: Northwind Traders
// Due Date: April 13, 2026
// Total Due: $1,234.53
// Tracking Number: (not found) labelNotFound