前言
博主介紹:?目前全網(wǎng)粉絲2W+,csdn博客專家、Java領域優(yōu)質(zhì)創(chuàng)作者,博客之星、阿里云平臺優(yōu)質(zhì)作者、專注于Java后端技術(shù)領域。
涵蓋技術(shù)內(nèi)容:Java后端、算法、分布式微服務、中間件、前端、運維、ROS等。
博主所有博客文件目錄索引:博客目錄索引(持續(xù)更新)
視頻平臺:b站-Coder長路
源碼獲取
項目源碼:Gitee、Github
本篇文檔的視頻系列講解:Java實現(xiàn)自動化pdf打水印工具 開源PDF工具PDFBoxWord、Word轉(zhuǎn)PDF開源工具Documents4j
一、認識Documents4j
? Documents4j 是一個開源的 Java 庫,用于在 Java 應用程序中進行 Microsoft Office 文檔(如 Word、Excel、PowerPoint 等)的轉(zhuǎn)換。它利用 Microsoft Office 的本機 API,通過啟動 Microsoft Office 進程來執(zhí)行文檔轉(zhuǎn)換。Documents4j 提供了一種簡單的方式來將 Office 文檔轉(zhuǎn)換為其他格式,例如將 Word 文檔轉(zhuǎn)換為 PDF、將 Excel 表格轉(zhuǎn)換為 CSV 等。
? Documents4j 的工作原理是通過將 Microsoft Office 作為外部進程啟動,并與其進行通信來執(zhí)行文檔轉(zhuǎn)換任務。這種方法使得可以利用 Microsoft Office 的強大功能來執(zhí)行文檔轉(zhuǎn)換,同時又能夠在 Java 環(huán)境中方便地進行集成和控制。
? Documents4j 提供了一個簡單的 API,使得在 Java 應用程序中執(zhí)行文檔轉(zhuǎn)換變得簡單。它支持多線程操作,并且具有一定的性能優(yōu)化,使得可以高效地處理大量文檔轉(zhuǎn)換任務。
二、快速集成
2.1、pom.xml依賴
<!--word 2 pdf start-->
<dependency>
<groupId>com.documents4j</groupId>
<artifactId>documents4j-local</artifactId>
<version>1.0.3</version>
</dependency>
<dependency>
<groupId>com.documents4j</groupId>
<artifactId>documents4j-transformer-msoffice-word</artifactId>
<version>1.0.3</version>
</dependency>
<!--word 2 pdf end -->
2.2、word轉(zhuǎn)PDF實現(xiàn)
項目目錄
WordUtils.java
package com.changlu.utils;
import com.documents4j.api.DocumentType;
import com.documents4j.api.IConverter;
import com.documents4j.job.LocalConverter;
import java.io.*;
public class WordUtils {
/**
* Word轉(zhuǎn)PDF
* @param filePath 源docx文件目錄及名稱 示例:C:\Users\93997\Desktop\watermark tools\watermarkTools\src\main\resources\2024-2-8計算機.docx
* @param outFilePath 輸出文件目錄及名稱 示例:C:\Users\93997\Desktop\watermark tools\watermarkTools\src\main\resources\2024-2-8.pdf
*/
public static void wordToPdf(String filePath, String outFilePath) {
//源文件地址
File inputWord = new File(filePath);
//導出文件地址
File outputFile = new File(outFilePath);
InputStream doc = null;
OutputStream outputStream = null;
try {
doc = new FileInputStream(inputWord);
outputStream = new FileOutputStream(outputFile);
IConverter converter = LocalConverter.builder().build();
//轉(zhuǎn)換docx=>pdf
boolean flag = converter.convert(doc).as(DocumentType.DOC).to(outputStream).as(DocumentType.PDF).execute();
if (flag) {
converter.shutDown();
}
doc.close();
outputStream.close();
System.out.println("文件名:" + outFilePath + " 轉(zhuǎn)換成功!");
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
String filePath = "C:\\Users\\93997\\Desktop\\watermark tools\\watermarkTools\\src\\main\\resources\\2024-2-8計算機.docx";
String outFilePath = "C:\\Users\\93997\\Desktop\\watermark tools\\watermarkTools\\src\\main\\resources\\2024-2-8.pdf";
//word轉(zhuǎn)pdf
WordUtils.wordToPdf(filePath, outFilePath);
}
}
Demo6.java
package com.changlu.demos;
import com.changlu.utils.WordUtils;
import java.io.UnsupportedEncodingException;
/**
* @Description: Documents4j案例:word轉(zhuǎn)PDF實現(xiàn)
* @Author: changlu
* @Date: 2:18 PM
*/
public class Demo6 {
public static void main(String[] args) throws UnsupportedEncodingException {
// String originPath = URLDecoder.decode(Main.class.getClassLoader().getResource("input.docx").getFile(), "UTF-8"); //獲取到的是target下的類目錄
String originPath = "F:\\00核心知識、成果、視頻產(chǎn)出區(qū)\\技術(shù)視頻\\2024.2.15 自制默默學打水印工具 watermark tools\\watermarkTools\\src\\main\\resources\\input.docx";
String targetPath = originPath.replace("input.docx", "output.pdf");
//docx轉(zhuǎn)為pdf文件
WordUtils.wordToPdf(originPath, targetPath);
}
}
測試效果
參考文章
[1]. 不要在去充VIP啦 ,Java 實現(xiàn) PDF、Word 互轉(zhuǎn)
資料獲取
大家點贊、收藏、關(guān)注、評論啦~
精彩專欄推薦訂閱:在下方專欄????
- 長路-文章目錄匯總(算法、后端Java、前端、運維技術(shù)導航):博主所有博客導航索引匯總
- 開源項目Studio-Vue—校園工作室管理系統(tǒng)(含前后臺,SpringBoot+Vue):博主個人獨立項目,包含詳細部署上線視頻,已開源
- 學習與生活-專欄:可以了解博主的學習歷程
- 算法專欄:算法收錄
更多博客與資料可查看????獲取聯(lián)系方式????,??文末獲取開發(fā)資源及更多資源博客獲取??文章來源:http://www.zghlxwxcb.cn/news/detail-829823.html
整理者:長路 時間:2024.2.15文章來源地址http://www.zghlxwxcb.cn/news/detail-829823.html
到了這里,關(guān)于Java使用Documents4j實現(xiàn)Word轉(zhuǎn)PDF(知識點+案例)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!