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

Extract a PDF Table with Merged Cells

See more PDF Text Extraction Examples

Demonstrates how merged cells are reported by the "tables" operation: a header cell that spans several columns has colSpan greater than 1, and a row label that spans several rows has rowSpan greater than 1. Also shows the minRows and minColumns options.

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 spans_table.pdf

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

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

// Grids smaller than minRows x minColumns are not reported as tables.  The defaults are 2 and 2.
// Setting them higher skips small boxes that are not really tables.
let _ = json.update_int("minRows", 3);
let _ = json.update_int("minColumns", 3);

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

println!("Tables found: {}", json_out.int_of("count"));
println!("{} rows x {} columns", json_out.int_of("tables[0].rows"), json_out.int_of("tables[0].columns"));

// A merged cell is reported once, at its top-left position, with its spans.
// The positions it covers have no cell of their own.
let mut row_span: i32 = 0;
let mut col_span: i32 = 0;
let mut j = 0;
let count_j = json_out.size_of_array("tables[0].cells");
while j < count_j {
    json_out.set_j(j);
    row_span = json_out.int_of("tables[0].cells[j].rowSpan");
    col_span = json_out.int_of("tables[0].cells[j].colSpan");
    if (row_span > 1) || (col_span > 1) {
        println!("row {}, col {}: \"{}\" spans {} row(s) and {} column(s)", json_out.int_of("tables[0].cells[j].row"), json_out.int_of("tables[0].cells[j].col"), json_out.string_of("tables[0].cells[j].text").unwrap_or_default(), row_span, col_span);
    }

    j = j + 1;
}

// Sample output:

// Tables found: 1
// 6 rows x 6 columns
// row 0, col 0: "Area" spans 2 row(s) and 1 column(s)
// row 0, col 1: "City" spans 2 row(s) and 1 column(s)
// row 0, col 2: "First Half" spans 1 row(s) and 2 column(s)
// row 0, col 4: "Second Half" spans 1 row(s) and 2 column(s)
// row 2, col 0: "East" spans 2 row(s) and 1 column(s)
// row 4, col 0: "West" spans 2 row(s) and 1 column(s)