Font Folder – How to Install using C#
Custom Font – Why font is important
Fonts play an important role in shaping the text’s look, accessibility, aesthetic appeal, and emotional impact. Therefore, choosing the right font can be crucial for achieving specific goals, whether making a text more visually appealing, readable, or recognizable. In certain cases, you might desire to install custom fonts that you have designed, bought, or acquired from an external source.
This article describes how to install font folders so that they can be used when rendering HTML documents.
Install Font – Add Custom Font Folder in C#
Aspose.HTML for .NET is a powerful library for working with HTML documents. It allows developers to access and manipulate the HTML DOM in C# and other .NET languages. It also provides the ability to set font folders for rendering HTML documents to particular formats.
By default, Aspose.HTML for .NET looks for fonts installed in the system. Using the FontsSettings class sets the folders where Aspose.HTML looks for fonts when rendering documents.
The C# examples will demonstrate how to set font folder for rendering an HTML document to PDF and Image formats. We will consider the SetFontsLookupFolder()
methods that are used in the Aspose.HTML for .NET library to specify a folder where custom fonts are located. But before you start:
- Locate the custom font files that you want to use.
- Create a folder where you will store the custom fonts.
- Copy the custom fonts to the font folder.
Set Font Folder to Render HTML to PDF
When rendering an HTML document to a PDF file, for example, it is important to ensure that the custom fonts used in the HTML document are available to the rendering engine. By default, Aspose.HTML looks for fonts in the system font folder. However, if you are using custom fonts, you will need to call the SetFontsLookupFolder(fontsFolder)
method and tell it where to find them.
Here is an example of how to set a custom font to render an HTML document to PDF using the Aspose.HTML C# library:
1using System.IO;
2using Aspose.Html;
3using Aspose.Html.Converters;
4using Aspose.Html.Saving;
5using Aspose.Html.Services;
6...
7
8 // Prepare a path to a source HTML file
9 string documentPath = Path.Combine(DataDir, "file.html");
10
11 // Create an instance of Configuration
12 using (var configuration = new Configuration())
13 {
14 // Get the IUserAgentService
15 var service = configuration.GetService<IUserAgentService>();
16
17 // Set a custom font folder path
18 service.FontsSettings.SetFontsLookupFolder(Path.Combine(DataDir + "fonts"));
19
20 // Initialize an HTML document with specified configuration
21 using (var document = new HTMLDocument(documentPath, configuration))
22 {
23 // Convert HTML to PDF
24 Converter.ConvertHTML(document, new PdfSaveOptions(), Path.Combine(OutputDir, "file-fontsetting.pdf"));
25 }
26 }
Let’s consider the C# example of how to set a custom font folder. You should follow a few steps:
- Create an instance of the Configuration class using Configuration() constructor.
- Call the GetService method on the configuration object to retrieve an instance of the IUserAgentService interface.
- Use the
SetFontsLookupFolder(
fontsFolder
) method of the FontsSettings class and pass a custom font folder path to it. This method allows the library to search for fonts in the specified folder when the PDF document is being generated. If any custom fonts are not located in this folder, they will not be available for use in the PDF document. - Initialize an HTML document with specified configuration using HTMLDocument() constructor.
- Call the ConvertHTML() method for HTML to PDF conversion.
You can download data files and complete C# examples that demonstrate how to set font folder from GitHub.
Set Font Folder to Render HTML to Image
Let’s consider the C# example of how to use the Aspose.HTML for .NET library to convert an HTML document to an image format, while also setting a folder for custom fonts:
1using System.IO;
2using Aspose.Html;
3using Aspose.Html.Converters;
4using Aspose.Html.Saving;
5using Aspose.Html.Services;
6...
7 // Prepare a path to a source HTML file
8 string documentPath = Path.Combine(DataDir, "file.html");
9
10 // Prepare a path to save the converted file
11 string savePath = Path.Combine(OutputDir, "file-output.png");
12
13 // Create an instance of Configuration class
14 using (var configuration = new Configuration())
15 {
16 // Get the IUserAgentService
17 var service = configuration.GetService<IUserAgentService>();
18
19 // Use the SetFontsLookupFolder() method to set a directory which will act as a new fontsFolder.
20 // Pass "true" as the recursive parameter to use all nested directories.
21 service.FontsSettings.SetFontsLookupFolder(Path.Combine(DataDir + "font"), true);
22
23 // Initialize the HTML document with specified configuration
24 using (var document = new HTMLDocument(documentPath, configuration))
25 {
26 // Convert HTML to Image
27 Converter.ConvertHTML(document, new ImageSaveOptions(), savePath);
28 }
29 }
To render HTML to Image using custom fonts, you should follow a few steps:
- Create the
configuration
object, and use the GetService method to get a reference to the IUserAgentService interface. - Call the
SetFontsLookupFolder(
fontsFolder
,recursive
) method to set font folder. This method takes two parameters: the folder path where the fonts are located and a boolean value that specifies whether to include subfolders or not. By setting thetrue
as therecursive
parameter, the method includes all subfolders of the specified folder in the font lookup. - Initialize the HTML document with the specified configuration.
- Call the ConvertHTML() method to convert HTML to Image.
Thus, by setting the font folder using SetFontsLookupFolder()
methods, the Aspose.HTML library will use the custom fonts located in the specified folder to render the HTML document to various output formats, such as PDF or images, ensuring that the documents are displayed as intended with the desired fonts.
Aspose.HTML offers free HTML Web Applications that are an online collection of converters, mergers, SEO tools, HTML code generators, URL tools, and more. The applications work on any operating system with a web browser and do not require any additional software installation. It’s a fast and easy way to efficiently and effectively solve your HTML-related tasks.