Components All New MacOS Windows Linux iOS
Examples Mac & Win Server Client Guides Statistic FMM Blog Deprecated Old


Computes the width of a text string.

Component Version macOS Windows Linux Server iOS SDK
DynaPDF 5.0 ✅ Yes ✅ Yes ✅ Yes ✅ Yes ✅ Yes
MBS( "DynaPDF.GetTextWidth"; PDF; Text )   More


Parameter Description Example
PDF The PDF reference returned from DynaPDF.New. $pdf
Text The text to measure. "Hello World"


Returns width or error.


Computes the width of a text string.
The graphics state parameters character spacing, word spacing, text scaling, and the current font size are all considered. A font must be set before this function can be used.
The function computes the visible width of the string. This is the width excluding the last character spacing. The bounding box of the string is the text width plus the current character spacing. This behavior must be taken into account when writing right aligned text with DynaPDF.WriteFText. Due to certain formatting rules DynaPDF.WriteFText uses the bounding box of a text string to output text.

Warning: Does only work within an open page of text with a font set.

See also GetTextWidth function in DynaPDF manual.


Draw text over rectangle:

# where?
Set Variable [ $x ; Value: 100 ]
Set Variable [ $y ; Value: 100 ]
# the text
Set Variable [ $text ; Value: "Some blue text" ]
# set the font before measuring
Set Variable [ $r ; Value: MBS("DynaPDF.SetFont"; $pdf; "Helvetica"; 0; 12) ]
# measure now
Set Variable [ $width ; Value: MBS( "DynaPDF.GetTextWidth"; $pdf; $text ) ]
# draw rectangle width some addition 5 point margin
Set Variable [ $r ; Value: MBS("DynaPDF.SetFillColor"; $pdf; ,8; ,8; ,8) // light gray ]
Set Variable [ $r ; Value: MBS( "DynaPDF.Rectangle"; $pdf; $x - 5; $y - 5 - 2; $width + 10; 5 + 12 + 5; "fill") ]
# and draw text on top
Set Variable [ $r ; Value: MBS("DynaPDF.SetFillColor"; $pdf; 0; 0; 1) // 100% blue ]
Set Variable [ $r ; Value: MBS("DynaPDF.WriteText"; $pdf; 100; $x; $text) ]

See also

Blog Entries

This function checks for a license.

Created 12nd November 2014, last changed 1st May 2023

DynaPDF.GetTextScaling - DynaPDF.GetTransparentColor