本文共 2515 字,大约阅读时间需要 8 分钟。
在项目根目录创建lib文件夹,将下载的Aspose Slides JAR包放置在此目录下。接着在pom.xml文件中添加以下依赖项:
com.aspose aspose-slides 19.3 system ${project.basedir}/lib/aspose.slides-19.3.jar
将license.xml文件放置在项目的resources根目录下,确保Aspose能够正确读取并应用许可证,以去除转换后的PPT水印。
创建一个Java类PPT2PdfAsposeUtil,以下是核心转换逻辑:
import com.aspose.slides.License;import com.aspose.slides.Presentation;import com.aspose.slides.SaveFormat;import org.springframework.core.io.support.PathMatchingResourcePatternResolver;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.util.Date;public class PPT2PdfAsposeUtil { public static boolean getLicensePPT() { InputStream is = null; try { ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); org.springframework.core.io.Resource[] resources = resolver.getResources("classpath:license.xml"); is = resources[0].getInputStream(); License aposeLic = new License(); aposeLic.setLicense(is); return true; } catch (Exception e) { e.printStackTrace(); return false; } finally { if (is != null) { try { is.close(); } catch (IOException e) { e.printStackTrace(); } } } } public static boolean ppt2Pdf(String inPath, String outPath) { if (!getLicensePPT()) { return false; } long start = new Date().getTime(); try { FileInputStream fileInput = new FileInputStream(inPath); Presentation pres = new Presentation(fileInput); FileOutputStream out = new FileOutputStream(new File(outPath)); pres.save(out, SaveFormat.Pdf); out.close(); } catch (Exception e) { e.printStackTrace(); return false; } long end = new Date().getTime(); System.out.println("PDF转换成功,耗时:" + (end - start) / 1000.0 + "秒"); return true; } public static void main(String[] args) { ppt2Pdf("E:/code/test/1.pptx", "E:/code/test/1.pdf"); }} 将上述类中的main方法调用,传入需要转换的PPT文件路径和生成的PDF文件路径即可完成转换操作。
license.xml已正确放置在资源目录下。license.xml文件格式和存储位置是否正确。转载地址:http://tvvfk.baihongyu.com/