Text-to-image conversion is often required in various cases, such as, to make the text read-only. In a previous article, we wrote about how to convert the text in a TXT file to a PDF in Java. In this article, you will learn how to convert a text to image, i.e., PNG, JPEG, or GIF, in Java.
Java Text to Image (PNG, JPEG, or GIF) Converter - Free Download
Aspose.Words for Java is a feature-rich API to create, edit, or convert MS Word documents in Java-based applications. Furthermore, it supports high fidelity conversion of text documents to other document and image formats. In this article, we will use Aspose.Words for Java to convert text to PNG, JPEG, or GIF images. You can download the API’s JAR or install it using the following Maven configurations:
<repository> <id>AsposeJavaAPI</id> <name>Aspose Java API</name> <url>https://repository.aspose.com/repo/</url> </repository>
<dependency> <groupId>com.aspose</groupId> <artifactId>aspose-words</artifactId> <version>22.4</version> <classifier>jdk17</classifier> </dependency>
Convert Text (TXT) to Image in Java
TXT files provide the simplest and easiest way to store plain text without any formatting. Therefore, we will use a TXT file and convert its text into PNG, JPEG, or GIF images. The following are the steps to convert text to image in Java.
- Load the text file using the Document class.
- Loop through all the pages in the document.
- Extract each page using Document.ExtractPages() method.
- Save page as PNG (or another image format) using Document.save() method.
The following code sample shows how to save text to image in Java.
Java Text to Image Conversion Library - Get a Free License
You can get a temporary license in order to use Aspose.Words for Java without evaluation limitations.
In this article, you have learned how to convert a text to image programmatically in Java. You can simply install the API and use the provided code sample to convert the text to PNG, JPEG, or GIF images. Besides, you can explore other features of Aspose.Words for Java using the documentation. Also, in case you would have any questions or queries, you can contact us via our forum.