API referenceText

pdfExtractText

Extract the visible text of one page, either from the document

CategoryText extraction
NoteFlags is accepted but unused — see Remarks

Purpose — Extract the visible text of one page, either from the document currently being built or from an open import source, as a single Unicode string with best-effort reading-order assembly.

Description — dual-mode, real Unicode-aware decoding. Two independent code paths, chosen automatically:

  • Output-side (GetPageContentBytes succeeds — PageNum refers to a page of the document you're building): decodes each text-showing run through DecodeGIDString, the same font tables the engine used to write the glyphs — always accurate for output the engine itself produced.
  • Import-side (falls back to GetImportPageContentDecoded when the page isn't one of the output document's own): a considerably richer decoder — resolves each font's /ToUnicode CMap when present; for CJK Type0 fonts without /ToUnicode, resolves the encoding CMap (or treats codes as Identity 2-byte CIDs) and maps CID→Unicode via the embedded Adobe Adobe-<Ordering>-UCS2 CMaps (GB1/CNS1/Japan1/Korea1); for simple fonts with /Encoding /Differences and no /ToUnicode, maps overridden codes via glyph name → Adobe Glyph List, falling back to WinAnsi for non-overridden codes; recurses into Form XObjects (Do) so text inside them is included. When the imported page has a /Rotate of 90/180/270, extracted run coordinates are corrected to match the visual (rotated) orientation before area filtering, so Area and the resulting reading order are consistent with what a viewer displays.

Runs are assembled into text via RunsToText, which inserts spaces based on the horizontal-gap threshold set by pdfSetTextExtractionWordGap and a fixed line-break heuristic (vertical gap 0.5).

Declarations

C
BOOL32 __stdcall pdfExtractText(PPDF IPDF, uint32_t PageNum, TTextExtractionFlags Flags, PFltRect Area, const wchar_t* Text, uint32_t* TextLen);
Delphi
function pdfExtractText(const IPDF: PPDF; PageNum: Cardinal; Flags: TTextExtractionFlags; const Area: PFltRect; var Text: PWideChar; var TextLen: Cardinal): LongBool; stdcall; external 'LumasPdf.dll';

TTextExtractionFlags (from Lumas.Pdf.Types, bit values; accepted for ABI shape but not read anywhere in this call's implementation — see Remarks):

Delphi
TTextExtractionFlags = Integer;
const tefDefault = 0; tefSortTextX = 1; tefSortTextY = 2; tefSortTextXY = 3;
      tefDeleteOverlappingText = 4; tefNoHeuristic = 8;

TFltRect:

Delphi
TFltRect = record Left, Bottom, Right, Top: Single; end;

Parameters

ParameterTypeDescription
IPDFPPDFInstance handle.
PageNumCardinal1-based page number.
FlagsTTextExtractionFlagsAccepted for ABI compatibility; has no effect — see Remarks.
AreaPFltRectOptional filter rectangle, or NULL for the whole page. Runs are kept only if their origin point (X, Y) falls inside [Left, Right] × [Bottom, Top] — this is a point-containment test on the run's start, not a bounding-box intersection, so a run that starts just outside Area but visually overlaps it is entirely excluded, and a run that starts inside but extends far past the edge is entirely kept.
TextPWideChar*Receives the extracted text.
TextLenCardinal*Receives the character count.

Return valueTRUE on success; FALSE if the handle is invalid or PageNum resolves to no content on either the output or import side.

Memory & buffersText points at an engine-owned buffer (ExtractTextBufPtr) valid until the next text-extraction call on this instance.

Remarks — compatibility parameters. Flags is accepted to match the reference ABI shape but is not consulted anywhere in this function's implementation — sort order (tefSortTextX/Y/XY), overlap deletion, and heuristic suppression are not selectable through this call regardless of what you pass. pdfSplitPageText and pdfGetTextBlockAsString are both thin wrappers over this function (with Flags=0, Area=nil), splitting the result on #10 — so "blocks"/"split lines" from those two calls are exactly the same reading-order lines this function produces, not a separate layout/paragraph analysis.

See alsopdfSplitPageText, pdfGetTextBlockAsString, pdfSetTextExtractionWordGap

C# (P/Invoke)

wrappers/dotnet/LumasPdf.cs
[return: MarshalAs(UnmanagedType.Bool)]
Area
Text
Category

Core

Exported names

pdfExtractText

String variants

The …A form takes UTF-8, …W takes UTF-16; a bare name aliases the ANSI form.

See working code

Worked examples — complete programs in ten languages.