实现想法是将文档转换为pdf进行在线浏览,上代码
pom
com.aspose
aspose-words
15.8.0
com.aspose
aspose-cells
8.5.2
com.aspose
aspose-slides
15.9.0
工具类
主要调用
officeToPdf(uploadPath +relativePath + fileName, parsePath, viewModel.getFileExt(), parsePath + fileName);
package com.demo.Utils;
import com.aspose.cells.License;
import com.aspose.cells.Workbook;
import com.aspose.slides.Presentation;
import com.aspose.slides.SaveFormat;
import com.aspose.words.Document;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
/**
* @ClassName:FileToPdfUtils
* @Description: 文件转pdf工具类
* @Author: liuhm
* @Date: 2019/10/10 13:38
*/
public class FileToPdfUtils {
/**
* @description: 验证License
* @params:
* @return:
* @author: liuhm
* @Date: 2019/10/10 13:40
*/
public static boolean getLicense() {
boolean result = false;
try {
// license.xml应放在..\WebRoot\WEB-INF\classes路径下
InputStream is = FileToPdfUtils.class.getClassLoader().getResourceAsStream("license.xml");
License aposeLic = new License();
aposeLic.setLicense(is);
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
* @description: excel转pdf
* @params: Address:源文件地址,filepathWithoutFileName:不含文件路径,filepath:转换后文件路径
* @return:
* @author: liuhm
* @Date: 2019/10/10 13:41
*/
public static void excel2pdf(String Address,String filepathWithoutFileName,String fileExt,String filepath) {
// 验证License 若不验证则转化出的pdf文档会有水印产生
if (!getLicense()) {
return;
}
try {
long old = System.currentTimeMillis();
// 原始excel路径
Workbook wb = new Workbook(Address);
//验证路径
try {
if (!(new File(filepathWithoutFileName).isDirectory())) {
new File(filepathWithoutFileName).mkdirs();
}
} catch (SecurityException e) {
e.printStackTrace();
}
// 输出路径
File pdfFile = new File(filepath.replace(fileExt,"pdf"));
FileOutputStream fileOS = new FileOutputStream(pdfFile);
wb.save(fileOS,com.aspose.cells.SaveFormat.PDF);
long now = System.currentTimeMillis();
//System.out.println("共耗时:" + ((now - old) / 1000.0) + "秒");
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* @description: ppt转pdf
* @params: Address:源文件地址,filepathWithoutFileName:不含文件路径,filepath:转换后文件路径
* @return:
* @author: liuhm
* @Date: 2019/10/10 13:46
*/
public static void ppt2pdf(String Address,String filepathWithoutFileName,String fileExt, String filepath) {
// 验证License 若不验证则转化出的pdf文档会有水印产生
if (!getLicense()) {
return;
}
try {
long old = System.currentTimeMillis();
//验证路径
try {
if (!(new File(filepathWithoutFileName).isDirectory())) {
new File(filepathWithoutFileName).mkdirs();
}
} catch (SecurityException e) {
e.printStackTrace();
}
//新建一个空白pdf文档
File file = new File(filepath.replace(fileExt, "pdf"));
//输入pdf路径
Presentation pres = new Presentation(Address);
FileOutputStream fileOS = new FileOutputStream(file);
pres.save(fileOS, SaveFormat.Pdf);
fileOS.close();
long now = System.currentTimeMillis();
//转化用时
System.out.println("共耗时:" + ((now - old) / 1000.0) + "秒");
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* @description: doc转pdf
* @params: Address:源文件地址,filepathWithoutFileName:不含文件路径,filepath:转换后文件路径
* @return:
* @author: liuhm
* @Date: 2019/10/10 13:46
*/
public static void doc2pdf(String Address, String filepathWithoutFileName, String fileExt, String filepath) {
// 验证License 若不验证则转化出的pdf文档会有水印产生
if (!getLicense()) {
return;
}
try {
long old = System.currentTimeMillis();
//新建一个空白pdf文档
try {
if (!(new File(filepathWithoutFileName).isDirectory())) {
new File(filepathWithoutFileName).mkdirs();
}
} catch (SecurityException e) {
e.printStackTrace();
}
//输出路径
File file = new File(filepath.replace(fileExt, "pdf"));
FileOutputStream os = new FileOutputStream(file);
Document doc = new Document(Address);
doc.save(os, com.aspose.words.SaveFormat.PDF);
long now = System.currentTimeMillis();
//转化用时
//System.out.println("共耗时:" + ((now - old) / 1000.0) + "秒");
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* @description: 文件转pdf
* @params: path:源文件地址,filepathWithoutFileName:不含文件路径,filepath:转换后文件路径
* @return:
* @author: liuhm
* @Date: 2019/10/10 15:17
*/
public void officeToPdf(String path, String filepathWithoutFileName, String fileExt, String filePath) {
//office文档转pdf
//String pdfFileName = path.substring(0, path.lastIndexOf("."));
if ("doc".equals(fileExt) || "docx".equals(fileExt)) {
FileToPdfUtils.doc2pdf(path, filepathWithoutFileName, fileExt, filePath);
}
if ("xls".equals(fileExt) || "xlsx".equals(fileExt)) {
FileToPdfUtils.excel2pdf(path, filepathWithoutFileName, fileExt, filePath);
}
if ("ppt".equals(fileExt) || "pptx".equals(fileExt)) {
FileToPdfUtils.ppt2pdf(path, filepathWithoutFileName, fileExt, filePath);
}
}
}
license.xml 好像这个没什么用
Aspose.Total for Java
Aspose.Words for Java
Enterprise
20991231
20991231
8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7
sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU=
注意:如果部署版本后发现转pdf是乱码,请在该环境安装字体