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

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

Zig
const std = @import("std");
const chilkat = @import("chilkat");

pub fn main(init: std.process.Init) !void {
    const alloc = init.arena.allocator();

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

    const pdf = try chilkat.Pdf.init();
    defer pdf.deinit();
    pdf.loadFile("qa_data/pdf/price_list.pdf") catch {
        std.debug.print("{s}\n", .{try pdf.getLastErrorText(alloc)});
        return;
    };

    const json = try chilkat.JsonObject.init();
    defer json.deinit();
    const json_out = try chilkat.JsonObject.init();
    defer json_out.deinit();

    // The "tables" operation returns the tables found on the selected pages.
    // Tables are recognized by their drawn cell borders.
    pdf.extractText("tables", json, json_out) catch {
        std.debug.print("{s}\n", .{try pdf.getLastErrorText(alloc)});
        return;
    };

    std.debug.print("Number of tables: {d}\n", .{json_out.intOf("count")});

    // Used to build each row of output.
    const sb_row = try chilkat.StringBuilder.init();
    defer sb_row.deinit();
    var current_row: i32 = 0;
    var row: i32 = 0;
    var j: i32 = 0;
    var count_j: i32 = 0;

    var i: i32 = 0;
    const count_i = json_out.sizeOfArray("tables");
    while (i < count_i) {
        json_out.setI(i);
        std.debug.print("Table on page {d}: {d} rows x {d} columns, detected by {s}\n", .{ json_out.intOf("tables[i].page"), json_out.intOf("tables[i].rows"), json_out.intOf("tables[i].columns"), try json_out.stringOf(alloc, "tables[i].detectedBy") });

        // The cells are listed row by row.  Print each row as its cells separated by " | ".
        current_row = -1;
        sb_row.setString("") catch {};
        j = 0;
        count_j = json_out.sizeOfArray("tables[i].cells");
        while (j < count_j) {
            json_out.setJ(j);
            row = json_out.intOf("tables[i].cells[j].row");
            if (row != current_row) {
                if (current_row >= 0) {
                    std.debug.print("{s}\n", .{try sb_row.getAsString(alloc)});
                }

                current_row = row;
                sb_row.setString("  ") catch {};
            } else {
                sb_row.append(" | ") catch {};
            }

            sb_row.append(try json_out.stringOf(alloc, "tables[i].cells[j].text")) catch {};
            j = j + 1;
        }

        std.debug.print("{s}\n", .{try sb_row.getAsString(alloc)});
        i = i + 1;
    }

    // The cells of the first table as JSON:
    json_out.setEmitCompact(false);
    std.debug.print("{s}\n", .{try json_out.emit(alloc)});

    // 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",
    //     ...
    //   }
    // }
}