java中word、excel、ppt转PDF文件
1、pom.xm引入依赖
<dependency>
<groupId> com.aspose</groupId>
<artifactId>aspose-cells</artifactId>
<version>8.5.2</version>
</dependency>
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-slides</artifactId>
<version>15.9.0</version>
</dependency>
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-words</artifactId>
<version>15.8.0</version>
</dependency>
2、转换工具类
import cn.hutool.core.util.IdUtil;
import com.aspose.cells.PdfSaveOptions;
import com.aspose.cells.Workbook;
import com.aspose.slides.Presentation;
import com.aspose.words.Document;
import com.aspose.words.SaveFormat;
import lombok.extern.slf4j.Slf4j;
import java.io.*;
@Slf4j
public class AsposeUtils {
/**
* 判断文件类型调用转换方法
* @param filePath
* @param pdfPath
* @throws Exception
* @return
*/
public static void officesToPdf(String filePath, String pdfPath, String fileType) throws Exception {
if (".doc".equals(fileType) || ".docx".equals(fileType)) {
wordToPdf(filePath, pdfPath);
} else if (".xls".equals(fileType) || ".xlsx".equals(fileType)) {
excelToPDF(filePath, pdfPath);
} else if ( ".ppt".equals(fileType) || ".pptx".equals(fileType)) {
pptToPDF(filePath, pdfPath);
}else{
throw new RuntimeException("暂不支持该类型:"+fileType);
}
}
/**
* wordToPdf
* @param wordPath
* @param pdfPath
*/
private static void wordToPdf(String wordPath,String pdfPath) {
// 验证License
if (!loadLicense(com.aspose.words.License.class)) {
return;
}
OutputStream out=null;
try {
Document doc = new Document(wordPath);// 原始word路径
File pdfFile = new File(pdfPath);// 输出路径
out = new FileOutputStream(pdfFile);
doc.save(out, SaveFormat.PDF);
} catch (Exception e) {
e.printStackTrace();
}finally {
if(out!=null){
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* excelToPDF
* @param filePath
* @param pdfPath
* @throws Exception
*/
private static void excelToPDF(String filePath, String pdfPath) {
if (!loadLicense(com.aspose.cells.License.class)) {
return;
}
OutputStream out=null;
try {
Workbook wb = new Workbook(filePath);// 原始excel路径
out= new FileOutputStream(pdfPath);
PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();
pdfSaveOptions.setOnePagePerSheet(true);
wb.save(out, pdfSaveOptions);
} catch (Exception e) {
e.printStackTrace();
}finally {
if(out!=null){
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* pptToPDF
* @param filePath
* @param pdfPath
* @throws Exception
*/
private static void pptToPDF(String filePath, String pdfPath) {
if (!loadLicense(com.aspose.slides.License.class)) {
return;
}
OutputStream out=null;
try {
Presentation pres = new Presentation(filePath);
out = new FileOutputStream(pdfPath);
pres.save(out,com.aspose.slides.SaveFormat.Pdf);
} catch (Exception e) {
e.printStackTrace();
}finally {
if(out!=null){
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static boolean loadLicense(Class clazz) {
boolean result = false;
try {
InputStream is = AsposeUtils.class.getClassLoader().getResourceAsStream("license.xml");
Object obj = clazz.newInstance();
clazz.getMethod("setLicense", InputStream.class).invoke(obj, is);
result = true;
} catch (Exception e) {
log.error(e.getMessage(),e);
}
return result;
}
}
3、license文件(放在项目classpath资源目录 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>
ok!!!


阅读排行


Copyright © 叮叮声的奶酪 版权所有
备案号:鄂ICP备17018671号-1