public class PDFUtils {
//校验license
private static boolean judgeLicense() {
boolean result = false;
// 21.6
try {
Class<?> aClass = Class.forName("com.aspose.words.zzXyu");
java.lang.reflect.Field zzYAC = aClass.getDeclaredField("zzZXG");
zzYAC.setAccessible(true);
java.lang.reflect.Field modifiersField = zzYAC.getClass().getDeclaredField("modifiers");
modifiersField.setAccessible(true);
modifiersField.setInt(zzYAC,zzYAC.getModifiers() & ~Modifier.FINAL);
zzYAC.set(null, new byte[] {76,73,67,69,78,83,69,68});
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
根据byte数组,生成文件
**/
public static void getFile(byte[] bfile, String filePath,String fileName) {
BufferedOutputStream bos = null;
FileOutputStream fos = null;
File file = null;
try {
File dir = new File(filePath);
if (!dir.exists() || dir.isDirectory()) {//判断文件目录是否存在
dir.mkdirs();
}
file = new File(filePath + "\\" + fileName+".pdf");
fos = new FileOutputStream(file);
bos = new BufferedOutputStream(fos);
bos.write(bfile);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (bos != null) {
try {
bos.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
if (fos != null) {
try {
fos.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
/**
* @param filePath pdf 缓存地址
* @param fileName 名称
* @param isDel 是否删除 filePath 地址缓存文件
* @param response
* @throws Exception
*/
public static void showPdf(String filePath,String fileName,Boolean isDel, HttpServletResponse response) throws Exception {
if (!judgeLicense()) {
System.out.println("license错误");
}
File file = new File(filePath);
if (file.exists()){
byte[] data = null;
FileInputStream input = new FileInputStream(file);
data = new byte[input.available()];
input.read(data);
response.getOutputStream().write(data);
input.close();
if(isDel) {
DeleteFileUtil.deleteFile(filePath);
}
}else{
return;
}
}
/**
* @param fileUrl url 网络地址
* @param response
* @throws Exception
*/
public static void showWordToPdf(String fileUrl,HttpServletResponse response) throws Exception {
if (!judgeLicense()) {
System.out.println("license错误");
}
try {
Document doc = new Document(fileUrl);
doc.save(response.getOutputStream(), SaveFormat.PDF);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void transWordToPdf(InputStream fileInputStream, OutputStream outputStream) {
if (!judgeLicense()) {
System.out.println("license错误");
}
try {
Document doc = new Document(fileInputStream);
doc.save(outputStream, SaveFormat.PDF);
} catch (Exception e) {
e.printStackTrace();
}
}
}
aspose-words-21.6.jar