Write Text on Image using Java

This tutorial describes how to write text on image using Java with the specified font without installing it on the system where the application will be executed. It shares the instructions, IDE settings, and a sample code to add text to the image using Java. You will also learn the customization of the rendered text as per the requirements.

Steps to Draw Text on Image using Java

  1. Establish the environment by adding Aspose.Font for Java to render text on an image
  2. Load the license to access the full features of the product
  3. Set the path of the font file to be used and the output image to be created
  4. Create the FontDefinition object by setting the source font type, extension, and source font file content stream
  5. Open the font using the FontDefinition settings provided above in Font.Open() method
  6. Render text on the image with the loaded font
  7. Save the generated image stream on the disk as a PNG file

These steps summarize the process to add text on image using Java. You first prepare the environment by adding Aspose.Font for Java, loading the license, and specifying both the font file and output image path. Then you open the font, render text onto an image, and finally save it as a PNG file on disk.

Code to Add Text Over Image using Java

This code has demonstrated the process to insert text on image using Java. The FontDefinition handles the loading of the fonts from a file or stream, font extension, and file name. You can call DrawText by just giving it a font, text string, and size, or use overloads that also let you set line spacing type, a line spacing value, and a maximum image width to enable word wrapping.

This article guides on rendering text with a given font from a file. To convert TTF to SVG, refer to the article on Convert TTF to SVG using Java.

 English