aspose.slides-15.9.0 将ppt转为pdf,去除水印
使用aspose.slides-15.9.0.jar将ppt转为pdf实现在线预览,去除水印。
下载地址:https://download.csdn.net/download/qq_31674229/32461644
import com.aspose.slides.License;
import com.aspose.slides.Presentation;
import com.aspose.slides.SaveFormat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
/**
* 将ppt转换为pdf
*/
public class Ppt2PdfUtil {
private static Logger logger = LoggerFactory.getLogger(Ppt2PdfUtil.class);
public static boolean getLicense() {
boolean result = false;
InputStream is = null;
try {
is = Ppt2PdfUtil.class.getClassLoader().getResourceAsStream("license-slides.xml"); // license.xml应放在..\WebRoot\WEB-INF\classes路径下
if(is != null ){
License aposeLic = new License();
aposeLic.setLicense(is);
result = true;
}
} catch (Exception e) {
e.printStackTrace();
}finally {
try{
if(is != null){
is.close();
}
}catch (Exception e){
e.printStackTrace();
}
}
return result;
}
/**
* 将ppt转换为pdf
* @param inPath ppt存储路径
* @param outPath pdf保存路径
*/
public static void ppt2pdf(String inPath, String outPath) {
if (!getLicense() || StringUtils.isEmpty(inPath) || StringUtils.isEmpty(outPath)) { // 验证License 若不验证则转化出的pdf文档会有水印产生
return;
}
try {
long old = System.currentTimeMillis();
File file = new File(outPath); // 新建一个空白pdf文档
FileOutputStream os = new FileOutputStream(file);
Presentation pres = new Presentation (inPath); // Address是将要被转化的ppt文档
pres.save(os, SaveFormat.Pdf);// 全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF,EPUB, XPS, SWF 相互转换
os.flush();
os.close();
long now = System.currentTimeMillis();
System.out.println("Ppt转换成功,共耗时:" + ((now - old) / 1000.0) + "秒"); // 转化用时
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 将ppt转为pdf
* @param input 需要转换的ppt
* @param saveFile 保存pdf文件
*/
public static void ppt2pdf(InputStream input, File saveFile) {
if (!getLicense() || input==null || saveFile==null) { // 验证License 若不验证则转化出的pdf文档会有水印产生
return;
}
try {
long old = System.currentTimeMillis();
FileOutputStream os = new FileOutputStream(saveFile);
Presentation pres = new Presentation (input); // Address是将要被转化的excel文档
pres.save(os, SaveFormat.Pdf);// 全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF,EPUB, XPS, SWF 相互转换
os.flush();
os.close();
long now = System.currentTimeMillis();
System.out.println("Ppt转换成功,共耗时:" + ((now - old) / 1000.0) + "秒"); // 转化用时
} catch (Exception e) {
e.printStackTrace();
}
}
}