| Category | Text 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 viapdfSetCharSpacing): `Tc × (charCount - 1)
forcharCount > 1`. - Word spacing (
Tw, set viapdfSetWordSpacing):Tw × spaceCount. - Per-character width overrides set via
pdfSetCharWidth.
Worked example: Helvetica "Hello World" @ 12pt with Tc=1.25/Tw=3.5 → 62.004 + 10×1.25 + 1×3.5 = 78.004 pt.
Declarations
double __stdcall pdfGetTextWidthA(PPDF IPDF, const char* AText);
double __stdcall pdfGetTextWidthW(PPDF IPDF, const wchar_t* AText);
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
| Parameter | Type | Description |
|---|---|---|
IPDF | PPDF | Instance handle with a selected font. |
AText | string | Text 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 also — pdfGetTextWidthEx, pdfGetFTextHeight
C# (P/Invoke)
public static extern double pdfGetTextWidthA(IntPtr IPDF, [MarshalAs(UnmanagedType.LPStr)] string AText);
public static extern double pdfGetTextWidthW(IntPtr IPDF, [MarshalAs(UnmanagedType.LPWStr)] string AText);
Getters
pdfGetTextWidthA
pdfGetTextWidthW
The …A form takes UTF-8, …W
takes UTF-16; a bare name aliases the ANSI form.
Worked examples — complete programs in ten languages.