In the previous article, you have seen how to load CFF, TrueType and Type1 fonts programmatically using Java. Today, we will discuss another interesting feature of our Java font manipulation API – rendering text using fonts. By the end of this article, you will be able to render text using TrueType and Type1 fonts from within your Java applications. So let’s start.
- About Java Font Manipulation API
- Implement Text Rendering Method
- Render Text using TrueType Fonts in Java
- Render Text using Type1 Fonts in Java
Java Font Manipulation API
Aspose.Font for Java provides you with the features of loading and saving the fonts as well as getting the metrics of the popular font types including CFF, TrueType, OpenType, and Type1. In addition, the API lets you render the text using the provided TrueType or Type1 fonts. You can either install the API using Maven configurations or download the API’s JAR.
<repository> <id>AsposeJavaAPI</id> <name>Aspose Java API</name> <url>https://repository.aspose.com/repo/</url> </repository>
<dependency> <groupId>com.aspose</groupId> <artifactId>aspose-font</artifactId> <version>20.10</version> </dependency>
Implement Text Rendering Method
In order to render the text, Aspose.Font for Java requires you to implement the drawText() method which will draw the provided text. The following is the complete definition of the drawText() method.
The following is a utility method that is used to calculate the glyph width for bitmap coordinate system.
Steps to Render Text with Font in Java
The following are the steps of how to render a specified text as an image file using the above-mentioned darwText() method.
- Load the font using the FontDefinition class.
- Access the font using the class such as Type1Font or TtfFont.
- Call the drawText() method by providing the details in the form of its parameters.
Render Text with TrueType Font using Java
The following code sample shows how to use the drawText() method to render text using a TrueType font. The rendering results will be generated as a JPEG image.
Render Text using Type1 Fonts in Java
The following code sample shows how to render text to a JPEG image with a Type1 font using Java.
In this article, you have seen how to render the text as JPEG images using the TrueType or Type1 font from within the Java applications. In order to learn more about the Java font manipulation API, you can visit the documentation and evaluate the API’s features using source code samples.