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

SQL Server
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @success int
    SELECT @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

    DECLARE @pdf int
    EXEC @hr = sp_OACreate 'Chilkat.Pdf', @pdf OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    EXEC sp_OAMethod @pdf, 'LoadFile', @success OUT, 'qa_data/pdf/ligatures.pdf'
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @pdf, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @pdf
        RETURN
      END

    DECLARE @json int
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @json OUT

    DECLARE @jsonOut int
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @jsonOut OUT

    --  The first line is set with the fi and fl ligature glyphs; the second has circled digits,
    --  a trademark sign and a vulgar fraction.
    EXEC sp_OAMethod @pdf, 'ExtractText', @success OUT, 'text', @json, @jsonOut
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @pdf, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @pdf
        EXEC @hr = sp_OADestroy @json
        EXEC @hr = sp_OADestroy @jsonOut
        RETURN
      END

    PRINT '---- default (expandLigatures: true, normalize: NFC) ----'
    EXEC sp_OAMethod @jsonOut, 'StringOf', @sTmp0 OUT, 'text'
    PRINT @sTmp0

    --  Keep the ligature characters as they are in the PDF.
    EXEC sp_OAMethod @json, 'UpdateBool', @success OUT, 'expandLigatures', 0
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'normalize', 'none'
    EXEC sp_OAMethod @pdf, 'ExtractText', @success OUT, 'text', @json, @jsonOut
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @pdf, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @pdf
        EXEC @hr = sp_OADestroy @json
        EXEC @hr = sp_OADestroy @jsonOut
        RETURN
      END

    PRINT '---- expandLigatures: false, normalize: none ----'
    EXEC sp_OAMethod @jsonOut, 'StringOf', @sTmp0 OUT, 'text'
    PRINT @sTmp0

    --  NFKC folds the compatibility characters too (and expands ligatures on its own).
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'normalize', 'NFKC'
    EXEC sp_OAMethod @pdf, 'ExtractText', @success OUT, 'text', @json, @jsonOut
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @pdf, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @pdf
        EXEC @hr = sp_OADestroy @json
        EXEC @hr = sp_OADestroy @jsonOut
        RETURN
      END

    PRINT '---- normalize: NFKC ----'
    EXEC sp_OAMethod @jsonOut, 'StringOf', @sTmp0 OUT, 'text'
    PRINT @sTmp0

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

    EXEC @hr = sp_OADestroy @pdf
    EXEC @hr = sp_OADestroy @json
    EXEC @hr = sp_OADestroy @jsonOut


END
GO