仅供学习 请勿商用!
package com.wisdom.util;
import com.aspose.slides.Presentation;
import com.aspose.words.Document;
import com.aspose.cells.Workbook;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
/**
* Created by tanly on 2018/10/18 0018.
*/
public class TanlyTtest {
public static boolean getLicense(String type) {
boolean result = false;
try {
File file = new File("C:/Users/Administrator/Desktop/aposeLic/license.xml");
InputStream is = new FileInputStream(file);
if (type.equals("excel")) {
com.aspose.cells.License aposeLic = new com.aspose.cells.License();
aposeLic.setLicense(is);
} else if (type.equals("ppt")) {
com.aspose.slides.License aposeLic = new com.aspose.slides.License();
aposeLic.setLicense(is);
} else {
com.aspose.words.License aposeLic = new com.aspose.words.License();
aposeLic.setLicense(is);
}
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
public static void main(String[] args) {
String rootPath = "C:/Users/Administrator/Desktop/aposeLic";
File dir = new File(rootPath + "/source/");
File[] all = dir.listFiles();
if (all == null) {
return;
}
for (File sfile : all) {
String fileType = sfile.getName().substring(sfile.getName().lastIndexOf(".") + 1, sfile.getName().length());
if ("docx".equals(fileType) || "doc".equals(fileType) || "rtf".equals(fileType) || "txt".equals(fileType)) {
if (getLicense("word")) {//WORD
try {
long start = System.currentTimeMillis();
File pdfFile = new File( rootPath + "/target/" + sfile.getName().substring(0, sfile.getName().lastIndexOf(".")) + ".pdf");
FileOutputStream os = new FileOutputStream(pdfFile);
Document doc = new Document(sfile.getAbsolutePath());
doc.save(os, com.aspose.words.SaveFormat.PDF);
System.out.println("固化:" + fileType + " 耗时:" + ((System.currentTimeMillis() - start) / 1000.0) + "秒");
} catch (Exception e) {
e.printStackTrace();
}
} else {
System.out.println("WORD证书失败");
}
} else if ("xls".equals(fileType) || "xlsx".equals(fileType) || "xlsm".equals(fileType)) {
if (getLicense("excel")) {//EXCEL
try {
long start = System.currentTimeMillis();
Workbook wb = new Workbook(sfile.getAbsolutePath());
File pdfFile = new File( rootPath + "/target/" + sfile.getName().substring(0, sfile.getName().lastIndexOf(".")) + ".pdf");
FileOutputStream fileOS = new FileOutputStream(pdfFile);
wb.save(fileOS, com.aspose.cells.SaveFormat.PDF);
System.out.println("固化:" + fileType + " 耗时:" + ((System.currentTimeMillis() - start) / 1000.0) + "秒");
} catch (Exception e) {
e.printStackTrace();
}
} else {
System.out.println("EXCEL证书失败");
}
} else if ("ppt".equals(fileType) || "pptx".equals(fileType) || "pps".equals(fileType)) {
if (getLicense("ppt")) {//PPT
try {
long start = System.currentTimeMillis();
InputStream slides = new FileInputStream(sfile.getAbsoluteFile());
Presentation pres = new Presentation(slides);
File pdfFile = new File( rootPath + "/target/" + sfile.getName().substring(0, sfile.getName().lastIndexOf(".")) + ".pdf");
FileOutputStream fileOS = new FileOutputStream(pdfFile);
pres.save(fileOS, com.aspose.slides.SaveFormat.Pdf);
System.out.println("固化:" + fileType + " 耗时:" + ((System.currentTimeMillis() - start) / 1000.0) + "秒");
} catch (Exception e) {
e.printStackTrace();
}
} else {
System.out.println("PPT证书失败");
}
} else {
System.out.println("格式不支持:" + fileType);
}
}
System.out.println("\n>>>>>>>>>固化结束!");
}
}