去除水印操作参考:https://www.cnblogs.com/rickiyang/p/11336268.html
aspose使用教程
- maven引入被破解的版本(不推荐,推荐使用第2种方式)
第一种:jar包在项目目录下。先把jar拷贝到项目目录下,然后在maven中配置依赖,代码如下:
<dependency>
<groupId>com.aspose</groupId>
<artifactId>words</artifactId>
<version>20.8</version>
<scope>system</scope>
<systemPath>${basedir}/src/main/java/lib/aspose-words-20.8-jdk17.jar</systemPath>
</dependency>
<dependency>
<groupId>com.aspose</groupId>
<artifactId>cells</artifactId>
<version>20.4</version>
<scope>system</scope>
<systemPath>${basedir}/src/main/java/lib/aspose-cells-20.4.jar</systemPath>
</dependency>
第二种:jar包在maven仓库。在pom中引入依赖,然后把破解的jar包拷贝到仓库中去,配置仓库中需要的pom文件即可,代码如下:
<dependency>
<groupId>com.aspose</groupId>
<artifactId>cells</artifactId>
<version>20.4</version>
</dependency>
<dependency>
<groupId>com.aspose</groupId>
<artifactId>words</artifactId>
<version>20.8</version>
</dependency>
- 引入转PDF的工具
/**
* @param wordPath 需要被转换的word全路径带文件名
*/
public static File wordToPDF(String wordPath) throws Exception {
String fileNameSuffix = FileHelper.getFileNameAndSuffix(wordPath);
File file = null;
if (StringUtils.isNotBlank(wordPath) && StringUtils.isNotBlank(fileNameSuffix)){
String PDFPath = wordPath.split(fileNameSuffix)[0];
String fileName = FileHelper.getFileNameByName(fileNameSuffix);
//long old = System.currentTimeMillis();
file = new File(PDFPath + fileName + ".pdf");
if(!file.exists()){
FileOutputStream os = new FileOutputStream(file);
//Address是将要被转化的word文档
Document doc = new Document(wordPath);
//全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF, EPUB, XPS, SWF 相互转换
doc.save(os, com.aspose.words.SaveFormat.PDF);
os.close();
//long now = System.currentTimeMillis();
//System.out.println("共耗时:" + ((now - old) / 1000.0) + "秒"); //转化用时
// 返回转化后的PDF文件
}
}
return file;
}
/**
* @param excelPath 需要被转换的excel全路径带文件名
*/
public static File excelToPDF(String excelPath) throws Exception {
String fileNameSuffix = FileHelper.getFileNameAndSuffix(excelPath);
File file = null;
if (StringUtils.isNotBlank(excelPath) && StringUtils.isNotBlank(fileNameSuffix)) {
String PDFPath = excelPath.split(fileNameSuffix)[0];
String fileName = FileHelper.getFileNameByName(fileNameSuffix);
long old = System.currentTimeMillis();
file = new File(PDFPath + fileName + ".pdf");
if(!file.exists()) {
Workbook wb = new Workbook(excelPath);
FileOutputStream fileOS = new FileOutputStream(file);
wb.save(fileOS, com.aspose.cells.SaveFormat.PDF);
fileOS.close();
long now = System.currentTimeMillis();
System.out.println("共耗时:" + ((now - old) / 1000.0) + "秒"); //转化用时
// 返回转化后的PDF文件
}
}
return file;
}
/**
* @param pptPath 需要被转换的ppt全路径带文件名
*/
public static File pptToPDF(String pptPath) throws Exception {
String fileNameSuffix = FileHelper.getFileNameAndSuffix(pptPath);
File file = null;
if (StringUtils.isNotBlank(pptPath) && StringUtils.isNotBlank(fileNameSuffix)) {
String PDFPath = pptPath.split(fileNameSuffix)[0];
String fileName = FileHelper.getFileNameByName(fileNameSuffix);
long old = System.currentTimeMillis();
Workbook wb = new Workbook(pptPath);
file = new File(PDFPath + fileName + ".pdf");
FileOutputStream fileOS = new FileOutputStream(file);
wb.save(fileOS, com.aspose.cells.SaveFormat.PDF);
fileOS.close();
long now = System.currentTimeMillis();
System.out.println("共耗时:" + ((now - old) / 1000.0) + "秒"); //转化用时
// 返回转化后的PDF文件
}
return file;
}