ASPOSE office系列转换PDF文件的正确姿势
相关jar和license.xml下载地址
1、jar
序号 | jar名称 | 对应文件 |
---|
1 | aspose.slides | PPT |
2 | aspose-cells | EXCEL |
3 | aspose-words | WORD |
2、pom依赖
<dependency>
<groupId>acom.aspose.cells</groupId>
<artifactId>aspose-cells-jdk16</artifactId>
<version>15.8.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/resources/lib/aspose-cells-jdk16-8.5.2.jar</systemPath>
</dependency>
<dependency>
<groupId>aspose-words</groupId>
<artifactId>aspose-words</artifactId>
<version>15.8.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/resources/lib/aspose-words-jdk16-15.8.0.jar</systemPath>
</dependency>
<dependency>
<groupId>aspose.slides</groupId>
<artifactId>slides</artifactId>
<version>19.3</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/resources/lib/aspose.slides-19.3.jar</systemPath>
</dependency>
3、代码
import java.io.*;
import java.util.Arrays;
import cn.hutool.core.collection.CollUtil;
import com.aspose.cells.Workbook;
import com.aspose.words.Document;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.aspose.slides.Presentation;
public class Office2PdfUtil {
private static final Logger log = LoggerFactory.getLogger(Office2PdfUtil.class);
public static void main(String[] args) {
String filename = "测试.doc";
String inPath = "C:\\111\\pdf\\测试.doc";
String outPath = "C:\\111\\pdf\\测试.pdf";
Office2PdfUtil.toPdf(filename, inPath, outPath);
}
private static final String[] WORD = {"doc", "docx", "wps", "wpt", "txt"};
private static final String[] EXCEL = {"xls", "xlsx", "et", "xlsm"};
private static final String[] PPT = {"ppt", "pptx"};
private static final String[] PDF = {"pdf"};
private static final String[] IMG = {"bmp", "jpg", "png", "tif", "gif", "pcx", "tga", "exif", "fpx", "svg", "psd", "cdr", "pcd", "dxf", "ufo", "eps", "ai", "raw", "WMF", "webp", "avif", "apng"};
private static final String TYPE_UNSUPPORT = "不支持的格式";
private static final String TYPE_LICENSE = "证书过期";
private static final String TYPE_SUCCESS = "转换成功";
private static final String TYPE_ERROR = "转换失败";
private static final String TYPE_WORD = "WORD";
private static final String TYPE_EXCEL = "EXCEL";
private static final String TYPE_PPT = "PPT";
private static final String TYPE_PDF = "PDF";
private static final String TYPE_IMG = "IMG";
private static String getType(String fileName) {
String suffix = fileName.substring(fileName.lastIndexOf(".") + 1);
if (CollUtil.contains(Arrays.asList(WORD), suffix)) {
return TYPE_WORD;
} else if (CollUtil.contains(Arrays.asList(EXCEL), suffix)) {
return TYPE_EXCEL;
} else if (CollUtil.contains(Arrays.asList(PPT), suffix)) {
return TYPE_PPT;
} else if (CollUtil.contains(Arrays.asList(PDF), suffix)) {
return TYPE_PDF;
} else if (CollUtil.contains(Arrays.asList(IMG), suffix)) {
return TYPE_IMG;
} else {
return TYPE_UNSUPPORT;
}
}
public static String toPdf(String fileName, String inPath, String outPath) {
long old = System.currentTimeMillis();
try {
String type = getType(fileName);
switch (type) {
case TYPE_UNSUPPORT:
return TYPE_UNSUPPORT;
case TYPE_PDF:
return TYPE_UNSUPPORT;
case TYPE_IMG:
return TYPE_UNSUPPORT;
default:
break;
}
if (!getLicense(type)) {
log.info("License验证失败");
return TYPE_LICENSE;
}
log.info("License验证成功........");
log.info("开始本次转换........");
File file = new File(outPath);
FileOutputStream os = new FileOutputStream(file);
switch (type) {
case TYPE_WORD:
wordToPdfStream(inPath, os);
break;
case TYPE_EXCEL:
excelToPdfStream(inPath, os);
break;
case TYPE_PPT:
pptToPdfStream(inPath, os);
break;
default:
break;
}
long now = System.currentTimeMillis();
log.info("转换成功,共耗时:" + ((now - old) / 1000.0) + "秒........");
} catch (Exception e) {
log.error("转换失败", e);
return TYPE_ERROR;
}
return TYPE_SUCCESS;
}
private static void wordToPdfStream(String inPath, FileOutputStream os) throws Exception {
Document doc = new Document(inPath);
doc.save(os, com.aspose.words.SaveFormat.PDF);
}
private static void excelToPdfStream(String inPath, FileOutputStream os) throws Exception {
Workbook excel = new Workbook(inPath);
excel.save(os, com.aspose.cells.SaveFormat.PDF);
}
private static void pptToPdfStream(String inPath, FileOutputStream os) throws Exception {
Presentation ppt = new Presentation(inPath);
ppt.save(os, com.aspose.slides.SaveFormat.Pdf);
}
public static boolean getLicense() {
boolean result = false;
try {
InputStream is = Excel2PdfUtils.class.getClassLoader().getResourceAsStream("\\license.xml");
com.aspose.cells.License aposeLic = new com.aspose.cells.License();
aposeLic.setLicense(is);
result = true;
} catch (Exception e) {
log.error("License失效", e);
}
return result;
}
private static boolean getLicense(String type) {
boolean result = false;
try {
String license = "<License>\n" +
" <Data>\n" +
" <Products>\n" +
" <Product>Aspose.Total for Java</Product>\n" +
" </Products>\n" +
" <EditionType>Enterprise</EditionType>\n" +
" <SubscriptionExpiry>20991231</SubscriptionExpiry>\n" +
" <LicenseExpiry>20991231</LicenseExpiry>\n" +
" <SerialNumber>8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7</SerialNumber>\n" +
" </Data>\n" +
" <Signature>sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU=</Signature>\n" +
"</License>";
InputStream is = new ByteArrayInputStream(license.getBytes("UTF-8"));
if (type.equals(TYPE_WORD)) {
com.aspose.words.License aposeLic = new com.aspose.words.License();
aposeLic.setLicense(is);
result = true;
} else if (type.equals(TYPE_EXCEL)) {
com.aspose.cells.License aposeLic = new com.aspose.cells.License();
aposeLic.setLicense(is);
result = true;
} else if (type.equals(TYPE_PPT)) {
com.aspose.slides.License aposeLic = new com.aspose.slides.License();
aposeLic.setLicense(is);
result = true;
} else {
result = false;
}
} catch (Exception e) {
result = false;
log.error("License失效", e);
}
return result;
}
}