Font
Version 6.1.0
In this article
This article describes the format properties of a Font object.
Access the Font object
You can set font format properties directly or via a style (see Document / Formatting for more information).
Using a style, you can access the Font object with style.ParagraphFormat.Font
or the shortcut style.Font
, which returns the same object.
To format the font of a Paragraph object (see Contents / Text) directly, you can access the Font object with paragraph.Format.Font
.
To format the font of a FormattedText object (see Contents / Text) directly, you can access the Font object with formattedText.Font
.
Additionally, there are shortcuts for the font format properties directly in the FormattedText object.
To access the font size, you can for example simply write formattedText.Size
.
See AccessFont sample in Font in PDFsharp.Sample repository. Show resulting PDF
You can also set font format properties via object.Format.Font
or a style for some other objects of the document.
This applies to headers and footers (HeaderFooter objects, see Contents / Headers & footers) as well as Table objects and its child Column, Row and Cell objects (see Contents / Tables).
Also for Chart objects and its belonging TextArea and DataLabel objects (see Contents / Charts) you can set the font format this way.
See Document / Formatting for general information about how inheritance will carry these settings to the contained text.
For specific information about inheritance in tables, see Contents / Tables.
Font name, size & color
You can set the font name like this:
// Set the font name.
font.Name = "times new roman";
See full FontNameSizeAndColor sample in Font in PDFsharp.Sample repository. Show resulting PDF
If you’re using the Core build of MigraDoc, you may need to write an own font resolver to use a specific font (see Font resolving for more information).
You can set the font size like this:
// Set the font size.
font.Size = Unit.FromPoint(14);
See full FontNameSizeAndColor sample in Font in PDFsharp.Sample repository. Show resulting PDF
Size is a property of the type Unit (see Unit for more information).
You can set the font color like this:
// Set the font color.
font.Color = Colors.Blue;
See full FontNameSizeAndColor sample in Font in PDFsharp.Sample repository. Show resulting PDF
Color is a property of the type Color (see Colors for more information).
Bold & italic
You can set the boolean Bold property like this:
// Set the font to bold.
font.Bold = true;
See full BoldAndItalic sample in Font in PDFsharp.Sample repository. Show resulting PDF
You can set the boolean Italic property like this:
// Set the font to italic.
font.Italic = true;
See full BoldAndItalic sample in Font in PDFsharp.Sample repository. Show resulting PDF
If you’re using the Core build of MigraDoc, you may need to write an own font resolver to use a specific font (see Font resolving for more information). The font resolver is responsible to return the correct font face matching also the Bold and Italic values.
Underline
Most underlined text is underlined with one single line.
You can achieve this, by assigning Underline.Single
to the Underline property:
// Set the font.Underline to Single.
font.Underline = Underline.Single;
See full UnderlineTypes sample in Font in PDFsharp.Sample repository. Show resulting PDF
There are several patterns to underline text, which can be used by choosing a different value from the Underline enum:
Single underlines every word separately.
Dotted underlines text with a dotted line.
Dash underlines text with a dashed line.
DotDash underlines text with alternated dots and dashes.
DotDotDash underlines text with the pattern dot - dot - dash.
Superscript & subscript
You can set the boolean Superscript property like this:
// Set font to Superscript.
font.Superscript = true;
See full SuperscriptAndSubscript sample in Font in PDFsharp.Sample repository. Show resulting PDF
You can set the boolean Subscript property like this:
// Set font to Subscript.
font.Subscript = true;
See full SuperscriptAndSubscript sample in Font in PDFsharp.Sample repository. Show resulting PDF