SpringBoot實現(xiàn)Word轉PDF和TXT的實踐分享
背景
研發(fā)工作中難免會遇到一些奇奇怪怪的需求,就比如最近,客戶提了個新需求:上傳一個WORD文檔,要求通過系統(tǒng)把該文檔轉換成PDF和TXT。客戶的需求是沒得商量的,必須實現(xiàn)!承載著客戶的期望,我開始在網(wǎng)上找相關的資料。沒曾想,還真有開源的依賴專門處理這類問題,咱們一起來看看吧!
實踐
1、下載和引入Jar包
要實現(xiàn)WORD到PDF/TXT的轉換,需要引入以下幾個Jar包:
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-words</artifactId>
<version>19.1</version>
<scope>system</scope>
<systemPath>${pom.basedir}/src/main/resources/lib/aspose-words-19.1.jar</systemPath>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.pdfbox/pdfbox-tools -->
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>3.0.3</version>
</dependency>其中,aspose-words包不太好找,在阿里云鏡像庫中都沒有,需要在網(wǎng)上下載后,上傳到本地的私 服庫,或者用上文中的方式直接在lib中加載。我在網(wǎng)上找了這個地址,可以查看和下載相關包:Aspose.Words 24.4

2、代碼實現(xiàn)
將依賴包引入之后,編寫以下Java代碼:
package com.leixi.fileTrans.utils;
import com.aspose.words.SaveFormat;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import com.aspose.words.Document;
import org.apache.pdfbox.Loader;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;
/**
*
* @author leixiyueqi
* @since 2024/08/26 19:39
*/
public class FileTransUtils {
public static void main(String[] args) throws Exception {
File file = new File("D:\\upload\\SAAS.docx");
String output = "D:\\upload\\SAAS.pdf";
doc2pdf(file, output);
System.out.println("測度結束");
}
public static void doc2pdf(File file, String outPath) throws Exception{
FileInputStream fis = new FileInputStream(file);
Document document = new Document(fis);
if (!checkDirectory(outPath)) {
throw new Exception("創(chuàng)建目錄失敗");
}
document.save(outPath, SaveFormat.PDF);
System.out.println(String.format("WORD轉換Pdf成功: %s", outPath));
document.save(outPath.replace(".pdf", ".txt"), SaveFormat.TEXT);
System.out.println(String.format("WORD轉換Txt成功: %s", outPath.replace(".pdf", ".txt")));
document.save(outPath.replace(".pdf", ".html"), SaveFormat.HTML);
System.out.println(String.format("WORD轉換html成功: %s", outPath.replace(".pdf", ".html")));
pdfToTxt(new File(outPath), new File(outPath.replace(".pdf", "ByPdf.txt")));
System.out.println(String.format("通過Pdf轉換Txt成功: %s", outPath.replace(".pdf", "ByPdf.txt")));
}
public static boolean checkDirectory(String filePath) {
File file = new File(filePath);
if (file.isDirectory()) {
return true;
} else {
File dir = file.getParentFile();
if (dir != null && !dir.isDirectory() && !dir.mkdirs()) {
System.out.println(String.format("創(chuàng)建目錄%s失敗:", dir.getAbsolutePath()));
return false;
} else {
return true;
}
}
}
public static void pdfToTxt(File input, File output) {
BufferedWriter wr = null;
try {
PDDocument pd = Loader.loadPDF(input);
pd.save("CopyOf" + input.getName().split("\\.")[0] + ".pdf");
PDFTextStripper stripper = new PDFTextStripper();
wr = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(output)));
stripper.writeText(pd, wr);
if (pd != null) {
pd.close();
}
wr.close();
} catch (Exception e) {
e.printStackTrace();
}finally {
System.out.println("PDF轉換Txt成功");
}
}
}3、測試
先創(chuàng)建一個WORD文件,放在d:\upload\文件夾下:

然后執(zhí)行Java代碼中的main方法,結果如下:



從結果來看,咱們的轉換測試是非常成功的。
后記
這次的實踐的成果還是十分有價值的,它不僅可以用于項目中,還可以應用于工作生活中,比如博主平常習慣看電子書,在網(wǎng)上收集到的很多資料都是PDF格式的,怎么辦?用程序一轉換就行了。
但不得不說的是,這只是一個非常初級的,學習性的Demo,實際在項目中,要想實現(xiàn)PDF轉換為TXT或其他文件,其實十分麻煩。要針對PDF文件是文字居多,還是圖片/表格居多,采用不同的辦法;轉換的時候,還要計算圖片的偏轉角度,去除水印,去除格式字符等諸多操作,十分繁瑣。
以上就是SpringBoot實現(xiàn)Word轉PDF和TXT的實踐分享的詳細內(nèi)容,更多關于SpringBoot Word轉PDF/TXT的資料請關注腳本之家其它相關文章!
相關文章
Mybatis報Type interface *.*Mapper is not&
本文主要介紹了Mybatis報Type interface *.*Mapper is not known to the MapperRegis,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2024-07-07
Jrebel License Server 激活 IDEA-Jrebel-在線-
這篇文章主要介紹了Jrebel License Server 激活 IDEA-Jrebel-在線-離線-均適用,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-12-12
Java實現(xiàn)數(shù)據(jù)庫連接池簡易教程
這篇文章主要為大家介紹了Java實現(xiàn)數(shù)據(jù)庫連接池簡易教程,感興趣的小伙伴們可以參考一下2016-01-01

