SpringBoot實(shí)現(xiàn)Word轉(zhuǎn)PDF和TXT的實(shí)踐分享
背景
研發(fā)工作中難免會(huì)遇到一些奇奇怪怪的需求,就比如最近,客戶(hù)提了個(gè)新需求:上傳一個(gè)WORD文檔,要求通過(guò)系統(tǒng)把該文檔轉(zhuǎn)換成PDF和TXT??蛻?hù)的需求是沒(méi)得商量的,必須實(shí)現(xiàn)!承載著客戶(hù)的期望,我開(kāi)始在網(wǎng)上找相關(guān)的資料。沒(méi)曾想,還真有開(kāi)源的依賴(lài)專(zhuān)門(mén)處理這類(lèi)問(wèn)題,咱們一起來(lái)看看吧!
實(shí)踐
1、下載和引入Jar包
要實(shí)現(xiàn)WORD到PDF/TXT的轉(zhuǎn)換,需要引入以下幾個(gè)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包不太好找,在阿里云鏡像庫(kù)中都沒(méi)有,需要在網(wǎng)上下載后,上傳到本地的私 服庫(kù),或者用上文中的方式直接在lib中加載。我在網(wǎng)上找了這個(gè)地址,可以查看和下載相關(guān)包:Aspose.Words 24.4

2、代碼實(shí)現(xiàn)
將依賴(lài)包引入之后,編寫(xiě)以下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("測(cè)度結(jié)束");
}
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轉(zhuǎn)換Pdf成功: %s", outPath));
document.save(outPath.replace(".pdf", ".txt"), SaveFormat.TEXT);
System.out.println(String.format("WORD轉(zhuǎn)換Txt成功: %s", outPath.replace(".pdf", ".txt")));
document.save(outPath.replace(".pdf", ".html"), SaveFormat.HTML);
System.out.println(String.format("WORD轉(zhuǎn)換html成功: %s", outPath.replace(".pdf", ".html")));
pdfToTxt(new File(outPath), new File(outPath.replace(".pdf", "ByPdf.txt")));
System.out.println(String.format("通過(guò)Pdf轉(zhuǎn)換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轉(zhuǎn)換Txt成功");
}
}
}3、測(cè)試
先創(chuàng)建一個(gè)WORD文件,放在d:\upload\文件夾下:

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



從結(jié)果來(lái)看,咱們的轉(zhuǎn)換測(cè)試是非常成功的。
后記
這次的實(shí)踐的成果還是十分有價(jià)值的,它不僅可以用于項(xiàng)目中,還可以應(yīng)用于工作生活中,比如博主平常習(xí)慣看電子書(shū),在網(wǎng)上收集到的很多資料都是PDF格式的,怎么辦?用程序一轉(zhuǎn)換就行了。
但不得不說(shuō)的是,這只是一個(gè)非常初級(jí)的,學(xué)習(xí)性的Demo,實(shí)際在項(xiàng)目中,要想實(shí)現(xiàn)PDF轉(zhuǎn)換為T(mén)XT或其他文件,其實(shí)十分麻煩。要針對(duì)PDF文件是文字居多,還是圖片/表格居多,采用不同的辦法;轉(zhuǎn)換的時(shí)候,還要計(jì)算圖片的偏轉(zhuǎn)角度,去除水印,去除格式字符等諸多操作,十分繁瑣。
以上就是SpringBoot實(shí)現(xiàn)Word轉(zhuǎn)PDF和TXT的實(shí)踐分享的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot Word轉(zhuǎn)PDF/TXT的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
win10安裝JDK14.0.2的詳細(xì)安裝過(guò)程
這篇文章主要介紹了win10安裝JDK14.0.2的詳細(xì)安裝過(guò)程的相關(guān)資料,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09
Mybatis報(bào)Type interface *.*Mapper is not&
本文主要介紹了Mybatis報(bào)Type interface *.*Mapper is not known to the MapperRegis,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-07-07
Java 批量獲取地址間距離工具(支持中轉(zhuǎn)站)
本文主要介紹了Java批量獲取地址間距離,獲取兩個(gè)地址間距離,實(shí)現(xiàn)方式比較多,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-07-07
Feign利用自定義注解實(shí)現(xiàn)路徑轉(zhuǎn)義詳解
這篇文章主要講解一下如何通過(guò)注解實(shí)現(xiàn)對(duì)路由中的路徑進(jìn)行自定義編碼,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)或工作有一定的幫助,需要的可以參考一下2022-06-06
Jrebel License Server 激活 IDEA-Jrebel-在線-
這篇文章主要介紹了Jrebel License Server 激活 IDEA-Jrebel-在線-離線-均適用,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-12-12
Java實(shí)現(xiàn)數(shù)據(jù)庫(kù)連接池簡(jiǎn)易教程
這篇文章主要為大家介紹了Java實(shí)現(xiàn)數(shù)據(jù)庫(kù)連接池簡(jiǎn)易教程,感興趣的小伙伴們可以參考一下2016-01-01
關(guān)于Java的動(dòng)態(tài)代理機(jī)制
這篇文章主要介紹了關(guān)于Java的動(dòng)態(tài)代理機(jī)制,動(dòng)態(tài)代理就是,在程序運(yùn)行期,創(chuàng)建目標(biāo)對(duì)象的代理對(duì)象,并對(duì)目標(biāo)對(duì)象中的方法進(jìn)行功能性增強(qiáng)的一種技術(shù),需要的朋友可以參考下2023-05-05

