加入maven依赖
<!-- word2pdf -->
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-words</artifactId>
<scope>system</scope>
<version>15.8.0-jdk16</version>
<systemPath>${project.basedir}/lib/aspose-words-15.8.0-jdk16.jar</systemPath>
</dependency>
<!-- excel2pdf -->
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-cells</artifactId>
<scope>system</scope>
<version>8.5.2</version>
<systemPath>${project.basedir}/lib/aspose-cells-8.5.2.jar</systemPath>
</dependency>
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-slides</artifactId>
<scope>system</scope>
<version>15.9.0</version>
<systemPath>${project.basedir}/lib/aspose.slides-15.9.0.jar</systemPath>
</dependency>
转换工具类
package com.giseye.common.utils;
import cn.hutool.core.date.StopWatch;
import cn.hutool.core.io.FileUtil;
import com.aspose.cells.PdfSaveOptions;
import com.aspose.cells.Workbook;
import com.aspose.slides.Presentation;
import com.aspose.words.Document;
import com.aspose.words.License;
import lombok.extern.slf4j.Slf4j;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.CompletableFuture;
/**
*
*/
@Slf4j
public class AsposeUtil {
private static boolean getLicense() {
boolean result = false;
try {
InputStream is = AsposeUtil.class.getClassLoader().getResourceAsStream("license.xml");
License aposeLic = new License();
aposeLic.setLicense(is);
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
private static boolean getLicenseExcel() {
boolean result = false;
try {
System.out.println(AsposeUtil.class.getClassLoader().getResource("").getPath());
InputStream is = AsposeUtil.class.getClassLoader().getResourceAsStream("license.xml");
com.aspose.cells.License aposeLic = new com.aspose.cells.License();
aposeLic.setLicense(is);
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
private static InputStream license;
public static boolean getLicensePPT() {
boolean result = false;
try {
license = AsposeUtil.class.getClassLoader().getResourceAsStream("\\license.xml");
com.aspose.slides.License aposeLic = new com.aspose.slides.License();
aposeLic.setLicense(license);
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
public static void doc2pdf(String wordPath, String pdfPath) {
// 验证License 若不验证则转化出的pdf文档会有水印产生
if (!getLicense()) {
return;
}
try {
StopWatch sw = new StopWatch();
//新建一个pdf文档
File file = new File(pdfPath);
FileOutputStream os = new FileOutputStream(file);
//Address是将要被转化的word文档
Document doc = new Document(wordPath);
//全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF, EPUB, XPS, SWF 相互转换
doc.save(os, com.aspose.words.SaveFormat.PDF);
long now = System.currentTimeMillis();
os.close();
//转化用时
} catch (Exception e) {
e.printStackTrace();
}
}
public static boolean ppt2pdf(String pptPath, String pdfPath) {
// 验证License 若不验证则转化出的pdf文档会有水印产生
if (!getLicensePPT()) {
return false;
}
try {
//新建一个pdf文档
File file = new File(pdfPath);
FileOutputStream os = new FileOutputStream(file);
Presentation ppt = new Presentation(pptPath);
ppt.save(os, com.aspose.slides.SaveFormat.Pdf);
os.close();
//转化用时
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
public static boolean excel2pdf(String excelPath, String pdfPath) {
// 验证License 若不验证则转化出的pdf文档会有水印产生
if (!getLicenseExcel()) {
return false;
}
try {
// 原始excel路径
Workbook wb = new Workbook(excelPath);
PdfSaveOptions xlsSaveOption = new PdfSaveOptions();
xlsSaveOption.setAllColumnsInOnePagePerSheet(true);
FileOutputStream fileOS = new FileOutputStream(pdfPath);
wb.save(fileOS, xlsSaveOption);
fileOS.close();
//转化用时
return true;
} catch (Exception e) {
e.printStackTrace();
return true;
}
}
/**
* 文件转pdf
*
* @param srcPath 原文件路径
* @param pdfPath pdf保存路径
*/
public static void convertToPdf(String srcPath, String pdfPath) {
File file = new File(srcPath);
ConvertFactory convertFactory = ConvertFactoryBuilder.build(FileUtil.getSuffix(file.getName()));
if (Objects.isNull(convertFactory)) {
log.error("文件:{} 不支持pdf转换", srcPath);
return;
}
CompletableFuture.runAsync(() -> {
convertFactory.execution(srcPath, pdfPath);
});
}
/**
* 文件转pdf
* pdf保存路径和原文件路径保存一致
* @param srcPath
*/
public static void convertToPdf(String srcPath) {
File file = new File(srcPath);
if (!file.exists()) {
return;
}
String pdfPath = file.getParent() + "/" + FileUtil.getPrefix(file.getName()) + ".pdf";
convertToPdf(srcPath, pdfPath);
}
}
class ConvertFactoryBuilder {
static Map<String, ConvertFactory> methodMap = new HashMap<String, ConvertFactory>() {
{
put("doc", AsposeUtil::doc2pdf);
put("docx", AsposeUtil::doc2pdf);
put("xlsx", AsposeUtil::excel2pdf);
put("xls", AsposeUtil::excel2pdf);
put("ppt", AsposeUtil::ppt2pdf);
}
};
public static ConvertFactory build(String suffix) {
return methodMap.get(suffix);
}
}
@FunctionalInterface
interface ConvertFactory {
/**
* 文件转换为PDF
*
* @param srcPath 源文件路径
* @param pdfPath pdf保存路径
*/
void execution(String srcPath, String pdfPath);
}
在resources包下新建 license.xml,内容如下
<License>
<Data>
<Products>
<Product>Aspose.Total for Java</Product>
<Product>Aspose.Words for Java</Product>
</Products>
<EditionType>Enterprise</EditionType>
<SubscriptionExpiry>20991231</SubscriptionExpiry>
<LicenseExpiry>20991231</LicenseExpiry>
<SerialNumber>8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7</SerialNumber>
</Data>
<Signature>sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU=</Signature>
</License>