Private Fonts
Private fonts are fonts that are not installed or don't need to be installed on the computer that executes your PDF app. Using private 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). Private fonts can be embedded in an assembly or located in a custom directory.
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.
The image below shows the result of the example code snippet we used to demonstrate how to write text using private fonts (Almonte Snow.ttf and almonte woodgrain.ttf). Notice that they are located in the same directory as the application.
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
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 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.
The following image shows an example of a solution with font files added as embedded resources, and the following table illustrates the various assemblyName
and location
values you can specify to retrieve those fonts.
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. |