java程序office转PDF文件实现在线预览
所需jar包
1.aspose.slides-19.3.jar
2.aspose-cells-8.5.2.jar
3.aspose-words-18.6-jdk16-crack.jar
转换工具类
package com.risen.sydj.util;
import com.aspose.cells.PdfSaveOptions;
import com.aspose.cells.Workbook;
import com.aspose.slides.Presentation;
import com.aspose.words.Document;
import java.io.*;
/**
* @author admin
*/
@SuppressWarnings("ALL")
public class OfficeConvertPdf {
/**
* 转换文件类型
*/
@SuppressWarnings("AlibabaEnumConstantsMustHaveComment")
protected static enum FileTypeEnum {
/**
* WORD
*/
WORD,
/**
* EXCEL
*/
EXCEL,
/**
* PPT
*/
PPT
}
/**
* @param type
* @return
*/
private static boolean getLicense(FileTypeEnum type) {
boolean result = false;
try {
// 凭证
InputStream license = OfficeConvertPdf.class.getClassLoader().getResourceAsStream("\\com\\gs\\base\\config\\license.xml");
switch (type) {
case WORD:
new com.aspose.words.License().setLicense(license);
break;
case EXCEL:
new com.aspose.cells.License().setLicense(license);
break;
case PPT:
new com.aspose.slides.License().setLicense(license);
break;
default:
new com.aspose.words.License().setLicense(license);
break;
}
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
* word转pdf
*
* @param docPath
*/
public String docConvertPdf(String docPath) {
String pdfPath = docPath.substring(0, docPath.lastIndexOf(".")) + ".pdf";
if (!getLicense(FileTypeEnum.WORD)) {
System.out.println("doc2pdf,解析水印失败,请重试");
return pdfPath;
}
try {
File excelFile = new File(docPath);
if (excelFile.exists()) {
long old = System.currentTimeMillis();
Document convertDoc = new Document(docPath);
convertDoc.save(pdfPath, com.aspose.words.SaveFormat.PDF);
long now = System.currentTimeMillis();
System.out.println("转换成功,共耗时:" + ((now - old) / 1000.0) + "秒");
}
} catch (Exception e) {
e.printStackTrace();
}
return pdfPath;
}
/**
* excel转pdf
*
* @param excelPath
*/
public String excelConvertPdf(String excelPath) {
String pdfPath = excelPath.substring(0, excelPath.lastIndexOf(".")) + ".pdf";
if (!getLicense(FileTypeEnum.EXCEL)) {
return pdfPath;
}
try {
File excelFile = new File(excelPath);
if (excelFile.exists()) {
long old = System.currentTimeMillis();
Workbook convertExcel = new Workbook(excelPath);
PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();
pdfSaveOptions.setOnePagePerSheet(true);
convertExcel.save(pdfPath, pdfSaveOptions);
long now = System.currentTimeMillis();
System.out.println("转换成功,共耗时:" + ((now - old) / 1000.0) + "秒");
}
} catch (Exception e) {
e.printStackTrace();
}
return pdfPath;
}
/**
* ppt转pdf
*
* @param excelPath
*/
public String pptConvertPdf(String pptPath) {
String pdfPath = pptPath.substring(0, pptPath.lastIndexOf(".")) + ".pdf";
if (!getLicense(FileTypeEnum.PPT)) {
return pdfPath;
}
try {
File excelFile = new File(pptPath);
if (excelFile.exists()) {
long old = System.currentTimeMillis();
Presentation convertPpt = new Presentation(pptPath);
convertPpt.save(pdfPath, com.aspose.slides.SaveFormat.Pdf);
long now = System.currentTimeMillis();
System.out.println("转换成功,共耗时:" + ((now - old) / 1000.0) + "秒");
}
} catch (Exception e) {
e.printStackTrace();
}
return pdfPath;
}
private static OfficeConvertPdf officeConvertPdf;
/**
* 获取Doc2HtmlUtil实例
*/
public static void main(String[] args) {
OfficeConvertPdf officeConvertPdf = new OfficeConvertPdf();
officeConvertPdf.docConvertPdf("D:/test.doc");
}
}
调用并向页面输出
/*** 转换文件成pdf */
public void file2pdf(String filePath) throws IOException {
String type= filePath.substring(filePath.lastIndexOf("."));
HttpServletResponse response = ServletActionContext.getResponse();
if(".pdf".equals(type)||".PDF".equals(type)){
FileInputStream inputStream = null;
ServletOutputStream outputStream = null;
try {
File file1 = new File(filePath);
inputStream = new FileInputStream(file1);
outputStream = response.getOutputStream();
byte[] bytes = new byte[1024 * 10];
int len = 0;
while ((len = inputStream.read(bytes)) != -1) {
outputStream.write(bytes,0,len);
}
outputStream.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
inputStream.close();
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}else{
OfficeConvertPdf officeConvertPdf = new OfficeConvertPdf();
String pdfPath = null;
if(".doc".equals(type)||".docx".equals(type)){
pdfPath = officeConvertPdf.docConvertPdf(filePath);
}else if(".xls".equals(type)||".xlsx".equals(type)){
pdfPath = officeConvertPdf.excelConvertPdf(filePath);
}else if(".ppt".equals(type)||".pptx".equals(type)){
pdfPath = officeConvertPdf.pptConvertPdf(filePath);
}else if(".txt".equals(type)){
pdfPath = officeConvertPdf.docConvertPdf(filePath);
}
FileInputStream inputStream = null;
ServletOutputStream outputStream = null;
File file1 = null;
try {
file1 = new File(pdfPath);
inputStream = new FileInputStream(file1);
outputStream = response.getOutputStream();
byte[] bytes = new byte[1024 * 10];
int len = 0;
while ((len = inputStream.read(bytes)) != -1) {
outputStream.write(bytes,0,len);
}
outputStream.flush();
} catch (IOException e) {
if (file1.exists()){
file1.delete();
}
e.printStackTrace();
} finally {
try {
inputStream.close();
outputStream.close();
if (file1.exists()){
file1.delete();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
页面预览可使用pdf.js
pdf.js下载 http://mozilla.github.io/pdf.js/
项目中引入pdfjs
更改pdfjs中文件 view.js
代码: var DEFAULT_URL = ‘’; 默认路径置为空.
页面代码
<html>
<head>
<meta charset="UTF-8">
<title>Document</title>
<script src="${ctxPath}/static/js/plugins/pdfjs/buid/pdf.js"></script>
<script>
function showPdf(){ window.open('${ctxPath}/static/js/plugins/pdfjs/web/viewer.html? file='+encodeURIComponent(Feng.ctxPath + '/person/showpdf?fileId=123'),"pdf"); } function showHtml() { $("#showHtml").load(Feng.ctxPath + '/person/readHtml?fileId=123'); }
</script>
</head>
<body>
<ul><li><a href="#" οnclick="showPdf()">pdf预览</a> </li></ul><!--<ul><li><a href="#" οnclick="showHtml()">html预览</a> </li></ul>--><div id="showHtml"></div>
</body>
</html>