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

Clipped, Off-Page and White-on-White Text in PDF Extraction

See more PDF Text Extraction Examples

Demonstrates the "clippedText", "offPageText" and "obscuredText" filters. Text that a reader never sees - hidden by a clipping path, outside the crop box, or drawn in the same color as the box beneath it - is excluded by default, and the quality report notes what was skipped. Each kind can be included.

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

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

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

    //  The page has a visible paragraph plus three hidden lines: one drawn outside its clipping
    //  rectangle, one below the crop box, and one in white on a white box.
    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"---- default ----\n");
    wprintf(L"%s\n",CkJsonObjectW_stringOf(jsonOut,L"text"));

    //  The quality report says what was left out.
    i = 0;
    count_i = CkJsonObjectW_SizeOfArray(jsonOut,L"quality.pages[0].issues");
    while (i < count_i) {
        CkJsonObjectW_putI(jsonOut,i);
        wprintf(L"issue: %s\n",CkJsonObjectW_stringOf(jsonOut,L"quality.pages[0].issues[i].code"));
        i = i + 1;
    }

    //  Include everything.
    CkJsonObjectW_UpdateString(json,L"clippedText",L"include");
    CkJsonObjectW_UpdateString(json,L"offPageText",L"include");
    CkJsonObjectW_UpdateString(json,L"obscuredText",L"include");
    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"---- clippedText, offPageText, obscuredText: include ----\n");
    wprintf(L"%s\n",CkJsonObjectW_stringOf(jsonOut,L"text"));

    //  Sample output:

    //  ---- default ----
    //  Visible Text
    //  
    //  This paragraph is visible. The page also carries three kinds of text a reader never sees.
    //  
    //  End of the visible text.
    //  issue: obscuredTextSkipped
    //  issue: clippedTextSkipped
    //  ---- clippedText, offPageText, obscuredText: include ----
    //  Visible Text
    //  
    //  This paragraph is visible. The page also carries three kinds of text a reader never sees.
    //  
    //  CLIPPED: this line is drawn outside its clipping rectangle.
    //  
    //  OBSCURED: this line is white on a white box.
    //  
    //  End of the visible text.
    //  
    //  OFFPAGE: this line sits below the crop box.


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

    }