Lazarus Pascal Requires Chilkat v11.7.0+
Lazarus Pascal
Code Blocks and Code Spans in PDF to Markdown
See more PDF Text Extraction Examples
Demonstrates how monospaced text is converted by the "markdown" operation: a monospaced block becomes a fenced code block and a monospaced run inside a sentence becomes a code span. Set "code" to false to treat it as ordinary text.Chilkat Lazarus Pascal Downloads
program ChilkatDemo;
// Demonstrates using the Chilkat Pascal wrapper via the C bridge DLL.
// Builds as a console application under Lazarus (FPC) or Delphi.
{$IFDEF FPC}
{$MODE DELPHI}
{$ENDIF}
{$APPTYPE CONSOLE}
uses
{$IFDEF UNIX}
cthreads,
{$ENDIF}
SysUtils,
CkDllLoader,
Chilkat.Pdf,
Chilkat.JsonObject;
// ---------------------------------------------------------------------------
procedure RunDemo;
var
success: Boolean;
pdf: TPdf;
json: TJsonObject;
jsonOut: TJsonObject;
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 code_block.pdf
pdf := TPdf.Create;
success := pdf.LoadFile('qa_data/pdf/code_block.pdf');
if (success = False) then
begin
WriteLn(pdf.LastErrorText);
Exit;
end;
json := TJsonObject.Create;
jsonOut := TJsonObject.Create;
success := pdf.ExtractText('markdown',json,jsonOut);
if (success = False) then
begin
WriteLn(pdf.LastErrorText);
Exit;
end;
WriteLn('---- default (code: true) ----');
WriteLn(jsonOut.StringOf('markdown'));
json.UpdateBool('code',False);
success := pdf.ExtractText('markdown',json,jsonOut);
if (success = False) then
begin
WriteLn(pdf.LastErrorText);
Exit;
end;
WriteLn('---- code: false ----');
WriteLn(jsonOut.StringOf('markdown'));
// Sample output:
// ---- default (code: true) ----
// <!-- page 1 -->
//
// # Extracting Text
//
// Load the PDF and call `ExtractText` with the operation and an options object. The result is returned as JSON:
//
// ```
// new Pdf pdf;
// success = pdf.LoadFile("report.pdf");
// new JsonObject json;
// new JsonObject jsonOut;
// success = pdf.ExtractText("text",json,jsonOut);
// println jsonOut.StringOf("text");
// ```
//
// An empty options object extracts every page with the default clean-up. Set `pages` to limit the extraction.
// ---- code: false ----
// <!-- page 1 -->
//
// # Extracting Text
//
// Load the PDF and call ExtractText with the operation and an options object. The result is returned as JSON:
//
// new Pdf pdf; success = pdf.LoadFile("report.pdf"); new JsonObject json; new JsonObject jsonOut; success = pdf.ExtractText("text",json,jsonOut); println jsonOut.StringOf("text");
//
// An empty options object extracts every page with the default clean-up. Set pages to limit the extraction.
pdf.Free;
json.Free;
jsonOut.Free;
end;
// ---------------------------------------------------------------------------
begin
try
RunDemo;
except
on E: Exception do
WriteLn('Unhandled exception: ', E.ClassName, ': ', E.Message);
end;
WriteLn;
{$IFDEF MSWINDOWS}
WriteLn('Press Enter to exit...');
ReadLn;
{$ENDIF}
end.