SpringBoot實(shí)現(xiàn)Word轉(zhuǎn)PDF和TXT的實(shí)踐分享
背景
研發(fā)工作中難免會(huì)遇到一些奇奇怪怪的需求,就比如最近,客戶提了個(gè)新需求:上傳一個(gè)WORD文檔,要求通過(guò)系統(tǒng)把該文檔轉(zhuǎn)換成PDF和TXT??蛻舻男枨笫菦](méi)得商量的,必須實(shí)現(xiàn)!承載著客戶的期望,我開(kāi)始在網(wǎng)上找相關(guān)的資料。沒(méi)曾想,還真有開(kā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)
將依賴包引入之后,編寫(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)文章
http協(xié)議進(jìn)階之Transfer-Encoding和HttpCore實(shí)現(xiàn)詳解
這篇文章主要給大家介紹了http協(xié)議之Transfer-Encoding和HttpCore實(shí)現(xiàn)的相關(guān)資料,文中介紹的非常詳細(xì),相信對(duì)大家具有一定的參考價(jià)值,需要的朋友們下面來(lái)一起看看吧。2017-04-04
淺析Java如何高效將PDF轉(zhuǎn)換為高質(zhì)量TIFF圖片
在文檔處理和歸檔系統(tǒng)中,將PDF文件轉(zhuǎn)換為T(mén)IFF格式是一項(xiàng)非常常見(jiàn)的需求,本文將介紹如何使用Java通過(guò)Spire.PDF?for?Java庫(kù),快速實(shí)現(xiàn)PDF到TIFF的轉(zhuǎn)換,希望對(duì)大家有所幫助2026-04-04
關(guān)于Spring?Validation數(shù)據(jù)校檢的使用流程分析
在實(shí)際項(xiàng)目中,對(duì)客戶端傳遞到服務(wù)端的參數(shù)進(jìn)行校驗(yàn)至關(guān)重要,SpringValidation提供了一種便捷的方式來(lái)實(shí)現(xiàn)這一需求,通過(guò)在POJO類(lèi)的屬性上添加檢查注解,本文給大家介紹Spring?Validation數(shù)據(jù)校檢的使用流程,感興趣的朋友一起看看吧2024-11-11
基于HttpServletRequest 相關(guān)常用方法的應(yīng)用
本篇文章小編為大家介紹,基于HttpServletRequest 相關(guān)常用方法的應(yīng)用,需要的朋友參考下2013-04-04
Java開(kāi)發(fā)之spring security實(shí)現(xiàn)基于MongoDB的認(rèn)證功能
這篇文章主要介紹了Java開(kāi)發(fā)之spring security實(shí)現(xiàn)基于MongoDB的認(rèn)證功能,結(jié)合實(shí)例形式分析了spring security在非JDBC環(huán)境下的自定義認(rèn)證服務(wù)實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-11-11
Java?C++分別實(shí)現(xiàn)滑動(dòng)窗口的最大值
這篇文章主要介紹了分別通過(guò)Java和C++實(shí)現(xiàn)滑動(dòng)窗口最大值,即給定一個(gè)數(shù)組?nums?和滑動(dòng)窗口的大小?k,請(qǐng)找出所有滑動(dòng)窗口里的最大值。感興趣的可以了解一下2021-12-12

