Rust Requires Chilkat v11.7.0+
Rust
Extract Tables from a PDF
See more PDF Text Extraction Examples
Demonstrates the "tables" operation, which returns the tables detected on the pages as rows and cells. Each cell has its row and column (counted from 0), its row and column span, and its text.Chilkat Rust Downloads
// 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 price_list.pdf
let pdf = chilkat::Pdf::new();
if pdf.load_file("qa_data/pdf/price_list.pdf").is_err() {
println!("{}", pdf.last_error_text());
return;
}
let json = chilkat::JsonObject::new();
let json_out = chilkat::JsonObject::new();
// The "tables" operation returns the tables found on the selected pages.
// Tables are recognized by their drawn cell borders.
if pdf.extract_text("tables", &json, &json_out).is_err() {
println!("{}", pdf.last_error_text());
return;
}
println!("Number of tables: {}", json_out.int_of("count"));
// Used to build each row of output.
let sb_row = chilkat::StringBuilder::new();
let mut current_row: i32 = 0;
let mut row: i32 = 0;
let mut j: i32 = 0;
let mut count_j: i32 = 0;
let mut i = 0;
let count_i = json_out.size_of_array("tables");
while i < count_i {
json_out.set_i(i);
println!("Table on page {}: {} rows x {} columns, detected by {}", json_out.int_of("tables[i].page"), json_out.int_of("tables[i].rows"), json_out.int_of("tables[i].columns"), json_out.string_of("tables[i].detectedBy").unwrap_or_default());
// The cells are listed row by row. Print each row as its cells separated by " | ".
current_row = -1;
let _ = sb_row.set_string("");
j = 0;
count_j = json_out.size_of_array("tables[i].cells");
while j < count_j {
json_out.set_j(j);
row = json_out.int_of("tables[i].cells[j].row");
if row != current_row {
if current_row >= 0 {
println!("{}", sb_row.get_as_string().unwrap_or_default());
}
current_row = row;
let _ = sb_row.set_string(" ");
} else {
let _ = sb_row.append(" | ");
}
let _ = sb_row.append(&json_out.string_of("tables[i].cells[j].text").unwrap_or_default());
j = j + 1;
}
println!("{}", sb_row.get_as_string().unwrap_or_default());
i = i + 1;
}
// The cells of the first table as JSON:
json_out.set_emit_compact(false);
println!("{}", json_out.emit().unwrap_or_default());
// Sample output:
// Number of tables: 1
// Table on page 1: 8 rows x 5 columns, detected by ruled
// Code | Product | Unit | Price | In Stock
// HT-101 | Claw hammer, 16 oz | each | $18.00 | 24
// HT-102 | Adjustable wrench, 10 in | each | $14.50 | 31
// HT-103 | Screwdriver set, 6 piece | set | $22.00 | 12
// HT-104 | Tape measure, 25 ft | each | $9.75 | 40
// HT-105 | Utility knife | each | $6.20 | 58
// HT-106 | Pliers, needle nose | each | $11.00 | 19
// HT-107 | Hand saw, 20 in | each | $21.40 | 8
// {
// "count": 1,
// "tables": [
// {
// "page": 1,
// "detectedBy": "ruled",
// "rows": 8,
// "columns": 5,
// "cells": [
// {
// "row": 0,
// "col": 0,
// "rowSpan": 1,
// "colSpan": 1,
// "text": "Code"
// },
// {
// "row": 0,
// "col": 1,
// "rowSpan": 1,
// "colSpan": 1,
// "text": "Product"
// },
// ...
// ]
// }
// ],
// "quality": {
// "rating": "good",
// ...
// }
// }