PdfTextExtractor class

Extracts positioned text from a list of content stream operators.

What is tracked

BT/ET, Tf, Td, TD, Tm, T*, Tj, TJ, ', ", Tc, Tw, Tz, TL, Ts, Tr, plus the current transformation matrix through cm, q and Q. Optionally descends into the form XObjects of Do, applying the form's /Matrix.

Where it gets things wrong, spelled out

  • subsetted Type0 font without /ToUnicode: the codes are internal glyph indices and there is no way to recover the text. The characters come out as � and the item carries PdfTextItem.fullyMapped as false. Reading the cmap table of the embedded font would solve part of this and is out of scope here;
  • approximate rectangle: the height is estimated and does not come from the bbox of the glyphs; the width depends on /Widths and /W. Without them, the extractor settles on 500/1000 em per glyph;
  • Type3: the font's own /FontMatrix is ignored, so position and width come out wrong;
  • vertical writing (/Identity-V, /WMode 1): treated as horizontal;
  • reading order: the items come out in stream order, which is not necessarily the visual order. plainText applies a simple heuristic, not a column segmentation;
  • text in annotations (/AP) is not part of the page content and is not visited, unless the caller passes the appearance stream.

What is left out by decision

Text replacement is not implemented. Rewriting Tj/TJ safely requires re-encoding the text in the target font, checking whether the embedded subset has the glyphs, recomputing advances and, when it does not, embedding or swapping the font — the work that in practice only MuPDF does in open source. Covering the reading half without promising the writing half is deliberate: a replacement that sometimes corrupts the stream or changes the drawing would be worse than none at all. See doc/roteiro_edicao_pdf.md, phase F8.

Constructors

PdfTextExtractor({PdfContentResources? resources, PdfTextExtractionOptions options = const PdfTextExtractionOptions()})
Creates an extractor.

Properties

hashCode → int
The hash code for this object.
no setterinherited
options → PdfTextExtractionOptions
Options.
final
resources → PdfContentResources
Resources used to resolve fonts and form XObjects.
final
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited

Methods

extract(List<PdfContentOperator> operators) → List<PdfTextItem>
Extracts the text of operators.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() → String
A string representation of this object.
inherited

Operators

operator ==(Object other) → bool
The equality operator.
inherited

Static Methods

extractPage(PdfDocumentParser parser, int pageIndex, {PdfTextExtractionOptions options = const PdfTextExtractionOptions()}) → List<PdfTextItem>
Extracts the text of a page from a document opened by the parser.
plainText(List<PdfTextItem> items) → String
Joins the items into running text, inserting breaks when the baseline changes and spaces when there is a horizontal gap.