Fonts

GemBox.Pdf needs access to fonts used in a document and, by default, it will check these font locations on supported operating systems:

  • Windows: C:\Windows\Fonts\
  • Linux: /usr/share/fonts/, /usr/local/share/fonts/, and ~/.local/share/fonts/
  • macOS: /System/Library/Fonts/, /Library/Fonts/, and ~/Library/Fonts/

If a specific font is not located at the default location, you need to use one of the PdfFontFamily constructors that allow you to specify the location of that font. This is especially needed in environments with no fonts installed, like Docker.

Custom fonts

The following example shows how you can use GemBox.Pdf to write text to a PDF file using custom fonts (Almonte Snow.ttf and almonte woodgrain.ttf).

using GemBox.Pdf;
using GemBox.Pdf.Content;
using System.Linq;

class Program
{
    static void Main()
    {
        // If using the Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY");

        using (var document = new PdfDocument())
        {
            var page = document.Pages.Add();

            using (var formattedText = new PdfFormattedText())
            {
                formattedText.FontSize = 48;
                formattedText.LineHeight = 72;

                // Use the font family 'Almonte Snow' whose font file is located in the same directory as the application.
                formattedText.FontFamily = new PdfFontFamily(null, "Almonte Snow");
                formattedText.AppendLine("Hello World 1!");

                // Use the font family 'Almonte Woodgrain' whose font file is located in the same directory as the application.
                formattedText.FontFamily = new PdfFontFamily(null, "Almonte Woodgrain");
                formattedText.AppendLine("Hello World 2!");

                // Another way to use the font family 'Almonte Snow' whose font file is located in the same directory as the application.
                formattedText.FontFamily = PdfFonts.GetFontFamilies(null).First(ff => ff.Name == "Almonte Snow");
                formattedText.AppendLine("Hello World 3!");

                // Another way to use the font family 'Almonte Woodgrain' whose font file is located in the same directory as the application.
                formattedText.FontFamily = PdfFonts.GetFontFamilies(null).First(ff => ff.Name == "Almonte Woodgrain");
                formattedText.Append("Hello World 4!");

                page.Content.DrawText(formattedText, new PdfPoint(100, 500));
            }

            document.Save("Private Fonts.%OutputFileType%");
        }
    }
}
Imports GemBox.Pdf
Imports GemBox.Pdf.Content
Imports System.Linq

Module Program

    Sub Main()

        ' If using the Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY")

        Using document = New PdfDocument()

            Dim page = document.Pages.Add()

            Using formattedText = New PdfFormattedText()

                formattedText.FontSize = 48
                formattedText.LineHeight = 72

                ' Use the font family 'Almonte Snow' whose font file is located in the same directory as the application.
                formattedText.FontFamily = New PdfFontFamily(Nothing, "Almonte Snow")
                formattedText.AppendLine("Hello World 1!")

                ' Use the font family 'Almonte Woodgrain' whose font file is located in the same directory as the application.
                formattedText.FontFamily = New PdfFontFamily(Nothing, "Almonte Woodgrain")
                formattedText.AppendLine("Hello World 2!")

                ' Another way to use the font family 'Almonte Snow' whose font file is located in the same directory as the application.
                formattedText.FontFamily = PdfFonts.GetFontFamilies(Nothing).First(Function(ff) ff.Name = "Almonte Snow")
                formattedText.AppendLine("Hello World 3!")

                ' Another way to use the font family 'Almonte Woodgrain' whose font file is located in the same directory as the application.
                formattedText.FontFamily = PdfFonts.GetFontFamilies(Nothing).First(Function(ff) ff.Name = "Almonte Woodgrain")
                formattedText.Append("Hello World 4!")

                page.Content.DrawText(formattedText, New PdfPoint(100, 500))
            End Using

            document.Save("Private Fonts.%OutputFileType%")
        End Using
    End Sub
End Module
PDF file with text from private fonts
Screenshot of PDF file with text from private fonts

The PdfFonts class allows you to browse all standard, system, and custom font families and font faces.

Note that GemBox.Pdf ignores the font embedding rights (PdfFontFace.EmbeddingRights). That's why it's the application developer's responsibility to run the font licensing rights check. In case you have any doubts, please contact the font's vendor.

Assembly fonts

GemBox.Pdf supports assembly-embedded fonts in PDF files by using the PdfFonts.GetFontFamilies(string, string) method.

When you add fonts as resources to your application, make sure you're setting the Build Action as an Embedded resource.

Or if your application is targeting .NET Framework and thus uses WPF, set it as Resource instead. Also, make sure that System.Windows.Application.ResourceAssembly is correctly initialized, if you want to format text that uses embedded fonts with PdfTextFormattingMode.WPF or PdfTextFormattingMode.WPFDisplay text formatting mode.

Setting font file as embedded resource

The following image shows a structure of an example solution with font files added as embedded resources, and the following table lists base resource locations you need to specify to use those fonts.

Packaging fonts in .NET application
Retrieved fontassemblyNamelocationNotes
Font1.ttfnull, string.Empty, or "MyConsoleApplication"null or string.EmptyFont resource files are in the root of the local assembly.
Font2.ttfnull, string.Empty, or "MyConsoleApplication""MyFonts"Font resource files are in the subfolder of the local assembly.
Font3.ttf"MyClassLibrary"null or string.EmptyFont resource files are in the root of the referenced assembly.
Font4.ttf"MyClassLibrary""MyFonts"Font resource files are in the subfolder of the referenced assembly.

See also


Next steps

GemBox.Pdf is a .NET component that enables developers to read, merge and split PDF files or execute low-level object manipulations from .NET applications in a simple and efficient way.

Download Buy