Notepad is the easiest tool to write down important points and create quick notes in a TXT file. Since TXT files do not support advanced features, such as comments, etc., they are converted to PDF format. To perform this conversion programmatically, this article covers how to convert TXT to PDF in Java.
How to Convert TXT to PDF in Java
To render TXT files in PDF format, we will use Aspose.Words for Java. It is a popular library to create and manipulate word-processing documents. Also, it provides high-quality conversion of plain text files to PDF format, which you can use for free.
You can download the API’s JAR or install it using the following Maven configuration.
<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>21.11</version>
<type>pom</type>
</dependency>
Save TXT as PDF in Java
The conversion of a TXT file to PDF format is extremely easy using Aspose.Words. Just load the TXT file and save it as a PDF document to your desired location. The following are the steps to convert a TXT file to PDF in Java.
- Load TXT file using Document class.
- Convert TXT file to PDF using Document.save(string, SaveFormat) method.
Java Code to Export TXT to PDF
The following code sample shows how to convert a TXT file to PDF format.
The following screenshot shows the converted PDF file.
Free Java TXT to PDF Converter
You can get a free temporary license to convert your TXT files to PDF format without any limitations.
Conclusion
In this article, you have learned how to convert TXT files to PDF format in Java. You can simply install Aspose.Words for Java and automate TXT to PDF conversion in your applications. You can explore more about Aspose.Words for Java using the documentation. Also, you can post your queries or questions to our forum.