1,测试环境springboot,jdk1,8,aspose-cells-8.5.2.jar 用于转换xls,aspose-words-16.8.0-jdk16.jar 用于转换doc
2,所用jar,签名百度网盘地址
链接:https://pan.baidu.com/s/14zwqI4d5-WweXKki8LyI-A
提取码:0kgc
3,下载jar加入本地maven仓库
控制台执行,命令如下:(将install-file值改成自己存放jar的路径)
mvn install:install-file "-Dfile=aspose-cells-8.5.2.jar" "-DgroupId=com.aspose" "-DartifactId=aspose-cells" "-Dversion=8.5.2" "-Dpackaging=jar"
mvn install:install-file "-Dfile=D:\Program Files\lib\aspose-words-16.8.0-jdk16.jar" "-DgroupId=com.aspose" "-DartifactId=aspose-words" "-Dversion=16.8.0" "-Dpackaging=jar"
4,引入jar
<!-- xls转pdf -->
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-cells</artifactId>
<version>8.5.2</version>
</dependency>
<!-- word转pdf -->
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-words</artifactId>
<version>16.8.0</version>
</dependency>
5,将下载的license.xml放置项目resources下
6,上代码
package com.zhw.common.utils;
import com.aspose.cells.PdfSaveOptions;
import com.aspose.cells.Workbook;
import com.aspose.words.Document;
import com.aspose.words.License;
import com.aspose.words.SaveFormat;
import java.io.FileInputStream;
import java.io.FileOutputStream;
/**
* 文件转换PDF支持doc,docx,xls,xlsx
*/
public class PdfUtils {
/**
* 文件转换PDF
* @param original_file_path 源文件路径
* @param transform_file_path 输出路径
* @param fileType 转换类型 0:doc转pdf,1:xls转pdf
* @return
*/
public static boolean fileToPdf(String original_file_path, String transform_file_path,String fileType) {
try (
FileInputStream in = new FileInputStream(original_file_path);
FileOutputStream out = new FileOutputStream(transform_file_path)
) {
//设置签名,若不设置签名转换后的pdf会有水印
if(!getLicense(fileType)){
System.out.println("设置签名失败");
return false;
}
if(fileType.equals("0")){
//为doc
//获取doc对象
Document doc = new Document(in);
//输出
doc.save(out, SaveFormat.PDF);
}else {
//为xls
//生成工作表
Workbook workbook = new Workbook(in);
PdfSaveOptions saveOptions = new PdfSaveOptions();
//重点,设置所有列放在一页里,会自动适应宽度
saveOptions.setAllColumnsInOnePagePerSheet(true);
//输出
workbook.save(out,saveOptions);
}
System.out.println("转换成功");
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 设置签名,若不设置签名,转换后的pdf将会有水印
*
* @return
*/
public static boolean getLicense(String type) {
boolean res = false;
ClassLoader loader = Thread.currentThread().getContextClassLoader();
try {
FileInputStream in = new FileInputStream(loader.getResource("license.xml").getPath());
if(type.equals("0")){
License aposeLic = new License();
aposeLic.setLicense(in);
res = true;
}else {
com.aspose.cells.License license = new com.aspose.cells.License();
license.setLicense(in);
res = true;
}
} catch (Exception e) {
e.printStackTrace();
}
return res;
}
public static void pptToPdf(String from, String to) throws Exception {
long start = System.currentTimeMillis();
// 验证License
if (!getLicense()) {
return;
}
File file = new File(to);// 输出pdf路径
Presentation pres = new Presentation(from);//输入ppt路径
FileOutputStream fileOS = new FileOutputStream(file);
//IFontsManager fontsManager = pres.getFontsManager();
pres.save(fileOS, SaveFormat.Pdf);
fileOS.close();
System.out.println("转换完成工耗时" + (System.currentTimeMillis() - start) + "毫秒");
}
public static void main(String[] args) {
//测试
//源文件路径
String original_file_path = "C:\\Users\\Administrator\\Desktop\\1611640020354生活垃圾科室统计.xlsx";
//输出路径
String transform_file_path = "C:\\Users\\Administrator\\Desktop\\1.pdf";
boolean b = fileToPdf(original_file_path,transform_file_path,"1");
System.out.println("转换成功?"+b);
}
}