API referenceFonts

pdfSetFont

Select (creating on first use) a font by name, style, and size

CategoryFont selection
Exported namespdfSetFontA, pdfSetFontW
Notestyle-bitmask mismatch above fixed 2026-07-12; two accepted-but-unused parameters remain (see Remarks)

Purpose — Select (creating on first use) a font by name, style, and size for subsequent text output.

Description — the font resolution chain. pdfSetFont never truly fails for a non-empty name; it degrades through five tiers and always lands somewhere:

  1. Standard-14 matchName (after style-suffixing) matches one of the 14 base PDF fonts (Helvetica/Times/Courier/Symbol/ZapfDingbats families, including common aliases like "Arial" → Helvetica, "Times New Roman" → Times) → uses that standard font, no embedding needed.
  2. Exact system TrueType matchFindSystemFontPath(Name, Style) finds an installed font file matching both name and style.
  3. Regular-variant system match — same lookup with Style=0, if the styled variant wasn't found (so a request for "Arial Bold" when only plain Arial is installed still resolves to the Arial file — then synthesizes bold/italic at draw time: stroke-mode render for bold, sheared text matrix for oblique, tagged -SynBold/-SynItal on the internal font entry).
  4. Alternate font list — if steps 2–3 found nothing, tries each name registered via pdfSetAltFonts, in order, until one resolves to a real system font. (This makes the alt-font list a genuine substitution input — not a write-only store, unlike several other "list" setters in this SDK.)
  5. Bundled embedded fallback — 20 fonts compiled directly into the DLL (Arial/Times New Roman/Courier New/Symbol/Wingdings/Calibri/Tahoma family members), consulted only if nothing above resolved.
  6. Last resort — if even the bundled fallback has nothing, the engine emits a font resource using the literal requested name (with a -Bold/-Italic/-BoldOblique suffix as appropriate) as /BaseFont, with no embedded program and no /Widths array. The PDF is still valid and pdfSetFont still reports success — but text drawn with this font relies entirely on the viewer having (or substituting) a matching face, and width measurement falls back to the coarse char-count estimate documented for pdfGetTextWidth.

Declarations

C
int32_t __stdcall pdfSetFontA(PPDF IPDF, const char* Name, TFStyle Style, double Size, BOOL32 Embed, TCodepage CP);
int32_t __stdcall pdfSetFontW(PPDF IPDF, const wchar_t* Name, TFStyle Style, double Size, BOOL32 Embed, TCodepage CP);
Delphi
function pdfSetFontA(const IPDF: PPDF; const Name: PAnsiChar; Style: TFStyle; Size: Double; Embed: LongBool; CP: TCodepage): Integer; stdcall; external 'LumasPdf.dll';
function pdfSetFontW(const IPDF: PPDF; const Name: PWideChar; Style: TFStyle; Size: Double; Embed: LongBool; CP: TCodepage): Integer; stdcall; external 'LumasPdf.dll';

Parameters

ParameterTypeDescription
IPDFPPDFInstance handle with an open document.
NamestringFont family/PostScript name. pdfSetFontW's wide→narrow conversion is a raw per-character truncating cast (AnsiChar(Ord(WideChar))), not a codepage-aware transcode — matters only if the name itself contains non-ASCII characters (rare for font names, but not impossible for some localized family names).
StyleTFStyleOnly bit 0 (Bold) and bit 1 (Italic) have any effect; the fsBold/fsItalic named constants correctly set those bits (fixed 2026-07-12 — see the chapter-level note).
SizeDoublePoint size.
EmbedBOOL32Accepted but not read by this implementation — see Remarks.
CPTCodepageAccepted but not read by this implementation — see Remarks.

Return value — 1-based font handle (> 0) on success; 0 if Name is NULL or no document is open. Per the resolution chain above, this essentially never fails for a valid handle and non-empty name.

Remarks — two more accepted-but-unused parameters. Embed and CP are part of the exported signature but are not passed through to TLumasPdfDoc.SetFont at all — whether a font ends up embedded is decided entirely by the resolution chain (standard-14 never embeds; a resolved TrueType file is always embedded; the last-resort literal-name fallback never embeds), not by this flag. CP (a requested codepage for narrow-string decoding) is likewise inert here.

See alsopdfSetFontEx, pdfChangeFontStyle

C# (P/Invoke)

wrappers/dotnet/LumasPdf.cs
public static extern int pdfSetFontA(IntPtr IPDF, [MarshalAs(UnmanagedType.LPStr)] string Name, int Style, double Size, [MarshalAs(UnmanagedType.Bool)] bool Embed, TCodepage CP);
public static extern int pdfSetFontW(IntPtr IPDF, [MarshalAs(UnmanagedType.LPWStr)] string Name, int Style, double Size, [MarshalAs(UnmanagedType.Bool)] bool Embed, TCodepage CP);
Area
Fonts
Category

Setters

Exported names

pdfSetFontA pdfSetFontW

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.