Drawing Text and Fonts with Java
Working with text and fonts using Java is simplified with Aspose.Drawing for Java. You can effortlessly combine fonts and text styles to draw text during graphics drawing. This article guides you on:
- Drawing Text
- Formatting Text
- Text Hinting
- Retrieving Existing Fonts
Draw Text in Java
Below are the steps to draw text:
- Create an instance of the
Bitmap
class. - Initialize a new object of the
Graphics
class with the bitmap object. - Set up a
Brush
object for text drawing. - Employ the
drawString()
method to draw text on the bitmap.
These simple steps empower you to effortlessly integrate text into your graphics using Aspose.Drawing for Java.
// For complete examples and data files, please go to https://github.com/aspose-drawing/Aspose.Drawing-for-JAVA | |
import com.aspose.drawing.*; | |
import com.aspose.drawing.text; | |
Bitmap bitmap = new Bitmap(1000, 800, com.aspose.drawing.imaging.Format32bppPArgb); | |
Graphics graphics = Graphics.fromImage(bitmap); | |
graphics.setTextRenderingHint(TextRenderingHint.AntiAliasGridFit); | |
graphics.clear(Color.getWhite()); | |
Brush brush = new SolidBrush(Color.getBlack()); | |
Pen pen = new Pen(Color.getBlue(), 1); | |
Font arial = new Font("Arial", 20, FontStyle.Regular); | |
String text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas sapien tellus, mattis et condimentum eget, commodo ut ipsum. Maecenas elit sapien, tempus sit amet mauris sit amet, hendrerit laoreet nisi. Nulla facilisi. Sed commodo, mauris eget porta commodo, nunc tellus volutpat mi, eu auctor diam libero vel neque. Vestibulum nec mattis dui, nec molestie nisl. Etiam in magna felis. Praesent non nulla tortor. Integer nec convallis purus. Fusce vitae mollis mauris. Cras efficitur dui at mi viverra scelerisque. Morbi quis magna elit. Nulla facilisis id ante sit amet fringilla. Sed iaculis consectetur lectus a interdum. Etiam ut sollicitudin lectus, et congue lectus."; | |
Rectangle rectangle = new Rectangle(100, 100, 800, 600); | |
graphics.drawRectangle(pen, rectangle); | |
graphics.drawString(text, arial, brush, RectangleF.to_RectangleF(rectangle)); | |
bitmap.save("DrawText.png"); |
Format Text in Java
Formatting text adds a layer of sophistication to your Java graphics, and with Aspose.Drawing for Java, it’s a straightforward process. Here’s how you can format text:
- Begin by creating a
Bitmap
object. - Initialize a new
Graphics
object using the created bitmap object. - Specify the necessary string format parameters, such as string alignment and line alignment, to achieve the desired formatting.
- Utilize the
drawString()
method to effortlessly draw formatted text on the bitmap.
// For complete examples and data files, please go to https://github.com/aspose-drawing/Aspose.Drawing-for-JAVA | |
import com.aspose.drawing.*; | |
import com.aspose.drawing.text; | |
Bitmap bitmap = new Bitmap(1000, 800, com.aspose.drawing.imaging.Format32bppPArgb); | |
Graphics graphics = Graphics.fromImage(bitmap); | |
graphics.setTextRenderingHint(TextRenderingHint.AntiAliasGridFit); | |
graphics.clear(Color.getWhite()); | |
StringFormat stringFormat = new StringFormat(); | |
stringFormat.setAlignment(StringAlignment.Center); | |
stringFormat.setLineAlignment(StringAlignment.Center); | |
Brush brush = new SolidBrush(Color.getBlack()); | |
Pen pen = new Pen(Color.getBlue(), 1); | |
Font arial = new Font("Arial", 20, FontStyle.Regular); | |
String text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas sapien tellus, mattis et condimentum eget, commodo ut ipsum. Maecenas elit sapien, tempus sit amet mauris sit amet, hendrerit laoreet nisi. Nulla facilisi. Sed commodo, mauris eget porta commodo, nunc tellus volutpat mi, eu auctor diam libero vel neque. Vestibulum nec mattis dui, nec molestie nisl. Etiam in magna felis. Praesent non nulla tortor. Integer nec convallis purus. Fusce vitae mollis mauris. Cras efficitur dui at mi viverra scelerisque. Morbi quis magna elit. Nulla facilisis id ante sit amet fringilla. Sed iaculis consectetur lectus a interdum. Etiam ut sollicitudin lectus, et congue lectus."; | |
RectangleF rectangle = new RectangleF(100, 100, 800, 600); | |
graphics.drawRectangle(pen, Rectangle.ceiling(rectangle)); | |
graphics.drawString(text, arial, brush, rectangle, stringFormat); | |
bitmap.save("FormatText.png"); |
Text Hinting in Java
Text hinting mode is a crucial aspect of text rendering, and with Aspose.Drawing for Java, you have the flexibility to control it. Steps to Set Text Hinting Mode:
- Start by creating a
Bitmap
object. - Initialize a new
Graphics
object using the created bitmap object. - Utilize the
TextRenderingHint
property of theGraphics
object to specify the desired hinting mode. - Finally, employ the
drawText
function to draw text with the specified hinting mode.
By following these steps, you can precisely control the text hinting mode to achieve optimal text rendering using Aspose.Drawing for Java.
Text hinting mode can be specified using the API. The following Java code shows how to set the grid fitting mode.
// For complete examples and data files, please go to https://github.com/aspose-drawing/Aspose.Drawing-for-JAVA | |
import com.aspose.drawing.*; | |
import com.aspose.drawing.text; | |
Bitmap bitmap = new Bitmap(1000, 800, com.aspose.drawing.imaging.Format32bppPArgb); | |
Graphics graphics = Graphics.fromImage(bitmap); | |
graphics.setTextRenderingHint(TextRenderingHint.AntiAliasGridFit); | |
graphics.clear(Color.getWhite()); | |
drawText(graphics, "Arial", 100); | |
drawText(graphics, "Times New Roman", 200); | |
drawText(graphics, "Verdana", 300); | |
bitmap.save("Hinting.png"); | |
void drawText(Graphics graphics, String familyName, int y) | |
{ | |
Brush brush = new SolidBrush(Color.getBlack()); | |
Font font = new Font(familyName, 10, FontStyle.Regular); | |
String text = "The quick brown fox jumps over the lazy dog. 0123456789 ~!@#$%^&*()_+-={}[];':\"<>?/,.\\¹`"; | |
graphics.drawString(text, font, brush, 100, y); | |
} |

Installed Fonts in Java
Retrieving Existing Fonts in Java:
// For complete examples and data files, please go to https://github.com/aspose-drawing/Aspose.Drawing-for-JAVA | |
import com.aspose.drawing.*; | |
import com.aspose.drawing.text; | |
Bitmap bitmap = new Bitmap(1000, 800, com.aspose.drawing.imaging.Format32bppPArgb); | |
Graphics graphics = Graphics.fromImage(bitmap); | |
graphics.setTextRenderingHint(TextRenderingHint.AntiAliasGridFit); | |
graphics.clear(Color.getWhite()); | |
Brush brush = new SolidBrush(Color.getBlack()); | |
InstalledFontCollection fonts = new InstalledFontCollection(); | |
Font arial = new Font("Arial", 20, FontStyle.Regular); | |
graphics.drawString(fonts.getFamilies().length + " installed font families.", arial, brush, 100, 100); | |
for (int i = 0; i < 6 && i < fonts.getFamilies().length; ++i) | |
{ | |
graphics.drawString(fonts.getFamilies()[i].getName(), arial, brush, 100, (i + 2) * 100); | |
} | |
bitmap.save("InstalledFonts.png"); |