Aspose.Words是一个商业.NET类库,可以使得应用程序处理大量的文件任务。Aspose.Words支持Doc,Docx,RTF,HTML,OpenDocument,PDF,XPS,EPUB和其他格式。使用Aspose.Words可以在不使用Microsoft.Word的情况下生成、修改、转换和打印文档。官方文档:https://www.aspose.com
自己写的工具类分享记录一下
package com.feng.util;
import com.aspose.cells.*;
import com.aspose.cells.PdfSaveOptions;
import com.aspose.slides.Presentation;
import com.aspose.words.*;
import com.feng.bean.ResultCode;
import com.feng.error.MyException;
import lombok.extern.slf4j.Slf4j;
import java.io.*;
/**
* aspose转PDF工具类
* Created by feng on 2020/6/29.
*/
@Slf4j
public class AsposeUtil {
public static void main(String[] args) throws Exception {
//word转pdf
doc2Pdf("d:/tmp/tmp/1.doc", "d:/tmp/tmp/doc1.pdf");
doc2Pdf("d:/tmp/tmp/1.docx", "d:/tmp/tmp/docx1.pdf");
//xls转pdf
excel2Pdf("d:/tmp/tmp/1.xls", "d:/tmp/tmp/xls1.pdf");
excel2Pdf("d:/tmp/tmp/1.xlsx", "d:/tmp/tmp/xlsx1.pdf");
//ppt转pdf
ppt2Pdf("d:/tmp/tmp/1.ppt", "d:/tmp/tmp/ppt1.pdf");
ppt2Pdf("d:/tmp/tmp/1.pptx", "d:/tmp/tmp/pptx1.pdf");
}
/**
* word to pdf
*
* @param inPath word 全路径
* @param outPath 生成的pdf 全路径
* @throws Exception
* @author
*/
public static String doc2Pdf(String inPath, String outPath) throws MyException{
FileOutputStream os = null;
try {
if (!isWordLicense()) {
return null;
}
File file = new File(outPath);// 新建一个空白pdf文档
os = new FileOutputStream(file);
Document doc = new Document(inPath); // Address是将要被转化的word文档
doc.save(os, com.aspose.words.SaveFormat.PDF);
} catch (Exception e) {
log.error("word转换PDF错误:", e);
throw new MyException(ResultCode.FAILURE, "word转换PDF错误");
} finally {
if (os != null) {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return outPath;
}
/**
* ppt to pdf
*
* @param inPath ppt 全路径
* @param outPath 生成的pdf 全路径
* @throws Exception
* @author
*/
public static String ppt2Pdf(String inPath, String outPath) throws MyException{
FileOutputStream os = null;
try {
if (!isPPTLicense()) {
return null;
}
File file = new File(outPath);// 新建一个空白pdf文档
os = new FileOutputStream(file);
Presentation doc = new Presentation(inPath); // Address是将要被转化的word文档
doc.save(os, com.aspose.slides.SaveFormat.Pdf);
} catch (Exception e) {
log.error("PPT转换PDF错误:", e);
throw new MyException(ResultCode.FAILURE, "PPT转换PDF错误");
} finally {
if (os != null) {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return outPath;
}
/**
* excel to pdf
*
* @param inPath excel 全路径
* @param outPath 生成的pdf 全路径
* @throws Exception
* @author
*/
public static String excel2Pdf(String inPath, String outPath) throws MyException{
FileOutputStream os = null;
try {
if (!isExcelLicense()) {
return null;
}
File file = new File(outPath);// 新建一个空白pdf文档
os = new FileOutputStream(file);
PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();
pdfSaveOptions.setOnePagePerSheet(true);
Workbook doc = new Workbook(inPath); // Address是将要被转化的word文档
doc.save(os, com.aspose.cells.SaveFormat.PDF);
} catch (Exception e) {
log.error("excel转换PDF错误:", e);
throw new MyException(ResultCode.FAILURE, "excel转换PDF错误");
} finally {
if (os != null) {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return outPath;
}
/**
* ppt to new ppt
* 截取ppt前x页
*
* @param inPath ppt 全路径
* @param outPath new ppt 全路径
* @param x 页数
* @throws Exception
* @author
*/
public static String ppt2NewPPT(String inPath, String outPath, int x) throws MyException{
FileOutputStream os = null;
try {
if (!isPPTLicense()) {
return null;
}
//源文件
Presentation pres = new Presentation(inPath);
//新文件
Presentation pes = new Presentation();
//移除默认生成的第一页幻灯片
pes.getSlides().removeAt(0);
if (pres.getSlides().size() > x) {
for (int i = 0; i < x; i++) {
//拷贝
pes.getSlides().addClone(pres.getSlides().get_Item(i));
}
//最后一页添加说明:前x页预览完成!如需阅读全文请下载该文档到本地进行查看。
pes.getSlides().addFromHtml("前" + x + "页预览完成!如需阅读全文请下载该文档到本地进行查看。");
} else {
for (int i = 0; i < pres.getSlides().size(); i++) {
//拷贝
pes.getSlides().addClone(pres.getSlides().get_Item(i));
}
}
// 新建一个空白文档
File file = new File(outPath);
os = new FileOutputStream(file);
pes.save(os, com.aspose.slides.SaveFormat.Pptx);
} catch (Exception e) {
log.error("PPT转换新PPT错误:", e);
throw new MyException(ResultCode.FAILURE, "PPT转换新PPT错误");
} finally {
if (os != null) {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return outPath;
}
/**
* excel to image
*
* @param inPath excel 全路径
* @param outPath 生成的image 全路径
* @throws Exception
* @author
*/
public static String excel2Image(String inPath, String outPath) throws MyException{
try {
if (!isExcelLicense()) {
return null;
}
//Instantiate and open an Excel file
Workbook book = new Workbook(inPath);
//Define ImageOrPrintOptions
ImageOrPrintOptions imgOptions = new ImageOrPrintOptions();
//Set the vertical and horizontal resolution
imgOptions.setVerticalResolution(200);
imgOptions.setHorizontalResolution(200);
//Set the image's format
imgOptions.setImageFormat(ImageFormat.getJpeg());
//One page per sheet is enabled
imgOptions.setOnePagePerSheet(true);
//Get the first worksheet
Worksheet sheet = book.getWorksheets().get(0);
//Render the sheet with respect to specified image/print options
SheetRender sr = new SheetRender(sheet, imgOptions);
//Render the image for the sheet
sr.toImage(0, outPath);
} catch (Exception e) {
log.error("excel转换img错误:", e);
throw new MyException(ResultCode.FAILURE, "excel转换img错误");
}
return outPath;
}
/**
* excel to html
*
* @param inPath excel 全路径
* @param outPath 生成的html 全路径
* @throws Exception
* @author an
*/
public static String excel2Html(String inPath, String outPath) throws MyException{
try {
if (!isExcelLicense()) {
return null;
}
Workbook doc = new Workbook(inPath); // Address是将要被转化的word文档
//计算公式后的工作簿
doc.calculateFormula();
doc.save(outPath, com.aspose.cells.SaveFormat.HTML);
} catch (Exception e) {
log.error("excel转换html错误:", e);
throw new MyException(ResultCode.FAILURE, "excel转换html错误");
}
return outPath;
}
/**
* @Description: 验证aspose.word组件是否授权:无授权的文件有水印和试用标记
*/
public static boolean isWordLicense() {
boolean result = false;
try {
// InputStream inputStream = new
// FileInputStream("D:\\Workspaces\\TestFilters\\lib\\license.xml");
// 避免文件遗漏
String licensexml = "<License>\n" + "<Data>\n" + "<Products>\n"
+ "<Product>Aspose.Total for Java</Product>\n" + "<Product>Aspose.Words for Java</Product>\n"
+ "</Products>\n" + "<EditionType>Enterprise</EditionType>\n"
+ "<SubscriptionExpiry>20991231</SubscriptionExpiry>\n"
+ "<LicenseExpiry>20991231</LicenseExpiry>\n"
+ "<SerialNumber>23dcc79f-44ec-4a23-be3a-03c1632404e9</SerialNumber>\n" + "</Data>\n"
+ "<Signature>\n"
+ "sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU=\n"
+ "</Signature>\n" + "</License>";
InputStream inputStream = new ByteArrayInputStream(licensexml.getBytes());
com.aspose.words.License license = new com.aspose.words.License();
license.setLicense(inputStream);
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
* @Description: 验证aspose.slides组件是否授权:无授权的文件有水印和试用标记
*/
public static boolean isPPTLicense() {
boolean result = false;
try {
// InputStream inputStream = new
// FileInputStream("D:\\Workspaces\\TestFilters\\lib\\license.xml");
// 避免文件遗漏
String licensexml = "<License>\n" + "<Data>\n" + "<Products>\n"
+ "<Product>Aspose.Total for Java</Product>\n" + "<Product>Aspose.Words for Java</Product>\n"
+ "</Products>\n" + "<EditionType>Enterprise</EditionType>\n"
+ "<SubscriptionExpiry>20991231</SubscriptionExpiry>\n"
+ "<LicenseExpiry>20991231</LicenseExpiry>\n"
+ "<SerialNumber>23dcc79f-44ec-4a23-be3a-03c1632404e9</SerialNumber>\n" + "</Data>\n"
+ "<Signature>\n"
+ "sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU=\n"
+ "</Signature>\n" + "</License>";
InputStream inputStream = new ByteArrayInputStream(licensexml.getBytes());
com.aspose.slides.License license = new com.aspose.slides.License();
license.setLicense(inputStream);
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
* @Description: 验证aspose.cells组件是否授权:无授权的文件有水印和试用标记
*/
public static boolean isExcelLicense() {
boolean result = false;
try {
// InputStream inputStream = new
// FileInputStream("D:\\Workspaces\\TestFilters\\lib\\license.xml");
// 避免文件遗漏
String licensexml = "<License>\n" + "<Data>\n" + "<Products>\n"
+ "<Product>Aspose.Total for Java</Product>\n" + "<Product>Aspose.Words for Java</Product>\n"
+ "</Products>\n" + "<EditionType>Enterprise</EditionType>\n"
+ "<SubscriptionExpiry>20991231</SubscriptionExpiry>\n"
+ "<LicenseExpiry>20991231</LicenseExpiry>\n"
+ "<SerialNumber>23dcc79f-44ec-4a23-be3a-03c1632404e9</SerialNumber>\n" + "</Data>\n"
+ "<Signature>\n"
+ "sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU=\n"
+ "</Signature>\n" + "</License>";
InputStream inputStream = new ByteArrayInputStream(licensexml.getBytes());
com.aspose.cells.License license = new com.aspose.cells.License();
license.setLicense(inputStream);
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
}
需要的包:
aspose-cells-18.9-jdk16.jar
aspose-slides-18.7-jdk16.jar
aspose-words-18.10-jdk16.jar
百度网盘链接: https://pan.baidu.com/s/1Xn93jdeu--BEB2JiC-PAzw 提取码: bqut
使用Aspose转换PDF遇到性能问题(80m的ppt都把内存撑爆了),最后改为了LibreOffice。
了解更多LibreOffice:java使用openoffice/libreoffice进行office转pdf