Unicode C Requires Chilkat v11.7.0+
Unicode C
Get the Text Block Below a PDF Caption
See more PDF Text Extraction Examples
Demonstrates the "label" option with labelValue "below", which captures the block of text directly under a caption such as "Ship To", ending at the next gap or heading. Combined with layout "lines", the lines of an address block are kept separate.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 packing slip used in this example is available at packing_slip.pdf
pdf = CkPdfW_Create();
success = CkPdfW_LoadFile(pdf,L"qa_data/pdf/packing_slip.pdf");
if (success == FALSE) {
wprintf(L"%s\n",CkPdfW_lastErrorText(pdf));
CkPdfW_Dispose(pdf);
return;
}
json = CkJsonObjectW_Create();
jsonOut = CkJsonObjectW_Create();
// The packing slip has the captions "Bill To" and "Ship To" side by side,
// each with an address block below it.
CkJsonObjectW_UpdateString(json,L"label",L"Ship To");
// "below" means the value is the block of text directly under the label.
CkJsonObjectW_UpdateString(json,L"labelValue",L"below");
// With the default "flow" layout the lines of the block would be joined into one paragraph.
// "lines" keeps each line of the address on its own line.
CkJsonObjectW_UpdateString(json,L"layout",L"lines");
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"Ship To:\n");
wprintf(L"%s\n",CkJsonObjectW_stringOf(jsonOut,L"text"));
wprintf(L"\n");
CkJsonObjectW_UpdateString(json,L"label",L"Bill To");
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"Bill To:\n");
wprintf(L"%s\n",CkJsonObjectW_stringOf(jsonOut,L"text"));
wprintf(L"\n");
// A caption over a short paragraph works the same way. Here the flow layout is better,
// because the two lines are one piece of text.
CkJsonObjectW_UpdateString(json,L"label",L"Special Instructions");
CkJsonObjectW_UpdateString(json,L"layout",L"flow");
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"Special Instructions:\n");
wprintf(L"%s\n",CkJsonObjectW_stringOf(jsonOut,L"text"));
// Sample output:
// Ship To:
// Frank Delgado
// c/o Harbor Pet Boarding
// 9 Ferry Lane
// Portside, WA 98001
//
// Bill To:
// Frank Delgado
// Apartment 3B
// 41 Mill Street
// Portside, WA 98000
//
// Special Instructions:
// Leave the carton with the boarding office if no one answers. Do not leave it outside; the cat tree is not packed for rain.
CkPdfW_Dispose(pdf);
CkJsonObjectW_Dispose(json);
CkJsonObjectW_Dispose(jsonOut);
}