Convert Text to PDF using Java

Converting text files (TXT) to Portable Document Format (PDF) is a common task in many applications. Fortunately, Java developers can achieve this task easily using Aspose.Words for Java, a powerful document processing library. Aspose.Words provides an extensive set of features to manipulate and convert various text/document formats, including TXT to PDF conversion. In this blog, we’ll walk you through the steps to convert TXT files to PDF in a Java application.

The following topics shall be covered in this article:

  1. Java Text to PDF Converter
  2. Convert Text to PDF in Java
  3. Trim Spaces and Convert Text to PDF

Java Library to Convert Text File to PDF

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

Convert Text File 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

Trim Spaces in Text to PDF Conversion

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.

Get a Free License

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

Conclusion

Converting TXT files to PDF in Java can be achieved efficiently using the Aspose.Words for Java library. The sample code provided in this blog demonstrates a simple and effective way to perform this conversion. Aspose.Words for Java offers various options and settings to customize the conversion process according to your requirements. Explore the documentation to learn more about the available features and advanced settings. In case of any ambiguity, please feel free to contact us on forum.

See Also