Sample code for 30+ languages & platforms
Unicode C 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 Unicode C Downloads

Unicode C
#include <C_CkPdfW.h>
#include <C_CkJsonObjectW.h>
#include <C_CkStringBuilderW.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkPdfW pdf;
    HCkJsonObjectW json;
    HCkJsonObjectW jsonOut;
    HCkStringBuilderW sbRow;
    int currentRow;
    int row;
    int j;
    int count_j;
    int i;
    int count_i;

    success = FALSE;

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

    pdf = CkPdfW_Create();
    success = CkPdfW_LoadFile(pdf,L"qa_data/pdf/price_list.pdf");
    if (success == FALSE) {
        wprintf(L"%s\n",CkPdfW_lastErrorText(pdf));
        CkPdfW_Dispose(pdf);
        return;
    }

    json = CkJsonObjectW_Create();
    jsonOut = CkJsonObjectW_Create();

    //  The "tables" operation returns the tables found on the selected pages.
    //  Tables are recognized by their drawn cell borders.
    success = CkPdfW_ExtractText(pdf,L"tables",json,jsonOut);
    if (success == FALSE) {
        wprintf(L"%s\n",CkPdfW_lastErrorText(pdf));
        CkPdfW_Dispose(pdf);
        CkJsonObjectW_Dispose(json);
        CkJsonObjectW_Dispose(jsonOut);
        return;
    }

    wprintf(L"Number of tables: %d\n",CkJsonObjectW_IntOf(jsonOut,L"count"));

    //  Used to build each row of output.
    sbRow = CkStringBuilderW_Create();

    i = 0;
    count_i = CkJsonObjectW_SizeOfArray(jsonOut,L"tables");
    while (i < count_i) {
        CkJsonObjectW_putI(jsonOut,i);
        wprintf(L"Table on page %d: %d rows x %d columns, detected by %s\n",CkJsonObjectW_IntOf(jsonOut,L"tables[i].page"),CkJsonObjectW_IntOf(jsonOut,L"tables[i].rows")
            ,CkJsonObjectW_IntOf(jsonOut,L"tables[i].columns"),CkJsonObjectW_stringOf(jsonOut,L"tables[i].detectedBy"));

        //  The cells are listed row by row.  Print each row as its cells separated by " | ".
        currentRow = -1;
        CkStringBuilderW_SetString(sbRow,L"");
        j = 0;
        count_j = CkJsonObjectW_SizeOfArray(jsonOut,L"tables[i].cells");
        while (j < count_j) {
            CkJsonObjectW_putJ(jsonOut,j);
            row = CkJsonObjectW_IntOf(jsonOut,L"tables[i].cells[j].row");
            if (row != currentRow) {
                if (currentRow >= 0) {
                    wprintf(L"%s\n",CkStringBuilderW_getAsString(sbRow));
                }

                currentRow = row;
                CkStringBuilderW_SetString(sbRow,L"  ");
            }
            else {
                CkStringBuilderW_Append(sbRow,L" | ");
            }

            CkStringBuilderW_Append(sbRow,CkJsonObjectW_stringOf(jsonOut,L"tables[i].cells[j].text"));
            j = j + 1;
        }

        wprintf(L"%s\n",CkStringBuilderW_getAsString(sbRow));
        i = i + 1;
    }

    //  The cells of the first table as JSON:
    CkJsonObjectW_putEmitCompact(jsonOut,FALSE);
    wprintf(L"%s\n",CkJsonObjectW_emit(jsonOut));

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


    CkPdfW_Dispose(pdf);
    CkJsonObjectW_Dispose(json);
    CkJsonObjectW_Dispose(jsonOut);
    CkStringBuilderW_Dispose(sbRow);

    }