How to Work with Text in TypeScript
This guide shows how to place and extract text in PDF documents with
Aspose.PDF FOSS for TypeScript. Page.AddTextBlock() places styled,
word-wrapped, and right-to-left text inside a bounding rectangle;
Page.GetText() and Page.GetStructuredText() read text back out, either
as a plain string or as position-aware blocks. It requires Node.js 22 or
later.
Step-by-Step Guide
Step 1: Install the Package
asposefoss/pdf is not yet published — build from source until it ships. See the project README for build instructions.
Verify the installation by importing the Document class in a new
TypeScript file — this line should resolve without error once the package
is installed:
import { Document } from '@asposefoss/pdf';Step 2: Import Required Classes
Import Document to open the file; AddTextBlock() and the read-back
methods used below are called directly on the Page instances it returns:
import { Document } from '@asposefoss/pdf';Step 3: Add a Formatted Text Block
Page.AddTextBlock(text, rect, options) wraps text inside rect,
handling word wrapping, alignment, and glyph-width measurement. options
covers font, fontSize, color, align, leading (line spacing),
underline, strikethrough, background, and opacity:
const doc = Document.OpenFile('input.pdf');
const page = doc.Pages[0];
page.AddTextBlock(
'This paragraph demonstrates automatic word wrapping at the right edge '
+ 'of the bounding rectangle. Words break on whitespace.',
[60, 400, 480, 100],
{ font: 'Times-Roman', fontSize: 11, color: [0.1, 0.1, 0.1], align: 'left', leading: 15.4 },
);
page.AddTextBlock('Underlined and struck-through text.', [60, 640, 300, 20],
{ fontSize: 11, underline: true });When the text overflows rect, AddTextBlock() returns the remaining
text (or null if everything fit) — pass that remainder into a second
call to continue the paragraph into another region:
const rest = page.AddTextBlock(longText, [72, 600, 200, 150], {
font: 'Times-Roman', fontSize: 11, align: 'justify', valign: 'top', leading: 14,
});
if (rest) page.AddTextBlock(rest, [300, 600, 200, 150]); // continue into a 2nd column
Step 4: Add Right-to-Left and Embedded-Font Text
Pass an EmbeddedFont (from Document.AddFont() / Document.AddFontFile())
as font to draw Unicode text outside the 14 standard PDF fonts. Set
shape: true and dir: 'rtl' to drive automatic BiDi and Arabic/Hebrew
glyph shaping:
page.AddTextBlock('Русский: Здравствуй, мир!', [60, 300, 480, 16],
{ font: embeddedFont, fontSize: 11 });
page.AddTextBlock('שלום עולם', [60, 280, 435, 16],
{ font: embeddedFont, fontSize: 12, shape: true, dir: 'rtl', align: 'right' });Step 5: Extract Plain Text
Page.GetText() returns the page’s full text content as a single string —
useful for search-and-verify checks or lightweight text extraction:
const doc2 = Document.OpenFile('signed.pdf');
const pageText = doc2.Pages[0].GetText();
console.log(pageText.includes('Internal memo'));Step 6: Get Position-Aware Structured Text
Page.GetStructuredText() returns an array of TextBlock entries, each
carrying its own text and a quad describing where it sits on the page —
useful for driving hand-authored tagging (see the
Structure
guide) or any workflow that needs to know where a run of text is located:
for (const block of page.GetStructuredText()) {
const text = block.text.trim();
if (text.length === 0) continue;
console.log(text, block.quad);
}Common Issues and Fixes
Page.AddTextBlock() clips text unexpectedly. Text is clipped to the
bounding rect — check the returned remainder value (string | TextRun[] | null) and route it into a further AddTextBlock() call, as shown in
Step 3, instead of assuming everything fit.
Right-to-left text renders in the wrong order. shape: true and
dir: 'rtl' must both be set — dir alone selects alignment, while
shape drives the BiDi reordering and Arabic/Hebrew glyph joining.
Page.GetText() returns text in a surprising order. It concatenates
the page’s text content; for a version that preserves per-run position and
grouping, use Page.GetStructuredText() instead.
A non-standard font renders as boxes or the wrong glyphs. The 14
standard PDF fonts (Helvetica, Times-Roman, Courier, and their
bold/oblique variants) do not need embedding, but any other typeface must
be loaded first through Document.AddFont() / Document.AddFontFile()
and passed as the font option, not referenced by a bare string name.
Frequently Asked Questions
Does AddTextBlock() support multiple text runs with different styles in one call?
Yes — passing an array of TextRun-shaped objects instead of a plain
string lets each run carry its own style within the same call.
Can I measure how much space a run of text will need before drawing it?
Page exposes a text-measurement method (MeasureText()) for this
purpose, separate from the drawing methods covered here.
Is GetText() the same as what a PDF viewer’s text-selection shows?
GetText() reads the page’s underlying text content in the order it
appears in the content stream, which usually — but not always — matches
visual reading order; GetStructuredText() provides the per-block
position data needed to reconstruct reading order explicitly.
Do word-wrapped paragraphs respect font metrics for different typefaces?
Yes — AddTextBlock() measures wrapping against the actual glyph widths
of the font in use, whether a standard font or an EmbeddedFont.