个人经验,其他转换工具麻烦或效果不好,且置入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网上太多了,这里不赘述了(可参考https://www.cnblogs.com/superxff/p/9809454.html)
jar包地址:https://download.csdn.net/download/c5211314963/12492983
注:本jar包仅用于学习交流,其他用途责任自负。