Unicode C Requires Chilkat v11.7.0+
Unicode C
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 Unicode C Downloads
#include <C_CkPdfW.h>
#include <C_CkJsonObjectW.h>
void ChilkatSample(void)
{
BOOL success;
HCkPdfW pdf;
HCkJsonObjectW json;
HCkJsonObjectW jsonOut;
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 ligatures.pdf
pdf = CkPdfW_Create();
success = CkPdfW_LoadFile(pdf,L"qa_data/pdf/ligatures.pdf");
if (success == FALSE) {
wprintf(L"%s\n",CkPdfW_lastErrorText(pdf));
CkPdfW_Dispose(pdf);
return;
}
json = CkJsonObjectW_Create();
jsonOut = CkJsonObjectW_Create();
// The first line is set with the fi and fl ligature glyphs; the second has circled digits,
// a trademark sign and a vulgar fraction.
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 (expandLigatures: true, normalize: NFC) ----\n");
wprintf(L"%s\n",CkJsonObjectW_stringOf(jsonOut,L"text"));
// Keep the ligature characters as they are in the PDF.
CkJsonObjectW_UpdateBool(json,L"expandLigatures",FALSE);
CkJsonObjectW_UpdateString(json,L"normalize",L"none");
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"---- expandLigatures: false, normalize: none ----\n");
wprintf(L"%s\n",CkJsonObjectW_stringOf(jsonOut,L"text"));
// NFKC folds the compatibility characters too (and expands ligatures on its own).
CkJsonObjectW_UpdateString(json,L"normalize",L"NFKC");
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"---- normalize: NFKC ----\n");
wprintf(L"%s\n",CkJsonObjectW_stringOf(jsonOut,L"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.
CkPdfW_Dispose(pdf);
CkJsonObjectW_Dispose(json);
CkJsonObjectW_Dispose(jsonOut);
}