Text

Text

This page covers reading text back out of a page, at three levels of detail: plain text, structured layout blocks, and individual positioned glyphs. For writing text onto a page, see Core API and Core API.


Extracting Plain Text

Page.GetText() returns the page’s assembled plain text, in reading order. It is the simplest way to confirm what text a page actually contains — for example, after flattening a field or applying a redaction.

const page = doc.Pages[0];
console.log(page.GetText().includes('Internal memo'));

Structured Text

Page.GetStructuredText() extracts positioned text grouped into lines (by baseline) and paragraph-like blocks (by vertical gap and left-edge alignment), returning TextBlock[] — useful when you need layout, not just a flat string.


Glyph-Level Introspection

visitContent(doc, page, visitor) walks a page’s content streams — including into Form XObjects — and calls the matching handler on visitor for every glyph, image, or path it encounters. The glyph handler receives a GlyphEvent: the glyph’s text, its device-space quad, fontSize, color, and provenance fields such as mcid (its enclosing marked-content element, if any) and artifact (true when drawn inside an /Artifact scope).

import { visitContent } from '@asposefoss/pdf';

let glyphCount = 0;
visitContent(doc, page, {
  glyph: (e) => {
    glyphCount++;
    if (e.artifact) return;       // skip decorative content
    console.log(e.text, e.quad, e.color);
  },
});
console.log('glyphs drawn:', glyphCount);

Tips and Best Practices

  • Use Page.GetText() for a quick presence/absence check; use visitContent() with a glyph handler when you need each glyph’s exact position, font size, or color.
  • Check GlyphEvent.artifact to skip decorative content (page furniture, backgrounds) that a tagged document marks as /Artifact rather than real content.
  • GlyphEvent.mcid links a glyph back to its enclosing structure element when the document is tagged — combine with Structure to walk from a glyph to its logical element.
  • Page.GetStructuredText() groups glyphs into blocks for you; reach for visitContent() directly only when you need per-glyph detail GetStructuredText() does not expose.

Common Issues

IssueCauseFix
Page.GetText() is missing text that is visibly on the pageThe text was drawn as vector paths or an image rather than real glyph-show operatorsUse visitContent() with a path / image handler to confirm what content type actually drew it
visitContent()’s glyph handler fires for decorative background textGlyphEvent.artifact was not checkedSkip events where e.artifact is true
Extracted text is out of visual reading orderPlain GetText() assembly does not reconstruct multi-column layoutUse Page.GetStructuredText(), which groups text into layout-aware blocks

FAQ

What is the difference between GetText and GetStructuredText?

GetText() returns one flat string of the page’s assembled text. GetStructuredText() returns TextBlock[] — text grouped into lines and paragraph-like blocks by baseline and layout, useful when position matters.

How do I get the exact position and font size of each character?

Call visitContent(doc, page, { glyph: (e) => ... }). Each GlyphEvent carries the glyph’s text, device-space quad, fontSize, color, and baseline angle.

Can I tell whether a piece of text is real content or decoration?

Yes — GlyphEvent.artifact is true when the glyph was drawn inside an /Artifact marked-content scope, the same distinction Document.AutoTag() uses when deciding what to tag.

Does text extraction see content inside Form XObjects?

Yes — visitContent() walks into Form XObjects as part of its traversal, so glyphs drawn through a reused form are still visited.


API Reference Summary

Class/MethodDescription
Page.GetText()The page’s assembled plain text, in reading order
Page.GetStructuredText()Text grouped into lines and paragraph-like blocks (TextBlock[])
visitContent()Walk a page’s content, emitting glyph/image/path events with provenance
GlyphEventOne positioned glyph: text, quad, fontSize, color, mcid, artifact

See Also