Click or drag to resize
Pdf Library for .NET

PdfFont Class

Represents a font in a pdf document. Five ways to create one:
Inheritance Hierarchy
SystemObject
  SelectPdf.UniversalPdfFont

Namespace: SelectPdf.Universal
Assembly: SelectPdf.Universal (in SelectPdf.Universal.dll) Version: 26.4
Syntax
public class PdfFont

The PdfFont type exposes the following members.

Properties
 NameDescription
Public propertyFontBaseFamily Gets the predefined font family (one of the 14 standard PDF fonts) used when this font was created via CreateStandardFont(PdfStandardFont, Single). Returns Helvetica as a default if the font was not created from a standard family.
Public propertyFontFamily Gets the name of the font family.
Public propertyHeight Gets the font height.
Public propertyIsStrikeout A flag to indicate if the font is strikeout.
Public propertyIsUnderline A flag to indicate if the font is underline.
Public propertySize Gets or sets the font size in points.
Top
Methods
 NameDescription
Public methodClone Creates a clone of this font, associated with the specified pdf document.
Public methodStatic memberCode exampleCreateCJKFont Creates a PdfFont from a standard Korean / Japanese / Chinese (CJK) font family. Like CreateStandardFont(PdfStandardFont, Single), these fonts are referenced by name (not embedded) — they ship with PDF/A-compliant readers, so the output is expected to render on any conforming viewer.
Public methodStatic memberCreateFromBytes Loads a TrueType / OpenType font from an in-memory byte array and creates a PdfFont. Convenience wrapper around CreateFromStream(Stream, Single); the font is subset-embedded into the output.
Public methodStatic memberCode exampleCreateFromFile Loads a TrueType (.ttf) or OpenType (.otf) font from a file path and creates a PdfFont. The font is subset-embedded into the output document — the subset only contains glyphs that are actually used, keeping file size small. This is the path to use for PDF/A and PDF/X output from scratch.
Public methodStatic memberCreateFromStream Loads a TrueType / OpenType font from a Stream and creates a PdfFont. The stream must be positioned at the start of the font data and is read synchronously in full. The font is subset-embedded into the output.
Public methodStatic memberCreateFromSystemFont(PdfSystemFont) Resolves the system font described by systemFont and creates a PdfFont. Equivalent to CreateFromSystemFont(String, Single, PdfFontStyle) with the descriptor's family / size / style.
Public methodStatic memberCode exampleCreateFromSystemFont(String, Single) Resolves a font installed on the host system by family name and creates a PdfFont. Loads the actual TrueType / OpenType file via PdfSystemFonts and subset-embeds it into the output document.
Public methodStatic memberCreateFromSystemFont(String, Single, PdfFontStyle) Resolves a system font by family name + style and creates a PdfFont. The font is subset-embedded into the output document.
Public methodStatic memberCode exampleCreateStandardFont Creates a PdfFont from a standard PDF font family and size.
Public methodGetCharWidth Gets the width of the specified character in points.
Public methodCode exampleGetSystemFont Gets the system font this font was created from, or null when it did not come from one.
Public methodGetTextSize Gets the size (width and height) in points of the specified string when rendered with this font.
Public methodGetTextWidth Gets the width in points of the specified string.
Top
Remarks
Fonts created via CreateStandardFont(PdfStandardFont, Single) can have Size / IsUnderline / IsStrikeout mutated after construction. TrueType, CJK, and externally-wrapped fonts are immutable — the setters throw on anything other than a standard font.
Example
// A built-in font (fast, not embedded)
PdfFont font = PdfFont.CreateStandardFont(PdfStandardFont.Helvetica, 12f);
PdfDocument document = new PdfDocument();
document.Fonts.Add(font);

// An embedded TrueType font (required for PDF/A output)
PdfFont title = PdfFont.CreateFromFile(@"C:\Windows\Fonts\arial.ttf", 22f);
float width = title.GetTextWidth("Hello world");
See Also