Convert TXT Files to Word, PDF and Image (JPG, PNG, BMP, TIFF) in Java
Tags: java, text, word, pdf
Published: 2022-06-10 03:50:32

TXT files are plain text files that can be opened by any text editor or word processor and can also be processed by many other programs. Though TXT is a popular file format, it has a few disadvantages. For example, it is easy to edit and does not support images, formatting and fonts. For such reasons, you may need to convert TXT files to other file formats such as Word, PDF and Image. This article will introduce how to **convert TXT files to Word, PDF and Image in Java** using [Free Spire.Doc for Java](https://www.e-iceblue.com/Introduce/free-doc-for-java.html).

The following topics will be covered:

- [Convert TXT Files to Word or PDF using Java](https://dev.to/alexis92/convert-txt-files-to-word-pdf-and-image-jpg-png-bmp-tiff-in-java-8i8/#convert-txt-files-to-word-or-pdf-using-java)
- [Convert TXT Files to JPG, PNG or BMP Image using Java](https://dev.to/alexis92/convert-txt-files-to-word-pdf-and-image-jpg-png-bmp-tiff-in-java-8i8/#convert-txt-files-to-jpg-png-or-bmp-image-using-java)
- [Convert TXT Files to TIFF Image using Java](https://dev.to/alexis92/convert-txt-files-to-word-pdf-and-image-jpg-png-bmp-tiff-in-java-8i8/#convert-txt-files-to-tiff-image-using-java)

## Add Dependencies
**Method 1:** If you are using maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml file.
```java
<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <name>e-iceblue</name>
        <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.doc.free</artifactId>
        <version>5.2.0</version>
    </dependency>
</dependencies>
```
**Method 2:** If you are not using maven, you can download the JAR file from [this link](https://www.e-iceblue.com/Download/doc-for-java-free.html), extract the zip file and then import the Spire.Doc.jar file under the lib folder into your project as a dependency.

## Convert TXT Files to Word or PDF using Java
To convert a txt file to Word or PDF, you just need to follow 3 steps:

- Initialize an instance of **Document** class.
- Load the txt file using **Document.loadText()** method.
- Save the txt file to Word or PDF using **Document.saveToFile(filePath, FileFormat)** method.
```java
import com.spire.doc.Document;
import com.spire.doc.FileFormat;

public class ConvertTxtToWordAndPdf {
    public static void main(String[] args){
        //Initialize an instance of Document class
        Document doc = new Document();
        //Load the txt file
        doc.loadText("Input.txt");

        //Save the txt file to Word
        doc.saveToFile("ToWord.docx", FileFormat.Docx);

        //Save the txt file to PDF
        doc.saveToFile("ToPdf.pdf", FileFormat.PDF);
    }
}
```
The convert Word:

![Convert TXT files to Word using Java](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rvpzvbwigykguq1xf6c5.png) 
The converted PDF:

![Converted TXT files to PDF using Java](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v26yyniedqi8lsixbkdm.png) 

## Convert TXT Files to JPG, PNG or BMP Image using Java
You can convert a txt file to common image formats such as JPG, PNG, BMP etc. The following are the steps to convert a txt file to PNG image:

- Initialize an instance of **Document** class.
- Load the txt file using **Document.loadText()** method.
- Loop through the pages in the document.
- Convert each page in the document to Bitmap image using **Document.saveToImages(pageIndex, ImageType.Bitmap)** method.
- Initialize an instance of **File** class.
- Save the image as PNG file using **ImageIO.write()** method.
```java
import com.spire.doc.Document;
import com.spire.doc.documents.ImageType;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class ConvertTxtToImage {
    public static void main(String[] args) throws IOException {
        //Initialize an instance of Document class
        Document doc = new Document();
        //Load the txt file
        doc.loadText("Input.txt");

        //Loop through the pages
        for (int i = 0; i < doc.getPageCount(); i++) {
            //Save all pages in the Word document to .png images
            BufferedImage image = doc.saveToImages(i, ImageType.Bitmap);
            File file = new File("out/" + String.format(("Img-%d.png"), i));
            ImageIO.write(image, "PNG", file);
        }
    }
}
```
The converted PNG image:

![Convert TXT files to PNG image using Java](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/orjn5iynypioa8vvwnhu.png) 

## Convert TXT Files to TIFF Image using Java
The following are the steps to convert a txt file to tiff image:

- Initialize an instance of **Document** class.
- Load the txt file using **Document.loadText()** method.
- Save the txt file to tiff using **Document.saveToTiff()** method.
```java
import com.spire.doc.Document;

public class ConvertTxtToTiff {
    public static void main(String[] args){
        //Initialize an instance of Document class
        Document doc = new Document();
        //Load the txt file
        doc.loadText("Input.txt");

        //Save the txt file to tiff
        doc.saveToTiff("ToTiff.tiff");
    }
}
```
The converted TIFF image:

![Convert TXT files to TIFF image using Java](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pnv3rmn38ehqerjdb7nx.png)