1.将aspose-cells-8.5.2.jar,aspose-words-15.8.0-jdk16.jar这个两个JAR包引入到项目中:
2.将license.xml放入resource路径下:
3.测试方法:
WordToPdfUtil
package com.huaru.utils;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import com.aspose.words.Document;
import com.aspose.words.License;
import com.aspose.words.SaveFormat;
/**
* 由于ASPOSE比较吃内存,操作大一点的文件就会堆溢出,所以请先设置好java虚拟机参数:-Xms512m -Xmx512m(参考值)<br>
*/
public class WordToPdfUtil {
public static boolean getLicense(){
boolean result = false;
try {
InputStream is = WordToPdfUtil.class.getClassLoader().getResourceAsStream("\\license.xml");
License aposeLic = new License();
aposeLic.setLicense(is);
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
* 支持DOC, DOCX, OOXML, RTF, HTML, OpenDocument, PDF, EPUB, XPS, SWF等相互转换<br>
* @param wordFilePath [原始excel路径]
* @param pdfFilePath [输出路径]
* @return
*/
public static Boolean wordConvertToPdf(String wordFilePath, String pdfFilePath){
//验证license
if (!getLicense()){
return false;
}
try {
//原始word路径
Document doc = new Document(wordFilePath);
//输出路径
File pdfFile = new File(pdfFilePath);
FileOutputStream fileOS = new FileOutputStream(pdfFile);
doc.save(fileOS, SaveFormat.PDF);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
}
public static void main(String[] args) {
String sourcePath = "G:\\aaa.docx";
String targetPath = "G:\\test.pdf";
//验证license
if (!WordToPdfUtil.getLicense()){
return;
}
long old = System.currentTimeMillis();
if (WordToPdfUtil.wordConvertToPdf(sourcePath, targetPath)){
long now = System.currentTimeMillis();
System.out.println("word转pdf成功,共耗时:" + ((now - old) / 1000.0) + "秒");
}
}
ExcelToPdfUtil
package com.huaru.utils;
import com.aspose.cells.License;
import com.aspose.cells.SaveFormat;
import com.aspose.cells.Workbook;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
/**
* excel转pdf工具类
*/
public class ExcelToPdfUtil {
public static boolean getLicense(){
boolean result = false;
try {
InputStream is = ExcelToPdfUtil.class.getClassLoader().getResourceAsStream("\\license.xml");
License aposeLic = new License();
aposeLic.setLicense(is);
result = true;
}catch (Exception e){
e.printStackTrace();
}
return result;
}
/**
* 支持DOC, DOCX, OOXML, RTF, HTML, OpenDocument, PDF, EPUB, XPS, SWF等相互转换<br>
*/
public static Boolean excelConvertToPdf(String excelFilePath, String pdfFilePath){
//验证License
if (!getLicense()){
return false;
}
try {
Workbook wb = new Workbook(excelFilePath);
File pdfFile = new File(pdfFilePath);
FileOutputStream fileOS = new FileOutputStream(pdfFile);
wb.save(fileOS, SaveFormat.PDF);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
}
public static void main(String[] args) {
String sourcePath = "C:\\Users\\caojinlong\\Desktop\\知识图谱工具.xlsx";
String targetPath = "C:\\Users\\caojinlong\\Desktop\\知识图谱工具.pdf";
if (!ExcelToPdfUtil.getLicense()){
return;
}
long old = System.currentTimeMillis();
if (ExcelToPdfUtil.excelConvertToPdf(sourcePath, targetPath)){
long now = System.currentTimeMillis();
System.out.println("Excel转Pdf成功,共耗时:" + ((now - old) / 1000.0) + "秒");
}
}
jar包和代码百度网盘提取地址
链接:https://pan.baidu.com/s/1xaA69NUw6-JJRz-_mvyBYA
提取码:yhxn