Skip to content

Commit 74e02e8

Browse files
Updated configuring environment examples
1 parent c4dc636 commit 74e02e8

File tree

8 files changed

+631
-325
lines changed

8 files changed

+631
-325
lines changed

content/english/java/configuring-environment/_index.md

+8-1
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,16 @@ url: /java/configuring-environment/
1010

1111
## Configuring Environment in Aspose.HTML for Java Tutorials
1212
### [Set Character Set in Aspose.HTML for Java](./set-character-set/)
13+
Learn how to set the character set in Aspose.HTML for Java and convert HTML to PDF in this step-by-step guide. Ensure proper text encoding and rendering.
1314
### [Configure Fonts in Aspose.HTML for Java](./configure-fonts/)
15+
Learn how to configure fonts in Aspose.HTML for Java with this detailed, step-by-step guide. Enhance your HTML to PDF conversions with custom fonts and styles.
1416
### [Use Message Handlers in Aspose.HTML for Java](./use-message-handlers/)
17+
Learn how to use message handlers in Aspose.HTML for Java to handle missing images and other network operations effectively.
1518
### [Set Up Network Service in Aspose.HTML for Java](./setup-network-service/)
19+
Learn how to set up a network service in Aspose.HTML for Java, manage network resources, and convert HTML to PNG with custom error handling.
1620
### [Configure Runtime Service in Aspose.HTML for Java](./configure-runtime-service/)
21+
Learn how to configure the Runtime Service in Aspose.HTML for Java to optimize script execution, preventing infinite loops, and improving application performance.
1722
### [Implement Sandboxing in Aspose.HTML for Java](./implement-sandboxing/)
18-
### [Set User Style Sheet in Aspose.HTML for Java](./set-user-style-sheet/)
23+
Learn how to implement sandboxing in Aspose.HTML for Java to securely control script execution in your HTML documents and convert them to PDF.
24+
### [Set User Style Sheet in Aspose.HTML for Java](./set-user-style-sheet/)
25+
Learn how to set a custom user stylesheet in Aspose.HTML for Java, enhancing your document styling and converting HTML to PDF with ease.

content/english/java/configuring-environment/configure-fonts/_index.md

+110-49
Original file line numberDiff line numberDiff line change
@@ -2,63 +2,124 @@
22
title: Configure Fonts in Aspose.HTML for Java
33
linktitle: Configure Fonts in Aspose.HTML for Java
44
second_title: Java HTML Processing with Aspose.HTML
5-
description:
5+
description: Learn how to configure fonts in Aspose.HTML for Java with this detailed, step-by-step guide. Enhance your HTML to PDF conversions with custom fonts and styles.
66
type: docs
77
weight: 11
88
url: /java/configuring-environment/configure-fonts/
99
---
10+
## Introduction
11+
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!
1012

11-
## Complete Source Code
12-
```java
13-
package com.aspose.html.documentation.examples;
13+
## Prerequisites
14+
Before we get started, there are a few prerequisites you'll need to have in place:
15+
1. Java Development Kit (JDK): Ensure you have JDK 1.8 or above installed on your system.
16+
2. Aspose.HTML for Java Library: You can download the library from the [Aspose website](https://releases.aspose.com/html/java/).
17+
3. Integrated Development Environment (IDE): Use an IDE like IntelliJ IDEA or Eclipse to manage your project.
18+
4. Basic Knowledge of Java Programming: Familiarity with Java will help you follow the tutorial more effectively.
19+
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](https://purchase.aspose.com/temporary-license/).
1420

21+
## Import Packages
22+
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.
23+
```java
1524
import java.io.IOException;
25+
```
26+
These imports bring in the core functionality of Aspose.HTML for Java, allowing you to interact with HTML content programmatically.
27+
## Step 1: Create the HTML Content
28+
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.
29+
### 1.1 Writing the HTML Code
30+
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`.
31+
```java
32+
String code = "<h1>FontsSettings property</h1>\r\n" +
33+
"<p>The FontsSettings property is used for configuration of fonts handling.</p>\r\n";
34+
```
35+
This string contains the HTML content that we want to style. Notice that it includes a header (`<h1>`) and a paragraph (`<p>`).
36+
### 1.2 Saving the HTML Content to a File
37+
Next, you’ll save this HTML content to a file using a `FileWriter`.
38+
```java
39+
try (java.io.FileWriter fileWriter = new java.io.FileWriter("user-agent-fontsetting.html")) {
40+
fileWriter.write(code);
41+
}
42+
```
43+
This code snippet writes the HTML string to a file named `user-agent-fontsetting.html` in your project directory.
44+
## Step 2: Configure the Aspose.HTML Environment
45+
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.
46+
### 2.1 Creating an Instance of Configuration
47+
We begin by creating an instance of the `Configuration` class, which allows us to configure various aspects of how HTML documents are processed.
48+
```java
49+
com.aspose.html.Configuration configuration = new com.aspose.html.Configuration();
50+
```
51+
This instance will be used to access and modify the user agent settings, which control how the HTML is rendered.
52+
### 2.2 Accessing the User Agent Service
53+
The user agent service is responsible for applying styles and managing fonts. We'll retrieve this service from the configuration.
54+
```java
55+
com.aspose.html.services.IUserAgentService userAgent = configuration.getService(com.aspose.html.services.IUserAgentService.class);
56+
```
57+
This line of code fetches the `IUserAgentService`, which we will use to apply custom styles and configure font settings.
58+
## Step 3: Apply Custom Styles and Fonts
59+
Now that the environment is set up, let's apply some custom styles and specify the fonts we want to use.
60+
### 3.1 Setting Custom Styles
61+
We’ll define custom styles for the header (`h1`) and paragraph (`p`) elements in the HTML document.
62+
```java
63+
userAgent.setUserStyleSheet("h1 { color:#a52a2a; }\r\n" +
64+
"p { color:grey; }\r\n");
65+
```
66+
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.
67+
### 3.2 Setting the Custom Font Folder
68+
To ensure that our document uses the correct fonts, we’ll set a custom folder where our fonts are stored.
69+
```java
70+
userAgent.getFontsSettings().setFontsLookupFolder("fonts");
71+
```
72+
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).
73+
## Step 4: Load the HTML Document with the Configuration
74+
With everything configured, it's time to load the HTML document using our customized settings.
1675

17-
public class WorkingWithHTMLDocuments_EnvironmentConfiguration_Font {
18-
public static void main(String [] args) throws IOException {
19-
// START_SNIPPET WorkingWithHTMLDocuments_EnvironmentConfiguration_Font
20-
// Prepare an HTML code and save it to the file.
21-
String code = "<h1>FontsSettings property</h1>\r\n" +
22-
"<p>The FontsSettings property is used for configuration of fonts handling.</p>\r\n";
23-
24-
try (java.io.FileWriter fileWriter = new java.io.FileWriter("user-agent-fontsetting.html")) {
25-
fileWriter.write(code);
26-
}
27-
28-
// Create an instance of Configuration
29-
com.aspose.html.Configuration configuration = new com.aspose.html.Configuration();
30-
try {
31-
// Get the IUserAgentService
32-
com.aspose.html.services.IUserAgentService userAgent = configuration.getService(com.aspose.html.services.IUserAgentService.class);
33-
34-
// Set the custom style parameters for the "h1" and "p" elements
35-
userAgent.setUserStyleSheet("h1 { color:#a52a2a; }\r\n" +
36-
"p { color:grey; }\r\n");
37-
38-
// Set custom font folder path
39-
userAgent.getFontsSettings().setFontsLookupFolder("fonts");
76+
We'll initialize an `HTMLDocument` object with the specified configuration and the path to our HTML file.
77+
```java
78+
com.aspose.html.HTMLDocument document = new com.aspose.html.HTMLDocument("user-agent-fontsetting.html", configuration);
79+
```
80+
This step creates an `HTMLDocument` object that is ready to be processed using the custom styles and fonts we’ve configured.
81+
## Step 5: Convert HTML to PDF
82+
The final step in this tutorial is to convert the styled HTML document into a PDF file.
4083

41-
// Initialize an HTML document with specified configuration
42-
com.aspose.html.HTMLDocument document = new com.aspose.html.HTMLDocument("user-agent-fontsetting.html", configuration);
43-
try {
44-
// Convert HTML to PDF
45-
com.aspose.html.converters.Converter.convertHTML(
46-
document,
47-
new com.aspose.html.saving.PdfSaveOptions(),
48-
"user-agent-fontsetting_out.pdf"
49-
);
50-
} finally {
51-
if (document != null) {
52-
document.dispose();
53-
}
54-
}
55-
} finally {
56-
if (configuration != null) {
57-
configuration.dispose();
58-
}
59-
}
60-
// END_SNIPPET
61-
}
84+
We'll use the `Converter` class to convert our HTML document to PDF format.
85+
```java
86+
com.aspose.html.converters.Converter.convertHTML(
87+
document,
88+
new com.aspose.html.saving.PdfSaveOptions(),
89+
"user-agent-fontsetting_out.pdf"
90+
);
91+
```
92+
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.
93+
## Step 6: Clean Up Resources
94+
After the conversion is complete, it’s important to dispose of the objects to free up resources.
95+
### 6.1 Disposing of the Document
96+
Make sure to dispose of the `HTMLDocument` object to avoid memory leaks.
97+
```java
98+
if (document != null) {
99+
document.dispose();
62100
}
63-
64101
```
102+
This ensures that all resources associated with the `HTMLDocument` are released.
103+
### 6.2 Disposing of the Configuration
104+
Similarly, dispose of the `Configuration` object when you're done with it.
105+
```java
106+
if (configuration != null) {
107+
configuration.dispose();
108+
}
109+
```
110+
This final cleanup step ensures that your application runs efficiently without consuming unnecessary resources.
111+
112+
## Conclusion
113+
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.
114+
115+
## FAQ's
116+
### Can I use any font with Aspose.HTML for Java?
117+
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`.
118+
### Do I need a license to use Aspose.HTML for Java?
119+
While you can use Aspose.HTML without a license for evaluation purposes, a [temporary license](https://purchase.aspose.com/temporary-license/) or full license is recommended for production use to avoid limitations.
120+
### How can I customize the output PDF settings?
121+
You can customize the PDF output by modifying the `PdfSaveOptions` object passed to the `convertHTML` method.
122+
### Is it possible to apply more complex CSS styles using Aspose.HTML?
123+
Yes, Aspose.HTML supports a wide range of CSS styles. You can apply complex styles just as you would in a regular web environment.
124+
### Where can I find more examples and documentation?
125+
You can find more detailed examples and documentation on the [Aspose.HTML for Java documentation page](https://reference.aspose.com/html/java/).

0 commit comments

Comments
 (0)