Delphi DLL Requires Chilkat v11.7.0+
Delphi DLL
Extract One Section of a PDF by its Heading
See more PDF Text Extraction Examples
Demonstrates "labelValue": "section", which returns everything from a heading up to the next heading of the same or higher level. With the "markdown" operation the section keeps its own sub-headings.Chilkat Delphi DLL Downloads
var
success: Boolean;
pdf: HCkPdf;
json: HCkJsonObject;
jsonOut: HCkJsonObject;
begin
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 sectioned_doc.pdf
pdf := CkPdf_Create();
success := CkPdf_LoadFile(pdf,'qa_data/pdf/sectioned_doc.pdf');
if (success = False) then
begin
Memo1.Lines.Add(CkPdf__lastErrorText(pdf));
Exit;
end;
json := CkJsonObject_Create();
jsonOut := CkJsonObject_Create();
// The document has the sections Introduction, Opening Hours, Terms and Conditions (with the
// sub-sections Admission, Safety and Liability) and Contact. Get the Terms and Conditions:
// everything after that heading up to the next heading of the same level (Contact). The heading
// itself is not part of the value; labelMatches[0].label has it.
CkJsonObject_UpdateString(json,'label','Terms and Conditions');
CkJsonObject_UpdateString(json,'labelValue','section');
success := CkPdf_ExtractText(pdf,'markdown',json,jsonOut);
if (success = False) then
begin
Memo1.Lines.Add(CkPdf__lastErrorText(pdf));
Exit;
end;
Memo1.Lines.Add(CkJsonObject__stringOf(jsonOut,'markdown'));
// The same with the "text" operation gives the section as plain text.
success := CkPdf_ExtractText(pdf,'text',json,jsonOut);
if (success = False) then
begin
Memo1.Lines.Add(CkPdf__lastErrorText(pdf));
Exit;
end;
Memo1.Lines.Add('---- as text ----');
Memo1.Lines.Add(CkJsonObject__stringOf(jsonOut,'text'));
// Sample output:
// ### Admission
//
// Admission is free. Groups of more than ten should book in advance so that a volunteer can be assigned to them.
//
// ### Safety
//
// No more than eight people may be on the stair at one time. Children under twelve must be accompanied by an adult, and the lantern room is closed in high winds.
//
// ### Liability
//
// Visitors climb the tower at their own risk. The society carries public liability insurance but accepts no responsibility for personal property.
// ---- as text ----
// Admission
//
// Admission is free. Groups of more than ten should book in advance so that a volunteer can be assigned to them.
//
// Safety
//
// No more than eight people may be on the stair at one time. Children under twelve must be accompanied by an adult, and the lantern room is closed in high winds.
//
// Liability
//
// Visitors climb the tower at their own risk. The society carries public liability insurance but accepts no responsibility for personal property.
CkPdf_Dispose(pdf);
CkJsonObject_Dispose(json);
CkJsonObject_Dispose(jsonOut);