aspose实现office文件转换pdf文件
jar:
- aspose.slides-19.3.jar
- aspose-cells-8.5.2.jar
- aspose-words-16.8.0.jar
- license.xml
链接:https://pan.baidu.com/s/11rBQxPYTn9x9WOc1OBZjUQ
提取码:x2b4
复制这段内容后打开百度网盘手机App,操作更方便哦
license.xml放入resources目录下
验证license.xml
private static InputStream license;
//校验license
public boolean judgeLicense() {
boolean result = false;
try {
license = AsposeUtilImpl.class.getClassLoader().getResourceAsStream("license.xml"); //文件路径
if (license != null) {
License aposeLic = new License();
aposeLic.setLicense(license);
result = true;
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
外部类调用方法
/**
*
* @param filePath office路径
* @param pdfPath pdf输出路径
* @param type office文件类型
* @return
* @throws OutOfMemoryError
*/
public boolean trans(String filePath, String pdfPath, String type) throws OutOfMemoryError{
try {
if (!judgeLicense()) {
logger.error("license错误");
}
File file = new File(pdfPath);
return toPdf(file, filePath, type);
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
判断文件类型
private boolean toPdf(File file, String filePath, String type) throws OutOfMemoryError{
boolean result = false;
switch (type) {
case "doc":
case "docx":
result = docToPdf(file, filePath);
break;
case "xls":
case "xlsx":
result = xlsToPdf(file, filePath);
break;
case "ppt":
case "pptx":
result = pptToPdf(file, filePath);
break;
default:
System.out.println("暂不支持该类型:" + type);
}
return result;
}
doc转pdf
private boolean docToPdf(File file, String filePath) throws OutOfMemoryError{
FileOutputStream os = null;
Document doc;
boolean result = false;
try {
//linux下转换乱码,安装windows字符集("cd /usr/share/fonts" 下安装)
if(System.getProperty("os.name").toLowerCase().contains("linux")){
FontSettings.getDefaultInstance().setFontsFolder(File.separator + "usr"
+ File.separator + "share" + File.separator + "fonts" + File.separator, true);
}
os = new FileOutputStream(file);
doc = new Document(filePath);
doc.save(os, com.aspose.words.SaveFormat.PDF);
result = true;
} catch (Exception e) {
logger.error("doc格式转换pdf格式出错:" + e.getStackTrace());
} finally {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
result = false;
}
}
return result;
}
xsl转换pdf
private boolean xlsToPdf(File file, String filePath) throws OutOfMemoryError{
FileOutputStream os = null;
boolean result = false;
try {
os = new FileOutputStream(file);
Workbook wb = new Workbook(filePath);
wb.save(os, com.aspose.cells.SaveFormat.PDF);
result = true;
} catch (Exception e) {
logger.error("xls格式转换pdf格式出错:" + e.getStackTrace());
} finally {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
result = false;
logger.error("xls格式StringBuffer关闭错误:" + e.getStackTrace());
}
}
return result;
}
ppt转换pdf
private boolean pptToPdf(File file, String filePath) throws OutOfMemoryError{
BufferedOutputStream out = null;
boolean result = false;
try {
out = new BufferedOutputStream(new FileOutputStream(file));
Presentation pres = new Presentation(filePath);// 输入pdf路径
pres.save(out, com.aspose.words.SaveFormat.PDF);
result = true;
} catch (Exception e) {
logger.error("ppt格式转换pdf格式出错:" + e.getStackTrace());
} finally {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
result = false;
logger.error("ppt格式StringBuffer关闭错误:" + e.getStackTrace());
}
}
return result;
}
重点!!!
- doc.save(os, com.aspose.words.SaveFormat.PDF);
- wb.save(os, com.aspose.cells.SaveFormat.PDF);
- pres.save(os,com.aspose.slides.SaveFormat.Pdf);