Fonts

<<<<<<<< HEAD:GemBox.ExampleExplorer.Web/Examples/Pdf/Features/PrivateFonts/PrivateFonts.cshtml

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

========

By default, GemBox.Pdf will check for installed fonts on these 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/

Custom or private fonts are fonts that are not installed or don't need to be installed on the computer that executes your PDF app. Using custom fonts is required in environments with no installed fonts (like on Docker) or restricted environments that don't have access to system fonts (like on Medium Level Trust applications). Custom fonts can be embedded in an assembly or located in a custom directory.

GemBox.Pdf enables you to use either system or custom fonts when writing text. 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.

Custom fonts

The image below shows the result of the example code snippet we used to demonstrate how to write text using private or custom fonts (Almonte Snow.ttf and almonte woodgrain.ttf). Notice that they are located in the same directory as the application.

PDF file with text from custom fonts
Screenshot of PDF file with text from custom fonts
>>>>>>>> features/custom-fonts:GemBox.ExampleExplorer.Web/Examples/Pdf/AdvancedFeatures/Fonts/Fonts.cshtml
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

GemBox.Pdf enables you to use either system or private fonts when writing text. The PdfFonts class allows you to browse all standard, system, and private 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 font assemblyName location Notes
Font1.ttf null, string.Empty, or "MyConsoleApplication" null or string.Empty Font resource files are in the root of the local assembly.
Font2.ttf null, string.Empty, or "MyConsoleApplication" "MyFonts" Font resource files are in the subfolder of the local assembly.
Font3.ttf "MyClassLibrary" null or string.Empty Font 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