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

Exclude or Include Running Headers and Page Numbers

See more PDF Text Extraction Examples

Demonstrates the "repeatedHeadersFooters" filter. Text repeated at the same position on most pages, such as a running header and "Page N of 4" footers, is left out by default; set the filter to "include" to keep it.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoPdf
    Variant vJson
    Handle hoJson
    Variant vJsonOut
    Handle hoJsonOut
    String sTemp1

    Move False To iSuccess

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

    //  The four-page PDF used in this example is available at running_header.pdf

    Get Create (RefClass(cComChilkatPdf)) To hoPdf
    If (Not(IsComObjectCreated(hoPdf))) Begin
        Send CreateComObject of hoPdf
    End
    Get ComLoadFile Of hoPdf "qa_data/pdf/running_header.pdf" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoPdf To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatJsonObject)) To hoJson
    If (Not(IsComObjectCreated(hoJson))) Begin
        Send CreateComObject of hoJson
    End
    Get Create (RefClass(cComChilkatJsonObject)) To hoJsonOut
    If (Not(IsComObjectCreated(hoJsonOut))) Begin
        Send CreateComObject of hoJsonOut
    End

    //  Every page has the header "Harbor Lighthouse Society - Annual Report 2026" at the top right
    //  and "Page N of 4" at the bottom.  By default both are excluded, so the text of page 2 is just
    //  its heading and paragraphs.
    Get ComUpdateString Of hoJson "pages" "2" To iSuccess
    Get pvComObject of hoJson to vJson
    Get pvComObject of hoJsonOut to vJsonOut
    Get ComExtractText Of hoPdf "text" vJson vJsonOut To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoPdf To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Showln "---- default (repeated headers and footers excluded) ----"
    Get ComStringOf Of hoJsonOut "text" To sTemp1
    Showln sTemp1

    //  Include them.  The detection looks at the neighbouring pages even when only one page is
    //  selected, so a single page extracted on its own still loses its running head by default.
    Get ComUpdateString Of hoJson "repeatedHeadersFooters" "include" To iSuccess
    Get pvComObject of hoJson to vJson
    Get pvComObject of hoJsonOut to vJsonOut
    Get ComExtractText Of hoPdf "text" vJson vJsonOut To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoPdf To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Showln "---- repeatedHeadersFooters: include ----"
    Get ComStringOf Of hoJsonOut "text" To sTemp1
    Showln sTemp1

    //  Sample output:

    //  ---- default (repeated headers and footers excluded) ----
    //  The Work
    //  
    //  The restoration society took ownership in 2021 with three goals: make the structure watertight, return the tower to public access, and relight the lantern with a historically accurate lens. The first two were met in the second season, and the lens arrived on loan in November.
    //  
    //  The spiral stair is original and is the most fragile part of the building. Twelve of the ninety-one treads were cracked or missing, and the central column had corroded badly at the second landing. The treads were cast from patterns taken off an intact tread.
    //  ---- repeatedHeadersFooters: include ----
    //  Harbor Lighthouse Society - Annual Report 2026
    //  
    //  The Work
    //  
    //  The restoration society took ownership in 2021 with three goals: ...
    //  
    //  The spiral stair is original and is the most fragile part of the building. ...
    //  
    //  Page 2 of 4


End_Procedure