aspose转换xlsx and doc and docx to pdf去水印无页数限制
pom文件引入
代码语言:javascript复制<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-cells</artifactId>
<version>19.2.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/resources/lib/aspose-cells.jar</systemPath>
</dependency>
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-words</artifactId>
<version>21.1.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/resources/lib/aspose-words.jar</systemPath><!-- jar包路径 -->
</dependency>
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-slides</artifactId>
<version>19.3.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/resources/lib/aspose-slides.jar</systemPath>
</dependency>
导入包
代码语言:javascript复制import com.aspose.cells.License;
import com.aspose.cells.PdfSaveOptions;
import com.aspose.cells.Workbook;
import com.aspose.slides.*;
import com.aspose.words.Document;
import com.aspose.words.SaveFormat;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;
import java.io.*;
import java.util.Date;
Java工具类代码
代码语言:javascript复制/*
*去水印
*/
public static boolean getLicense() {
boolean result = false;
try {
InputStream is = ExcelToPdf.class.getClassLoader().getResourceAsStream("license.xml"); // license.xml应放在..WebRootWEB-INFclasses路径下
License aposeLic = new License();
aposeLic.setLicense(is);
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
* xlsx to pdf
*/
public static boolean xlsxToPdf(String excelPath, String pdfPath) {
try {
getLicense();
long old = System.currentTimeMillis();
Workbook wb = new Workbook(excelPath);
FileOutputStream fileOS = new FileOutputStream(new File(pdfPath));
wb.save(fileOS, SaveFormat.PDF);
fileOS.close();
long now = System.currentTimeMillis();
System.out.println("Conversion time: " ((now - old) / 1000.0) " seconds");
return true;
} catch (Exception e) {
String errorMessage = e.getMessage();
throw new RuntimeException(errorMessage);
}
}
/**
* xlsx to pdf
*/
public static String xlsxToPdf(MultipartFile file, String pdfPath) {
getLicense();
if (file == null || file.isEmpty()) {
throw new RuntimeException("Excel文件不能为空");
}
try {
// 创建工作簿以加载Excel文件
Workbook workbook = new Workbook(file.getInputStream());
// 创建PDF选项
PdfSaveOptions options = new PdfSaveOptions();
options.setOnePagePerSheet(true);
// 将文档保存为PDF格式
workbook.save(pdfPath, options);
} catch (Exception e) {
e.printStackTrace();
}
return pdfPath;
}
/**
* @param wordPath 需要被转换的word全路径带文件名
* @param pdfPath 转换之后pdf的全路径带文件名
*/
public static boolean docTopdf(String wordPath, String pdfPath) {
try {
getLicense();
long old = System.currentTimeMillis();
File file = new File(pdfPath); //新建一个pdf文档
FileOutputStream os = new FileOutputStream(file);
Document doc = new Document(wordPath); //Address是将要被转化的word文档
doc.save(os, SaveFormat.PDF);//全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF, EPUB, XPS, SWF 相互转换
long now = System.currentTimeMillis();
os.close();
System.out.println("共耗时:" ((now - old) / 1000.0) "秒"); //转化用时
return true;
} catch (Exception e) {
String errorMessage = e.getMessage();
throw new RuntimeException(errorMessage);
}
}
/**
* doc and docx to pdf
*/
public static String docxToPdf(MultipartFile file, String pdfDir) {
getLicense();
if (file == null || file.isEmpty()) {
throw new RuntimeException("Word文档不能为空");
}
if (StringUtils.isEmpty(pdfDir)) {
throw new RuntimeException("PDF目录不能为空");
}
String pdfPath = pdfDir;
try {
// 加载Word文档
Document doc = new Document(file.getInputStream());
// 将文档保存为PDF格式
doc.save(pdfPath, SaveFormat.PDF);
} catch (Exception e) {
e.printStackTrace();
}
return pdfPath;
}
public static boolean getLicensePPT() {
boolean result = false;
InputStream is = null;
try {
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
org.springframework.core.io.Resource[] resources = resolver.getResources("classpath:license.xml");
is = resources[0].getInputStream();
com.aspose.slides.License aposeLic = new com.aspose.slides.License();
aposeLic.setLicense(is);
result = true;
} catch (Exception e) {
e.printStackTrace();
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return result;
}
/**
*ppt路径 to pdf路径
*/
public static boolean ppt2Pdf(String inPath,String outPath){
try {
// 验证License 去除水印
if (!getLicensePPT()) {
return false;
}
long start = new Date().getTime();
FileInputStream fileInput = new FileInputStream(inPath);
Presentation pres = new Presentation(fileInput);
FileOutputStream out = new FileOutputStream(new File(outPath));
pres.save(out, com.aspose.slides.SaveFormat.Pdf);
out.close();
long end = new Date().getTime();
System.out.println("pdf转换成功,共耗时:" ((end - start) / 1000.0) "秒"); // 转化用时
return true;
} catch (Exception e) {
String errorMessage = e.getMessage();
throw new RuntimeException(errorMessage);
}
}
/**
*ppt路径 to pdf路径
*/
public static boolean ppt2Pdf(MultipartFile inFile, String outPath) {
try {
// 验证License 去除水印
if (!getLicensePPT()) {
return false;
}
long start = new Date().getTime();
InputStream fileInput = inFile.getInputStream();
Presentation pres = new Presentation(fileInput);
FileOutputStream out = new FileOutputStream(new File(outPath));
pres.save(out, com.aspose.slides.SaveFormat.Pdf);
out.close();
long end = new Date().getTime();
System.out.println("pdf转换成功,共耗时:" ((end - start) / 1000.0) "秒"); // 转化用时
return true;
} catch (Exception e) {
String errorMessage = e.getMessage();
throw new RuntimeException(errorMessage);
}
}
license.xml.rp.ce https://qweqwe123.lanzoub.com/ibj1f174h9hg
aspose-words.jar https://qweqwe123.lanzoub.com/iy01u174h9fe
aspose-slides.jar https://qweqwe123.lanzoub.com/iwoPq174h8ha
aspose-cells.jar https://qweqwe123.lanzoub.com/iSmuC174h6hi
有什么不了解的可以私信或者评论