render text using font in Java

In the previous article we demonstrated how to load CFF, TrueType and Type1 fonts programmatically with Java. This article focuses on another powerful capability of the Aspose.Font Java API – rendering text with those fonts. After reading, you’ll know how to render text using TrueType and Type1 fonts directly in your Java applications. Let’s begin.

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

Follow these concise steps to convert any string into a JPEG image using the Aspose.Font drawText implementation.

  • 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.

Conclusion

This guide showed how to generate JPEG images from text using TrueType or Type1 fonts in Java. To explore more features of the Aspose.Font Java API, consult the documentation and review additional source code samples.