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

PowerBuilder
integer li_rc
long li_Success
oleobject loo_Pdf
oleobject loo_Json
oleobject loo_JsonOut

li_Success = 0

//  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

loo_Pdf = create oleobject
li_rc = loo_Pdf.ConnectToNewObject("Chilkat.Pdf")
if li_rc < 0 then
    destroy loo_Pdf
    MessageBox("Error","Connecting to COM object failed")
    return
end if
li_Success = loo_Pdf.LoadFile("qa_data/pdf/ligatures.pdf")
if li_Success = 0 then
    Write-Debug loo_Pdf.LastErrorText
    destroy loo_Pdf
    return
end if

loo_Json = create oleobject
li_rc = loo_Json.ConnectToNewObject("Chilkat.JsonObject")

loo_JsonOut = create oleobject
li_rc = loo_JsonOut.ConnectToNewObject("Chilkat.JsonObject")

//  The first line is set with the fi and fl ligature glyphs; the second has circled digits,
//  a trademark sign and a vulgar fraction.
li_Success = loo_Pdf.ExtractText("text",loo_Json,loo_JsonOut)
if li_Success = 0 then
    Write-Debug loo_Pdf.LastErrorText
    destroy loo_Pdf
    destroy loo_Json
    destroy loo_JsonOut
    return
end if

Write-Debug "---- default (expandLigatures: true, normalize: NFC) ----"
Write-Debug loo_JsonOut.StringOf("text")

//  Keep the ligature characters as they are in the PDF.
loo_Json.UpdateBool("expandLigatures",0)
loo_Json.UpdateString("normalize","none")
li_Success = loo_Pdf.ExtractText("text",loo_Json,loo_JsonOut)
if li_Success = 0 then
    Write-Debug loo_Pdf.LastErrorText
    destroy loo_Pdf
    destroy loo_Json
    destroy loo_JsonOut
    return
end if

Write-Debug "---- expandLigatures: false, normalize: none ----"
Write-Debug loo_JsonOut.StringOf("text")

//  NFKC folds the compatibility characters too (and expands ligatures on its own).
loo_Json.UpdateString("normalize","NFKC")
li_Success = loo_Pdf.ExtractText("text",loo_Json,loo_JsonOut)
if li_Success = 0 then
    Write-Debug loo_Pdf.LastErrorText
    destroy loo_Pdf
    destroy loo_Json
    destroy loo_JsonOut
    return
end if

Write-Debug "---- normalize: NFKC ----"
Write-Debug loo_JsonOut.StringOf("text")

//  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.


destroy loo_Pdf
destroy loo_Json
destroy loo_JsonOut