Convert Text to PDF using Java

A text document in TXT format contains plain text in the form of lines. TXT files are the simplest and easiest way to store plain text without any formatting. We can easily create, open, and edit TXT files in any text editor or word processing application. In certain cases, we may need to convert the text into read-only formats such as PDF. In this article, we will learn how to convert a text to PDF programmatically in Java.

The following topics shall be covered in this article:

Java Text to PDF Converter - Free Download

For converting text to PDF, we will be using the Aspose.Words for Java API. It allows creating and manipulating MS Word documents. Moreover, it lets you convert MS Word and text documents to various other file formats.

The Document class of the API represents a document loaded in a defined format. It is a root node of a tree that contains all other nodes of the document. The save() method of this class saves the document in a specified file format. The TxtLoadOptions class of the API allows specifying additional options when loading the text document into a Document object.

Please either download the JAR of the API or add the following pom.xml configuration in a Maven-based Java application.

<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.5</version>
    <type>pom</type>
</dependency> 

Convert Text to PDF in Java

We can convert any text document into a PDF file by following the steps given below:

  1. Load the text file using the Document class.
  2. Save as PDF using the Document.save() method. It takes the output file path as an argument.

The following code sample shows how to convert a TXT file to a PDF in Java.

Convert TXT to PDF in Java

Java Text to PDF Conversion - Trim Spaces

We can trim the white spaces while loading the text document and then convert it into a PDF file by following the steps given below:

  1. Firstly, create an instance of the TxtLoadOptions class.
  2. Next, specify various TXT load options.
  3. After that, load the text file using the Document class with the TxtLoadOptions object.
  4. Save as PDF using the Document.save() method. It takes the output file path as an argument.

The following code sample shows how to trim white spaces and convert a TXT file to a PDF in Java.

Java Text to PDF Converter API - Get a Free API License

You can get a free temporary license to try the library without evaluation limitations.

Conclusion

In this article, we have learned how to convert Text to PDF format programmatically in Java. We have also seen how to trim white spaces while loading the TXT file and then convert it to PDF. Besides, you can learn more about Aspose.Words for Java API using the documentation. In case of any ambiguity, please feel free to contact us on forum.

See Also