package com.bangzheng.common.bzcommon;
import com.aspose.words.Document;
import com.aspose.words.FontSettings;
import com.aspose.words.License;
import com.aspose.words.SaveFormat;
import com.jfinal.kit.PathKit;
import org.apache.logging.log4j.core.config.plugins.util.ResolverUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
/**
-
@author Administrator
*/
public class Doc2Pdf {/**
- 日志
*/
private static final Logger logger = LoggerFactory.getLogger(Doc2Pdf.class);
/public static void main(String[] args) {
Doc2Pdf.doc2pdf(“D:/国家总体应急预案.docx”, “F:/XYpxyl/”, “asd”);
}/
public static boolean getLicense() {
boolean result = false;
try {
// license.xml应放在…\WebRoot\WEB-INF\classes路径下
InputStream is = ResolverUtil.Test.class.getClassLoader().getResourceAsStream(“license.xml”);
License aposeLic = new License();
aposeLic.setLicense(is);
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}/**
- word转pdf
- @param inPath
- @param outPath
- @param fileName
- @return
/
public static String doc2pdf(String inPath, String outPath, String fileName) {
// 验证License 若不验证则转化出的pdf文档会有水印产生
if (!getLicense()) {
return null;
}
try {
long old = System.currentTimeMillis();
//上传地址路径
String uploadPath = PathKit.getWebRootPath() + File.separator + outPath;
File file = new File(uploadPath + File.separator + fileName.substring(0, fileName.lastIndexOf(".")) + “.pdf”);
FileOutputStream os = new FileOutputStream(file);
//设置一个字体目录
/ FontSettings.setFontsFolder("/fonts/chinese/consola.ttf", false);/
//设置多个字体目录
/ String[] fonts = {"/fonts/chinese/consola.ttf", “/fonts/chinese/simsun.ttf”, “/fonts/chinese/msyh.ttf”, “/fonts/chinese/msyhbd.ttf”, “/fonts/chinese/simfang.ttf”, “/fonts/chinese/simhei.ttf”, “/fonts/chinese/simkai.ttf”, “/fonts/chinese/simsunb.ttf”};
FontSettings.setFontsFolders(fonts, true);*/
//Address是将要被转化的word文档
Document doc = new Document(inPath);
//全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF, EPUB, XPS, SWF 相互转换
doc.save(os, SaveFormat.PDF);
os.close();
long now = System.currentTimeMillis();
//转化用时
logger.info(fileName + “转pdf共耗时:” + ((now - old) / 1000.0) + “秒”);
os.close();
return outPath + File.separator + file.getName();
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
- 日志
}