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

Expand Ligatures and Normalize Unicode in Extracted PDF Text

See more PDF Text Extraction Examples

Demonstrates the "expandLigatures" and "normalize" clean-up options. Typeset PDFs use single glyphs for "fi" and "fl"; by default they are expanded to their letters. Unicode normalization is NFC by default; NFKC also folds compatibility characters such as circled digits, the trademark sign and vulgar fractions into plain characters, and "none" leaves the text as the PDF has 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 PDF used in this example is available at ligatures.pdf

    Get Create (RefClass(cComChilkatPdf)) To hoPdf
    If (Not(IsComObjectCreated(hoPdf))) Begin
        Send CreateComObject of hoPdf
    End
    Get ComLoadFile Of hoPdf "qa_data/pdf/ligatures.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

    //  The first line is set with the fi and fl ligature glyphs; the second has circled digits,
    //  a trademark sign and a vulgar fraction.
    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 (expandLigatures: true, normalize: NFC) ----"
    Get ComStringOf Of hoJsonOut "text" To sTemp1
    Showln sTemp1

    //  Keep the ligature characters as they are in the PDF.
    Get ComUpdateBool Of hoJson "expandLigatures" False To iSuccess
    Get ComUpdateString Of hoJson "normalize" "none" 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 "---- expandLigatures: false, normalize: none ----"
    Get ComStringOf Of hoJsonOut "text" To sTemp1
    Showln sTemp1

    //  NFKC folds the compatibility characters too (and expands ligatures on its own).
    Get ComUpdateString Of hoJson "normalize" "NFKC" 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 "---- normalize: NFKC ----"
    Get ComStringOf Of hoJsonOut "text" To sTemp1
    Showln sTemp1

    //  Sample output (shown here in ASCII: (fi) and (fl) stand for the single ligature characters, (1) and (2) for the circled digits,
    //  (TM) for the trademark sign and 1/2 for the vulgar fraction; the NFKC output has plain "1", "2", "TM" and "1", fraction slash, "2"):

    //  ---- default (expandLigatures: true, normalize: NFC) ----
    //  Typeset Text
    //  
    //  The final flight of the season was officially confirmed on the fifth. Rooms (1) and (2) are reserved; the Harbor Cafe(TM) serves 1/2 portions.
    //  ---- expandLigatures: false, normalize: none ----
    //  Typeset Text
    //  
    //  The (fi)nal (fl)ight of the season was of(fi)cially con(fi)rmed on the (fi)fth. Rooms (1) and (2) are reserved; the Harbor Cafe(TM) serves 1/2 portions.
    //  ---- normalize: NFKC ----
    //  Typeset Text
    //  
    //  The final flight of the season was officially confirmed on the fifth. Rooms 1 and 2 are reserved; the Harbor CafeTM serves 1/2 portions.


End_Procedure