個人經驗,其他轉換工具麻煩或效果不好,且置入java項目代價大。已經習慣了引入jar包後直接調用工具類的人可以留步細看一下,在此分享一下封裝好的一個jar包,直接build path,然後調用即可,支持linux和windows環境。
使用樣例:
1.首先下載jar: kevy-office.jar,放在項目lib下,build path,maven項目則直接在pom引入本地jar或加入本地倉庫
2.jar對外暴露api介紹,如截圖
2.1 boolean convertOfficeToPdf(String originFilePath, String destFilePath) --office文件轉pdf 支持格式(doc/docx/ppt/pptx/xls/xlsx)
originFilePath--源文件全路徑
destFilePath--目標存儲文件全路徑
返回:true-轉換成功 false-轉換失敗。備註:ppt默認按A4紙至轉換。
2.2 boolean excel2Pdf(String originFilePath, String destFilePath) --excel文件(xls/xlsx)轉pdf
originFilePath--源文件全路徑
destFilePath--目標存儲文件全路徑
返回:true-轉換成功 false-轉換失敗
2.3 boolean ppt2Pdf(String originFilePath, String destFilePath) --ppt文件(ppt/pptx)轉pdf
originFilePath--源文件全路徑
destFilePath--目標存儲文件全路徑
返回:true-轉換成功 false-轉換失敗
2.3.1 boolean ppt2Pdf(String originFilePath, String destFilePath, boolean isA4) --ppt文件(ppt/pptx)轉pdf
originFilePath--源文件全路徑
destFilePath--目標存儲文件全路徑
isA4--true:使用A4大小紙張,false:跟源泉文件一樣大小
返回:true-轉換成功 false-轉換失敗
2.4 boolean word2Pdf(String originFilePath, String destFilePath) --word文件(doc/docx)轉pdf
originFilePath--源文件全路徑
destFilePath--目標存儲文件全路徑
返回:true-轉換成功 false-轉換失敗
2.5 int getOfficePageCount(String originPath, String fileName) --準確獲取office文件頁數,支持doc/docx/ppt/pptx/pdf等,xls和xlsx格式請使用2.2api生成pdf後調用此方法
originFilePath--源文件全路徑
fileName--文件名
返回:頁數
2.6 boolean imgToA4Pdf(String[] imgArr, String destFilePath) --圖片轉pdf,每張圖片分別在一張A4紙上,自動縮放至最佳比例
imgArr--圖片路徑,例如["d:\\1.png","d:\\2.png"]
destFilePath--目標存儲pdf文件全路徑
返回:true-轉換成功 false-轉換失敗
3.使用示例:
package com.test;
import com.kevy.office.util.KevyOffice;
public class Test {
public static void main(String[] args) {
String originPath = "C:\\Users\\Administrator\\Desktop\\demo\\doc.doc";
String destPath = "C:\\Users\\Administrator\\Desktop\\demo\\dest.pdf";
System.out.println(KevyOffice.getOfficePageCount(originPath, "doc.doc"));
boolean result = KevyOffice.convertOfficeToPdf(originPath, destPath);
if (result) {
System.out.println("轉換成功");
}
}
}
4.如果最終部署到linux服務器,需要把windows電腦字體添加到linux服務器字體中,然後重啓服務,怎樣把windows字體添加到linux網上太多了,這裏不贅述了
jar包地址:https://download.csdn.net/download/c5211314963/12492983
注:本jar包僅用於學習交流,其他用途責任自負。