로 변환.png)
Java에서 TXT 파일을 Word, PDF 및 이미지(JPG, PNG, BMP, TIFF)로 변환
TXT 파일은 모든 텍스트 편집기 또는 워드 프로세서에서 열 수 있고 다른 많은 프로그램에서도 처리할 수 있는 일반 텍스트 파일입니다. TXT는 널리 사용되는 파일 형식이지만 몇 가지 단점이 있습니다. 예를 들어 편집하기 쉽고 이미지, 서식 및 글꼴을 지원하지 않습니다. 이러한 이유로 TXT 파일을 Word, PDF 및 이미지와 같은 다른 파일 형식으로 변환해야 할 수도 있습니다. 이 기사에서는 Free Spire.Doc for Java을 사용하여 Java에서 TXT 파일을 Word, PDF 및 이미지로 변환하는 방법을 소개합니다.
다음 주제를 다룹니다.
방법 1: maven을 사용하는 경우 프로젝트의 pom.xml 파일에 다음 코드를 추가하여 애플리케이션에서 JAR 파일을 쉽게 가져올 수 있습니다.
방법 2: maven을 사용하지 않는 경우 this link 에서 JAR 파일을 다운로드하고 zip 파일을 추출한 다음 lib 폴더 아래의 Spire.Doc.jar 파일을 프로젝트에 종속성으로 가져올 수 있습니다.
txt 파일을 Word 또는 PDF로 변환하려면 다음 3단계만 수행하면 됩니다.
Document 클래스의 인스턴스를 초기화합니다. Document.loadText() 메서드를 사용하여 txt 파일을 로드합니다. Document.saveToFile(filePath, FileFormat) 메서드를 사용하여 txt 파일을 Word 또는 PDF로 저장합니다.
변환 단어:

변환된 PDF:

txt 파일을 JPG, PNG, BMP 등과 같은 일반적인 이미지 형식으로 변환할 수 있습니다. 다음은 txt 파일을 PNG 이미지로 변환하는 단계입니다.
Document 클래스의 인스턴스를 초기화합니다. Document.loadText() 메서드를 사용하여 txt 파일을 로드합니다. 문서의 페이지를 반복합니다. Document.saveToImages(pageIndex, ImageType.Bitmap) 메서드를 사용하여 문서의 각 페이지를 비트맵 이미지로 변환합니다. File 클래스의 인스턴스를 초기화합니다. ImageIO.write() 메서드를 사용하여 이미지를 PNG 파일로 저장합니다.
변환된 PNG 이미지:

다음은 txt 파일을 tiff 이미지로 변환하는 단계입니다.
Document 클래스의 인스턴스를 초기화합니다. Document.loadText() 메서드를 사용하여 txt 파일을 로드합니다. Document.saveToTiff() 메서드를 사용하여 txt 파일을 tiff에 저장합니다.
변환된 TIFF 이미지:
다음 주제를 다룹니다.
종속성 추가
방법 1: maven을 사용하는 경우 프로젝트의 pom.xml 파일에 다음 코드를 추가하여 애플리케이션에서 JAR 파일을 쉽게 가져올 수 있습니다.
<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>
방법 2: maven을 사용하지 않는 경우 this link 에서 JAR 파일을 다운로드하고 zip 파일을 추출한 다음 lib 폴더 아래의 Spire.Doc.jar 파일을 프로젝트에 종속성으로 가져올 수 있습니다.
Java를 사용하여 TXT 파일을 Word 또는 PDF로 변환
txt 파일을 Word 또는 PDF로 변환하려면 다음 3단계만 수행하면 됩니다.
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);
}
}
변환 단어:

변환된 PDF:

Java를 사용하여 TXT 파일을 JPG, PNG 또는 BMP 이미지로 변환
txt 파일을 JPG, PNG, BMP 등과 같은 일반적인 이미지 형식으로 변환할 수 있습니다. 다음은 txt 파일을 PNG 이미지로 변환하는 단계입니다.
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);
}
}
}
변환된 PNG 이미지:

Java를 사용하여 TXT 파일을 TIFF 이미지로 변환
다음은 txt 파일을 tiff 이미지로 변환하는 단계입니다.
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");
}
}
변환된 TIFF 이미지:
