API referenceText

pdfGetTextWidth

Measure the width, in points, of AText at the current

CategoryText measurement

Purpose — Measure the width, in points, of AText at the current font/size/spacing.

Description — Dispatches by font kind for the base glyph-advance sum: TrueType uses real font-metric widths (CalcTextWidthTT); standard-14 fonts (and Type 1 without their own embedded widths) use the built-in AFM width tables (Std14TextWidth1000); any other font kind (CID-predefined) falls back to a coarse char-count × mean-width estimate (GetTextWidth, character-count based) — this is the one case where the returned width is not glyph-accurate. On top of the base sum, the engine correctly adds:

  • Character spacing (Tc, set via pdfSetCharSpacing): `Tc × (charCount
  • 1) for charCount > 1`.
  • Word spacing (Tw, set via pdfSetWordSpacing): Tw × spaceCount.
  • Per-character width overrides set via pdfSetCharWidth.

Worked example: Helvetica "Hello World" @ 12pt with Tc=1.25/Tw=3.562.004 + 10×1.25 + 1×3.5 = 78.004 pt.

Declarations

C
double __stdcall pdfGetTextWidthA(PPDF IPDF, const char* AText);
double __stdcall pdfGetTextWidthW(PPDF IPDF, const wchar_t* AText);
Delphi
function pdfGetTextWidthA(const IPDF: PPDF; const AText: PAnsiChar): Double; stdcall; external 'LumasPdf.dll';
function pdfGetTextWidthW(const IPDF: PPDF; const AText: PWideChar): Double; stdcall; external 'LumasPdf.dll';

Parameters

ParameterTypeDescription
IPDFPPDFInstance handle with a selected font.
ATextstringText to measure.

Return value — Width in points; 0.0 if no font is selected, AText is empty, or the handle is invalid.

Remarks — For CID-predefined fonts, expect the returned width to be an approximation, not an exact glyph-metric sum — plan layout tolerances accordingly, or prefer a TrueType/standard-14 font when precise measurement matters.

See alsopdfGetTextWidthEx, pdfGetFTextHeight

C# (P/Invoke)

wrappers/dotnet/LumasPdf.cs
public static extern double pdfGetTextWidthA(IntPtr IPDF, [MarshalAs(UnmanagedType.LPStr)] string AText);
public static extern double pdfGetTextWidthW(IntPtr IPDF, [MarshalAs(UnmanagedType.LPWStr)] string AText);
Area
Text
Category

Getters

Exported names

pdfGetTextWidthA pdfGetTextWidthW

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.