# PdfFontCollection class

> Fonts found by family name, weight, width and style, the way CSS matches them. Add font files or directories, or use System for the fonts installed on...

Namespace: [PdfNetStack.Fonts](https://www.jadev-corp.com/api/pdfnetstack-fonts/)  
Assembly: PdfNetStack.dll  
Package: `PdfNetStack`

Fonts found by family name, weight, width and style, the way CSS matches them. Add font files
or directories, or use [System](https://www.jadev-corp.com/api/pdfnetstack-fonts/pdffontcollection/#PdfNetStack_Fonts_PdfFontCollection_System) for the fonts installed on the machine. Adding a
file reads only its names and style; the font itself loads when first found. Thread safe.

```csharp
public sealed class PdfFontCollection
```

**Inheritance**

[object](https://learn.microsoft.com/dotnet/api/system.object) ← 
[PdfFontCollection](https://www.jadev-corp.com/api/pdfnetstack-fonts/pdffontcollection/)

## Examples

<pre><code class="lang-csharp">PdfFont body = PdfFontCollection.System.Find("Segoe UI") ?? PdfStandardFont.Helvetica;
var fonts = new PdfFontCollection();
fonts.AddDirectory("fonts");
PdfFont? heading = fonts.Find("Inter", weight: 700);</code></pre>

## Remarks

A variable font counts for every weight, width and slant its axes cover: asking for weight 650
of a family whose font has a wght axis from 100 to 900 gives the instance at 650. Its named
instances are faces of the family too, and like any face can also be found by full name, such
as "Inter SemiBold".

## Properties

### <a id="PdfNetStack_Fonts_PdfFontCollection_Families"></a> Families

Family names, sorted.

```csharp
public IReadOnlyList<string> Families { get; }
```

#### Property Value

 [IReadOnlyList](https://learn.microsoft.com/dotnet/api/system.collections.generic.ireadonlylist\-1)<[string](https://learn.microsoft.com/dotnet/api/system.string)\>

### <a id="PdfNetStack_Fonts_PdfFontCollection_System"></a> System

The fonts installed on this machine (Windows, macOS and Linux font directories), scanned on first use.

```csharp
public static PdfFontCollection System { get; }
```

#### Property Value

 [PdfFontCollection](https://www.jadev-corp.com/api/pdfnetstack-fonts/pdffontcollection/)

## Methods

### <a id="PdfNetStack_Fonts_PdfFontCollection_Add_PdfNetStack_Fonts_PdfOpenTypeFont_"></a> Add\(PdfOpenTypeFont\)

Adds a loaded font under its family name. A variable font as loaded adds its whole design
space and its named instances; an instance from [GetInstance](https://www.jadev-corp.com/api/pdfnetstack-fonts/pdfopentypefont/#PdfNetStack_Fonts_PdfOpenTypeFont_GetInstance_System_String_)
adds that one location.

```csharp
public void Add(PdfOpenTypeFont font)
```

#### Parameters

`font` [PdfOpenTypeFont](https://www.jadev-corp.com/api/pdfnetstack-fonts/pdfopentypefont/)

### <a id="PdfNetStack_Fonts_PdfFontCollection_AddDirectory_System_String_System_Boolean_"></a> AddDirectory\(string, bool\)

Adds the fonts in a directory with a font extension (.ttf, .otf, .ttc, .otc, .woff, .woff2).
Files that are not readable fonts are skipped, and so are directories that are missing or
cannot be listed. When recursive, directories below are searched to 16 levels, without
following links to directories. Returns how many fonts were added.

```csharp
public int AddDirectory(string path, bool recursive = true)
```

#### Parameters

`path` [string](https://learn.microsoft.com/dotnet/api/system.string)

`recursive` [bool](https://learn.microsoft.com/dotnet/api/system.boolean)

#### Returns

 [int](https://learn.microsoft.com/dotnet/api/system.int32)

### <a id="PdfNetStack_Fonts_PdfFontCollection_AddFile_System_String_"></a> AddFile\(string\)

Adds every font of a file: one, or all fonts of a collection. Returns how many were added.

```csharp
public int AddFile(string path)
```

#### Parameters

`path` [string](https://learn.microsoft.com/dotnet/api/system.string)

#### Returns

 [int](https://learn.microsoft.com/dotnet/api/system.int32)

#### Exceptions

 [FontFormatException](https://www.jadev-corp.com/api/pdfnetstack-fonts/fontformatexception/)

The file is not a supported font.

### <a id="PdfNetStack_Fonts_PdfFontCollection_Find_System_String_System_Int32_System_Boolean_"></a> Find\(string, int, bool\)

The font of <code class="paramref">family</code> closest to the requested weight and style, chosen as
CSS does (CSS Fonts 4, 5.2): for weights 400 to 500, heavier up to 500 first, then
lighter, then heavier; below 400, lighter first; above 500, heavier first. An italic is
preferred when asked for, an upright face otherwise. A variable font gives its instance at
the weight asked when its wght axis covers it. Null when the family is unknown.

```csharp
public PdfOpenTypeFont? Find(string family, int weight = 400, bool italic = false)
```

#### Parameters

`family` [string](https://learn.microsoft.com/dotnet/api/system.string)

Family name, case insensitive, such as "Inter" or "Times New Roman", or a face's full name.

`weight` [int](https://learn.microsoft.com/dotnet/api/system.int32)

CSS weight, 1 to 1000: 400 regular, 700 bold.

`italic` [bool](https://learn.microsoft.com/dotnet/api/system.boolean)

True to prefer an italic or oblique face.

#### Returns

 [PdfOpenTypeFont](https://www.jadev-corp.com/api/pdfnetstack-fonts/pdfopentypefont/)?

### <a id="PdfNetStack_Fonts_PdfFontCollection_Find_System_String_PdfNetStack_Fonts_PdfFontQuery_"></a> Find\(string, PdfFontQuery\)

The font of <code class="paramref">family</code> that best matches <code class="paramref">query</code>, chosen as
CSS does (CSS Fonts 4, 5.2): the closest width first, then style, then weight. A variable
font gives its instance at the values found, clamped to its axes, with the query's optical
size and variations applied over them (CSS Fonts 4, 7.2). Each location is made once, so
finding it again returns the same font. Null when the family is unknown.

```csharp
public PdfOpenTypeFont? Find(string family, PdfFontQuery query)
```

#### Parameters

`family` [string](https://learn.microsoft.com/dotnet/api/system.string)

Family name, case insensitive, such as "Inter", or a face's full or PostScript name, such as "Inter SemiBold".

`query` [PdfFontQuery](https://www.jadev-corp.com/api/pdfnetstack-fonts/pdffontquery/)

The weight, width, style and variations wanted.

#### Returns

 [PdfOpenTypeFont](https://www.jadev-corp.com/api/pdfnetstack-fonts/pdfopentypefont/)?

## Used in

- [Run PdfNetStack on AWS Lambda](https://www.jadev-corp.com/guides/aws-lambda/)
- [Run PdfNetStack on Azure Functions](https://www.jadev-corp.com/guides/azure-functions/)
- [Fonts and text in PDF with C# and .NET](https://www.jadev-corp.com/guides/fonts-and-text/)
- [HTML to PDF in C# without a browser](https://www.jadev-corp.com/guides/html-to-pdf/)
- [Fonts, images and resources in HTML to PDF](https://www.jadev-corp.com/guides/html-to-pdf/fonts-and-assets/)

Source: https://www.jadev-corp.com/pdfnetstack/api/pdfnetstack-fonts/pdffontcollection/
