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

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

void ChilkatSample(void)
    {
    BOOL success;
    HCkPdfW pdf;
    HCkJsonObjectW json;
    HCkJsonObjectW jsonOut;
    int rowSpan;
    int colSpan;
    int j;
    int count_j;

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

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

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

    //  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.
    CkJsonObjectW_UpdateInt(json,L"minRows",3);
    CkJsonObjectW_UpdateInt(json,L"minColumns",3);

    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"Tables found: %d\n",CkJsonObjectW_IntOf(jsonOut,L"count"));
    wprintf(L"%d rows x %d columns\n",CkJsonObjectW_IntOf(jsonOut,L"tables[0].rows"),CkJsonObjectW_IntOf(jsonOut,L"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.

    j = 0;
    count_j = CkJsonObjectW_SizeOfArray(jsonOut,L"tables[0].cells");
    while (j < count_j) {
        CkJsonObjectW_putJ(jsonOut,j);
        rowSpan = CkJsonObjectW_IntOf(jsonOut,L"tables[0].cells[j].rowSpan");
        colSpan = CkJsonObjectW_IntOf(jsonOut,L"tables[0].cells[j].colSpan");
        if ((rowSpan > 1) || (colSpan > 1)) {
            wprintf(L"row %d, col %d: \"%s\" spans %d row(s) and %d column(s)\n",CkJsonObjectW_IntOf(jsonOut,L"tables[0].cells[j].row"),CkJsonObjectW_IntOf(jsonOut,L"tables[0].cells[j].col")
                ,CkJsonObjectW_stringOf(jsonOut,L"tables[0].cells[j].text"),rowSpan,colSpan);
        }

        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)


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

    }