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:
1// Prepare a path to a source HTML file
2string documentPath = Path.Combine(DataDir, "file.html");
3
4// Create an instance of Configuration
5using var configuration = new Configuration();
6
7// Get the IUserAgentService
8var service = configuration.GetService<IUserAgentService>();
9
10// Set a custom font folder path
11service.FontsSettings.SetFontsLookupFolder(Path.Combine(DataDir + "fonts"));
12
13// Initialize the HTML document with specified configuration
14using (var document = new HTMLDocument(documentPath, configuration))
15{
16 // Convert HTML to PDF
17 Converter.ConvertHTML(document, new PdfSaveOptions(), Path.Combine(OutputDir, "file-fontsetting.pdf"));
18}
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:
1// Prepare a path to a source HTML file
2string documentPath = Path.Combine(DataDir, "file.html");
3
4// Prepare a path to save the converted file
5string savePath = Path.Combine(OutputDir, "file-output.png");
6
7// Create an instance of the Configuration class
8using var configuration = new Configuration();
9
10// Get the IUserAgentService
11var userAgentService = configuration.GetService<IUserAgentService>();
12
13// Use the SetFontsLookupFolder() method to set a directory which will act as a new fontsFolder
14// Pass "true" as the recursive parameter to use all nested directories
15userAgentService.FontsSettings.SetFontsLookupFolder(Path.Combine(DataDir + "font"), true);
16
17// Initialize the HTML document with specified configuration
18using (var document = new HTMLDocument(documentPath, configuration))
19{
20 // Convert HTML to Image
21 Converter.ConvertHTML(document, new ImageSaveOptions(), savePath);
22}
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.