一、jar包下载和依赖引入
依赖jar百度云地址:https://pan.baidu.com/s/1_7Q5FLhdY7VYy89QTsgIQQ
提取码:bckw
com.aspose ToPDF system ${project.basedir}/src/main/resources/jar/aspose-cells-20.7.jar 1.0 com.aspose worldToPDF system ${project.basedir}/src/main/resources/jar/aspose-words-18.5.0718-jdk16.jar 1.0 com.aspose ppt2Pdf system ${project.basedir}/src/main/resources/jar/aspose-slides-19.6.jar 1.0
二、OfficePdfUtil 工具类
public class OfficePdfUtil {
public static boolean getLicense(){
boolean result = false;
try {
String license =
"n" +
" n" +
" n" +
" Aspose.Total for Java n" +
" Aspose.Words for Java n" +
" n" +
" Enterprise n" +
" 20991231 n" +
" 20991231 n" +
" 8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7 n" +
" n" +
" sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU= n" +
" ";
InputStream is = new ByteArrayInputStream(license.getBytes("UTF-8"));
if(is!=null){
License aposeLic = new License();
aposeLic.setLicense(is);
result = true;
is.close();
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
public static boolean docTopdf(String inPath, String outPath) {
boolean ret=false;
if (!getLicense()) { // 验证License 若不验证则转化出的pdf文档有水印
// throw new Exception("com.aspose.words lic ERROR!");
return ret;
}
System.out.println(inPath + " -> " + outPath);
try {
long old = System.currentTimeMillis();
File file = new File(outPath);
FileOutputStream os = new FileOutputStream(file);
document doc = new document(inPath); // word文档
// 支持RTF HTML,Opendocument, PDF,EPUB, XPS转换
doc.save(os, SaveFormat.PDF);
long now = System.currentTimeMillis();
System.out.println("convert OK! " + ((now - old) / 1000.0) + "秒");
return true;
} catch (Exception e) {
e.printStackTrace();
return ret;
}
}
public static Boolean excelToPdf(String excelFilePath, String pdfFilePath){
//验证License
System.out.println(excelFilePath + " -> " + pdfFilePath);
if (!getLicense()){
return false;
}
try {
Workbook wb = new Workbook(excelFilePath);
File pdfFile = new File(pdfFilePath);
FileOutputStream fileOS = new FileOutputStream(pdfFile);
PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();
pdfSaveOptions.setAllColumnsInOnePagePerSheet(true);
// wb.save(fileOS, SaveFormat.PDF);
// pdfSaveOptions.setonePagePerSheet(true);
wb.save(fileOS, pdfSaveOptions);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
public static boolean pptxTopdf(String sourcePath, String savePath){
if (!getLicense()) { // 验证License 若不验证则转化出的pdf文档有水印
// throw new Exception("com.aspose.words lic ERROR!");
return false;
}
try {
//读取ppt文件
FileInputStream fileInput = new FileInputStream(sourcePath);
Presentation pres = new Presentation(fileInput);
//指定输出路径
FileOutputStream outputStream = new FileOutputStream(new File(savePath));
//输出
pres.save(outputStream, SaveFormat.Pdf);
outputStream.close();
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
}



