Configure Fonts in Aspose.HTML for Java

Introduction

When working with HTML documents in Java, configuring fonts correctly is essential for creating visually appealing and readable content. Whether you’re generating reports, creating web pages, or converting documents, ensuring that your fonts are properly configured can make a significant difference. This tutorial will walk you through the process of configuring fonts in Aspose.HTML for Java, from setting up your environment to converting HTML to PDF with custom fonts. So, let’s dive in!

Prerequisites

Before we get started, there are a few prerequisites you’ll need to have in place:

  1. Java Development Kit (JDK): Ensure you have JDK 1.8 or above installed on your system.
  2. Aspose.HTML for Java Library: You can download the library from the Aspose website .
  3. Integrated Development Environment (IDE): Use an IDE like IntelliJ IDEA or Eclipse to manage your project.
  4. Basic Knowledge of Java Programming: Familiarity with Java will help you follow the tutorial more effectively.
  5. Aspose.HTML License: While you can use Aspose.HTML without a license, a temporary license or full license will remove any evaluation limitations. Get your temporary license here .

Import Packages

To begin, you’ll need to import the necessary packages into your Java project. These packages provide the classes and methods required to configure fonts, handle HTML documents, and convert them to other formats.

import java.io.IOException;

These imports bring in the core functionality of Aspose.HTML for Java, allowing you to interact with HTML content programmatically.

Step 1: Create the HTML Content

First, we need to create some basic HTML content that we’ll later style and convert to PDF. This content will be saved in an HTML file.

1.1 Writing the HTML Code

We’ll start by defining some HTML code with a header and a paragraph. This code will be saved in a file named user-agent-fontsetting.html.

String code = "<h1>FontsSettings property</h1>\r\n" +
    "<p>The FontsSettings property is used for configuration of fonts handling.</p>\r\n";

This string contains the HTML content that we want to style. Notice that it includes a header (<h1>) and a paragraph (<p>).

1.2 Saving the HTML Content to a File

Next, you’ll save this HTML content to a file using a FileWriter.

try (java.io.FileWriter fileWriter = new java.io.FileWriter("user-agent-fontsetting.html")) {
    fileWriter.write(code);
}

This code snippet writes the HTML string to a file named user-agent-fontsetting.html in your project directory.

Step 2: Configure the Aspose.HTML Environment

With the HTML file ready, the next step is to configure the Aspose.HTML environment, which involves setting up font handling and other styling parameters.

2.1 Creating an Instance of Configuration

We begin by creating an instance of the Configuration class, which allows us to configure various aspects of how HTML documents are processed.

com.aspose.html.Configuration configuration = new com.aspose.html.Configuration();

This instance will be used to access and modify the user agent settings, which control how the HTML is rendered.

2.2 Accessing the User Agent Service

The user agent service is responsible for applying styles and managing fonts. We’ll retrieve this service from the configuration.

com.aspose.html.services.IUserAgentService userAgent = configuration.getService(com.aspose.html.services.IUserAgentService.class);

This line of code fetches the IUserAgentService, which we will use to apply custom styles and configure font settings.

Step 3: Apply Custom Styles and Fonts

Now that the environment is set up, let’s apply some custom styles and specify the fonts we want to use.

3.1 Setting Custom Styles

We’ll define custom styles for the header (h1) and paragraph (p) elements in the HTML document.

userAgent.setUserStyleSheet("h1 { color:#a52a2a; }\r\n" +
    "p { color:grey; }\r\n");

Here, we’re applying a brown color (#a52a2a) to the header and a grey color (grey) to the paragraph text. These styles will be applied to the elements when the document is processed.

3.2 Setting the Custom Font Folder

To ensure that our document uses the correct fonts, we’ll set a custom folder where our fonts are stored.

userAgent.getFontsSettings().setFontsLookupFolder("fonts");

This line tells Aspose.HTML to look for fonts in the fonts directory. Make sure that this folder contains the necessary font files (e.g., .ttf or .otf files).

Step 4: Load the HTML Document with the Configuration

With everything configured, it’s time to load the HTML document using our customized settings.

We’ll initialize an HTMLDocument object with the specified configuration and the path to our HTML file.

com.aspose.html.HTMLDocument document = new com.aspose.html.HTMLDocument("user-agent-fontsetting.html", configuration);

This step creates an HTMLDocument object that is ready to be processed using the custom styles and fonts we’ve configured.

Step 5: Convert HTML to PDF

The final step in this tutorial is to convert the styled HTML document into a PDF file.

We’ll use the Converter class to convert our HTML document to PDF format.

com.aspose.html.converters.Converter.convertHTML(
    document,
    new com.aspose.html.saving.PdfSaveOptions(),
    "user-agent-fontsetting_out.pdf"
);

This code snippet converts the HTML document into a PDF file named user-agent-fontsetting_out.pdf. The PdfSaveOptions parameter allows you to specify various settings for the PDF output.

Step 6: Clean Up Resources

After the conversion is complete, it’s important to dispose of the objects to free up resources.

6.1 Disposing of the Document

Make sure to dispose of the HTMLDocument object to avoid memory leaks.

if (document != null) {
    document.dispose();
}

This ensures that all resources associated with the HTMLDocument are released.

6.2 Disposing of the Configuration

Similarly, dispose of the Configuration object when you’re done with it.

if (configuration != null) {
    configuration.dispose();
}

This final cleanup step ensures that your application runs efficiently without consuming unnecessary resources.

Conclusion

Configuring fonts in Aspose.HTML for Java is a straightforward process that can greatly enhance the appearance and readability of your HTML documents. By following the steps outlined in this guide, you can easily apply custom styles, manage fonts, and convert your HTML content into PDF format with just a few lines of code. Whether you’re a seasoned developer or new to Java, Aspose.HTML provides the tools you need to create professional-quality documents with ease.

FAQ’s

Can I use any font with Aspose.HTML for Java?

Yes, you can use any font that is supported by your operating system. Make sure to place the font files in the directory specified by the FontsLookupFolder.

Do I need a license to use Aspose.HTML for Java?

While you can use Aspose.HTML without a license for evaluation purposes, a temporary license or full license is recommended for production use to avoid limitations.

How can I customize the output PDF settings?

You can customize the PDF output by modifying the PdfSaveOptions object passed to the convertHTML method.

Is it possible to apply more complex CSS styles using Aspose.HTML?

Yes, Aspose.HTML supports a wide range of CSS styles. You can apply complex styles just as you would in a regular web environment.

Where can I find more examples and documentation?

You can find more detailed examples and documentation on the Aspose.HTML for Java documentation page .