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

Extract Text from a Two-Column PDF

See more PDF Text Extraction Examples

Demonstrates how multi-column pages are read. By default the columns are detected and each column is read top to bottom before the next ("columns": "auto"); "columns": "none" reads the full page width line by line, which interleaves the columns. The quality report tells how many columns were found.

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;

    success = FALSE;

    //  This example requires the Chilkat API to have been previously unlocked.
    //  See Global Unlock Sample for sample code.

    //  The two-column PDF used in this example is available at two_column_article.pdf

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

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

    //  With the default options the columns are detected, and the text of the left column
    //  comes before the text of the right column.
    success = CkPdfW_ExtractText(pdf,L"text",json,jsonOut);
    if (success == FALSE) {
        wprintf(L"%s\n",CkPdfW_lastErrorText(pdf));
        CkPdfW_Dispose(pdf);
        CkJsonObjectW_Dispose(json);
        CkJsonObjectW_Dispose(jsonOut);
        return;
    }

    wprintf(L"Columns detected: %d\n",CkJsonObjectW_IntOf(jsonOut,L"quality.pages[0].columns"));
    wprintf(L"Reading order: %s\n",CkJsonObjectW_stringOf(jsonOut,L"quality.pages[0].readingOrderSource"));
    wprintf(L"%s\n",CkJsonObjectW_stringOf(jsonOut,L"text"));

    //  "none" switches column detection off: the page is read across its full width, line by line,
    //  so each output line holds a line of the left column followed by a line of the right column.
    //  This is rarely wanted, but it shows what the detection does.
    CkJsonObjectW_UpdateString(json,L"columns",L"none");
    CkJsonObjectW_UpdateString(json,L"layout",L"lines");
    success = CkPdfW_ExtractText(pdf,L"text",json,jsonOut);
    if (success == FALSE) {
        wprintf(L"%s\n",CkPdfW_lastErrorText(pdf));
        CkPdfW_Dispose(pdf);
        CkJsonObjectW_Dispose(json);
        CkJsonObjectW_Dispose(jsonOut);
        return;
    }

    wprintf(L"---- columns: none ----\n");
    wprintf(L"%s\n",CkJsonObjectW_stringOf(jsonOut,L"text"));

    //  Sample output (abbreviated):

    //  Columns detected: 2
    //  Reading order: visual
    //  The Harbor Lighthouse, Season Two
    //  
    //  The harbor lighthouse was built in 1874 to mark the entrance to the inner channel. It operated continuously until 1968, ...
    //  
    //  The restoration society took ownership in 2021 with three goals: ...
    //  ...
    //  The lantern room frame was stripped, primed and repainted in the original dark green. ...
    //  ...
    //  ---- columns: none ----
    //  The Harbor Lighthouse, Season Two
    //  The harbor lighthouse was built in 1874 to The lantern room frame was stripped, primed
    //  mark the entrance to the inner channel. It and repainted in the original dark green. New
    //  ...


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

    }