Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.
To use custom fonts during HTML rendering in C#, create a Configuration, get IUserAgentService, call FontsSettings.SetFontsLookupFolder(), then load the HTMLDocument with that configuration and convert it to PDF or image.
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 set a font folder so Aspose.HTML for .NET can use custom fonts when rendering HTML documents to PDF and image formats.
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. The FontsSettings class lets you configure where Aspose.HTML looks for fonts when rendering documents.
The C# examples below demonstrate how to set a font folder for rendering an HTML document to PDF and image formats. They use the SetFontsLookupFolder() methods to specify where custom fonts are located. Before you start:
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.
To set a custom font folder for HTML to PDF conversion:
fontsFolder) and pass the folder that contains the required font files.Configuration.PdfSaveOptions and save the PDF output.Here is an example of how to set a custom font folder and render an HTML document to PDF:
1// Use custom font folder in HTML to PDF conversion
2
3// Prepare a path to a source HTML file
4string documentPath = Path.Combine(DataDir, "file.html");
5
6// Create an instance of Configuration
7using Configuration configuration = new Configuration();
8
9// Get the IUserAgentService
10IUserAgentService service = configuration.GetService<IUserAgentService>();
11
12// Set a custom font folder path
13service.FontsSettings.SetFontsLookupFolder(Path.Combine(DataDir + "fonts"));
14
15// Initialize the HTML document with specified configuration
16using (HTMLDocument document = new HTMLDocument(documentPath, configuration))
17{
18 // Convert HTML to PDF
19 Converter.ConvertHTML(document, new PdfSaveOptions(), Path.Combine(OutputDir, "file-fontsetting.pdf"));
20}The configured font folder should contain every custom font required by the HTML document. If the expected font is not available in the configured folder, the rendered PDF may use a fallback font or show text differently from the source design.
The same configuration approach can be used when converting HTML to PNG, JPG, BMP, GIF, or TIFF. The image example uses
SetFontsLookupFolder(fontsFolder, recursive), where the second parameter controls whether nested font folders are included.
To set a custom font folder for HTML to image conversion:
Configuration object and get the IUserAgentService.SetFontsLookupFolder(fontsFolder, true) when fonts may be stored in nested directories.HTMLDocument with the configured Configuration.Converter.ConvertHTML() and save the image output.Here is an example of how to convert an HTML document to an image format while using a custom font folder:
1// Set custom font folder for HTML to PNG conversion in C#
2
3// Prepare a path to a source HTML file
4string documentPath = Path.Combine(DataDir, "file.html");
5
6// Prepare a path to save the converted file
7string savePath = Path.Combine(OutputDir, "file-output.png");
8
9// Create an instance of the Configuration class
10using Configuration configuration = new Configuration();
11
12// Get the IUserAgentService
13IUserAgentService userAgentService = configuration.GetService<IUserAgentService>();
14
15// Use the SetFontsLookupFolder() method to set a directory which will act as a new fontsFolder
16// Pass "true" as the recursive parameter to use all nested directories
17userAgentService.FontsSettings.SetFontsLookupFolder(Path.Combine(DataDir + "font"), true);
18
19// Initialize the HTML document with specified configuration
20using (HTMLDocument document = new HTMLDocument(documentPath, configuration))
21{
22 // Convert HTML to Image
23 Converter.ConvertHTML(document, new ImageSaveOptions(), savePath);
24}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.
| Issue | Cause | Fix |
|---|---|---|
| Custom font is not used in PDF or image output | The font file is not in the folder passed to SetFontsLookupFolder(). | Copy the required .ttf or .otf files into the configured font folder and rerun the conversion. |
| Fonts in subfolders are ignored | The non-recursive overload is used, or the recursive parameter is false. | Use SetFontsLookupFolder(fontsFolder, true) when fonts are organized in nested folders. |
| Output differs between local machine and server | The available system fonts or custom font folder content differs between environments. | Package the required fonts with the application and configure the same font folder in each environment. |
Yes. By default, Aspose.HTML for .NET looks for fonts installed in the system. Use FontsSettings.SetFontsLookupFolder() when your HTML requires fonts stored in a specific application folder.
Use SetFontsLookupFolder(fontsFolder, true) when the custom font directory contains nested folders. This tells Aspose.HTML to include subfolders in font lookup.
Yes. Configure the font folder on Configuration, load the HTMLDocument with that configuration, and use the same document in PDF or image conversion workflows.
PdfSaveOptions.Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.