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.
The following topics will be covered:
- Convert TXT Files to Word or PDF using Java
- Convert TXT Files to JPG, PNG or BMP Image using Java
- 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.
<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, 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.
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 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.
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 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.
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:
Top comments (0)