基于Java編寫一個PDF與Word文件轉(zhuǎn)換工具
前言
前段時間一直使用到word文檔轉(zhuǎn)pdf或者pdf轉(zhuǎn)word,尋思著用Java應該是可以實現(xiàn)的,于是花了點時間寫了個文件轉(zhuǎn)換工具
源碼weloe/FileConversion (github.com)
主要功能就是word和pdf的文件轉(zhuǎn)換,如下
- pdf 轉(zhuǎn) word
- pdf 轉(zhuǎn) 圖片
- word 轉(zhuǎn) 圖片
- word 轉(zhuǎn) html
- word 轉(zhuǎn) pdf
實現(xiàn)方法
主要使用了pdfbox Apache PDFBox | A Java PDF Library以及spire.doc Free Spire.Doc for Java | 100% 免費 Java Word 組件 (e-iceblue.cn)兩個工具包
pom.xml
<repositories>
<repository>
<id>com.e-iceblue</id>
<url>http://repo.e-iceblue.cn/repository/maven-public/</url>
</repository>
</repositories>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>2.0.4</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.doc.free</artifactId>
<version>3.9.0</version>
</dependency>
</dependencies>策略接口
public interface FileConversion {
boolean isSupport(String s);
String convert(String pathName,String dirAndFileName) throws Exception;
}PDF轉(zhuǎn)圖片實現(xiàn)
public class PDF2Image implements FileConversion{
private String suffix = ".jpg";
public static final int DEFAULT_DPI = 150;
@Override
public boolean isSupport(String s) {
return "pdf2image".equals(s);
}
@Override
public String convert(String pathName,String dirAndFileName) throws Exception {
String outPath = dirAndFileName + suffix;
if(Files.exists(Paths.get(outPath))){
throw new RuntimeException(outPath+" 文件已存在");
}
pdf2multiImage(pathName,outPath,DEFAULT_DPI);
return outPath;
}
/**
* pdf轉(zhuǎn)圖片
* 多頁PDF會每頁轉(zhuǎn)換為一張圖片,下面會有多頁組合成一頁的方法
*
* @param pdfFile pdf文件路徑
* @param outPath 圖片輸出路徑
* @param dpi 相當于圖片的分辨率,值越大越清晰,但是轉(zhuǎn)換時間變長
*/
public void pdf2multiImage(String pdfFile, String outPath, int dpi) {
if (dpi <= 0) {
// 如果沒有設置DPI,默認設置為150
dpi = DEFAULT_DPI;
}
try (PDDocument pdf = PDDocument.load(new FileInputStream(pdfFile))) {
int actSize = pdf.getNumberOfPages();
List<BufferedImage> picList = new ArrayList<>();
for (int i = 0; i < actSize; i++) {
BufferedImage image = new PDFRenderer(pdf).renderImageWithDPI(i, dpi, ImageType.RGB);
picList.add(image);
}
// 組合圖片
ImageUtil.yPic(picList, outPath);
} catch (IOException e) {
e.printStackTrace();
}
}
}
PDF轉(zhuǎn)word實現(xiàn)
public class PDF2Word implements FileConversion {
private String suffix = ".doc";
@Override
public boolean isSupport(String s) {
return "pdf2word".equals(s);
}
/**
*
* @param pathName
* @throws IOException
*/
@Override
public String convert(String pathName,String dirAndFileName) throws Exception {
String outPath = dirAndFileName + suffix;
if(Files.exists(Paths.get(outPath))){
throw new RuntimeException(outPath+" 文件已存在");
}
pdf2word(pathName, outPath);
return outPath;
}
private void pdf2word(String pathName, String outPath) throws IOException {
PDDocument doc = PDDocument.load(new File(pathName));
int pagenumber = doc.getNumberOfPages();
// 創(chuàng)建文件
createFile(Paths.get(outPath));
FileOutputStream fos = new FileOutputStream(outPath);
Writer writer = new OutputStreamWriter(fos, "UTF-8");
PDFTextStripper stripper = new PDFTextStripper();
stripper.setSortByPosition(true);//排序
stripper.setStartPage(1);//設置轉(zhuǎn)換的開始頁
stripper.setEndPage(pagenumber);//設置轉(zhuǎn)換的結(jié)束頁
stripper.writeText(doc, writer);
writer.close();
doc.close();
}
}word轉(zhuǎn)html
public class Word2HTML implements FileConversion{
private String suffix = ".html";
@Override
public boolean isSupport(String s) {
return "word2html".equals(s);
}
@Override
public String convert(String pathName, String dirAndFileName) {
String outPath = dirAndFileName + suffix;
if(Files.exists(Paths.get(outPath))){
throw new RuntimeException(outPath+" 文件已存在");
}
Document doc = new Document();
doc.loadFromFile(pathName);
doc.saveToFile(outPath, FileFormat.Html);
doc.dispose();
return outPath;
}
}word轉(zhuǎn)圖片
public class Word2Image implements FileConversion{
private String suffix = ".jpg";
@Override
public boolean isSupport(String s) {
return "word2image".equals(s);
}
@Override
public String convert(String pathName, String dirAndFileName) throws Exception {
String outPath = dirAndFileName + suffix;
if(Files.exists(Paths.get(outPath))){
throw new RuntimeException(outPath+" 文件已存在");
}
Document doc = new Document();
//加載文件
doc.loadFromFile(pathName);
//上傳文檔頁數(shù),也是最后要生成的圖片數(shù)
Integer pageCount = doc.getPageCount();
// 參數(shù)第一個和第三個都寫死 第二個參數(shù)就是生成圖片數(shù)
BufferedImage[] image = doc.saveToImages(0, pageCount, ImageType.Bitmap);
// 組合圖片
List<BufferedImage> imageList = Arrays.asList(image);
ImageUtil.yPic(imageList, outPath);
return outPath;
}
}word轉(zhuǎn)pdf
public class Word2PDF implements FileConversion{
private String suffix = ".pdf";
@Override
public boolean isSupport(String s) {
return "word2pdf".equals(s);
}
@Override
public String convert(String pathName, String dirAndFileName) throws Exception {
String outPath = dirAndFileName + suffix;
if(Files.exists(Paths.get(outPath))){
throw new RuntimeException(outPath+" 文件已存在");
}
//加載word
Document document = new Document();
document.loadFromFile(pathName, FileFormat.Docx);
//保存結(jié)果文件
document.saveToFile(outPath, FileFormat.PDF);
document.close();
return outPath;
}
}使用
輸入轉(zhuǎn)換方法,文件路徑,輸出路徑(輸出路徑如果輸入'null'則為文件同目錄下同名不同后綴文件)
轉(zhuǎn)換方法可選項:
- pdf2word
- pdf2image
- word2html
- word2image
- word2pdf
例如輸入:
pdf2word D:\test\testpdf.pdf null
控制臺輸出:
轉(zhuǎn)換方法: pdf2word 文件: D:\test\testFile.pdf
轉(zhuǎn)換成功!文件路徑: D:\test\testFile.doc
到此這篇關于基于Java編寫一個PDF與Word文件轉(zhuǎn)換工具的文章就介紹到這了,更多相關Java PDF轉(zhuǎn)Word內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
使用Vue+Spring Boot實現(xiàn)Excel上傳功能
這篇文章主要介紹了使用Vue+Spring Boot實現(xiàn)Excel上傳,需要的朋友可以參考下2018-11-11
springboot業(yè)務功能實戰(zhàn)之告別輪詢websocket的集成使用
WebSocket使得客戶端和服務器之間的數(shù)據(jù)交換變得更加簡單,允許服務端主動向客戶端推送數(shù)據(jù),下面這篇文章主要給大家介紹了關于springboot業(yè)務功能實戰(zhàn)之告別輪詢websocket的集成使用,需要的朋友可以參考下2022-10-10
maven導入本地倉庫jar包,報:Could?not?find?artifact的解決
這篇文章主要介紹了maven導入本地倉庫jar包,報:Could?not?find?artifact的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-03-03
Jenkins系統(tǒng)如何進行數(shù)據(jù)備份
隨著我們的長期使用,Jenkins系統(tǒng)中的內(nèi)容會越來越多,特別是一些配置相關的東西,不能有任何丟失。這個時候我們就需要定期備份我們的Jenkins系統(tǒng),避免一些誤操作不小心刪除了某些重要文件,本文就將介紹下Jenkins系統(tǒng)如何進行數(shù)據(jù)備份2021-06-06
Spring?Boot?Security認證之Redis緩存用戶信息詳解
本文介紹了如何使用Spring Boot Security進行認證,并通過Redis緩存用戶信息以提高系統(tǒng)性能,通過配置RedisUserDetailsManager,我們成功地將用戶信息存儲到了Redis中,并在Spring Security中進行了集成,需要的朋友可以參考下2024-01-01
Springboot通用mapper和mybatis-generator代碼示例
這篇文章主要介紹了Springboot通用mapper和mybatis-generator代碼示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-12-12
SpringBoot 策略模式實現(xiàn)切換上傳文件模式
策略模式是指有一定行動內(nèi)容的相對穩(wěn)定的策略名稱,這篇文章主要介紹了SpringBoot 策略模式 切換上傳文件模式,需要的朋友可以參考下2023-11-11

