XPS(XML Paper Specification)是一个基于XML格式,以页为单位的电子文档格式。与PDF格式类似,其内容无法轻易变更,便于使用者进行保存、浏览及打印。本文将介绍如何用Java程序来将PPT文档转换为PDF及XPS格式,同时也将演示PPT与PPTX格式之间互转的方式。
本文代码的测试环境:
- Intellij Idea2019.1
- JDK 1.8.0
- Spire.Presentation.jar
Jar包导入方式:
方式 1:通过官网下载Free Spire.Presentation for Java类库,解压文档后将lib文件夹下的Spire.Presentation.jar手动导入IDEA即可。具体导入步骤可参考下图。
![d4d049bd36c6080c3c64e55da633324e.png](https://img-blog.csdnimg.cn/img_convert/d4d049bd36c6080c3c64e55da633324e.png)
方式 2:创建一个Maven应用程序,在pom.xml文件中配置Maven仓库路径及指定Spire.Presentation for Java的Maven依赖。
com.e-icebluehttp://repo.e-iceblue.cn/repository/maven-public/ e-iceblue spire.presentation.free 2.6.1
配置完成后,在IDEA中,您需点击"Import Changes"即可导入JAR包;在Eclipse中,则需要点击"Save"按钮。
PPT示例文档:
![0f025dfa3cf8df14f0ae6033f54a9802.png](https://img-blog.csdnimg.cn/img_convert/0f025dfa3cf8df14f0ae6033f54a9802.png)
代码示例
示例1:PPT转PDF
import com.spire.presentation.FileFormat;import com.spire.presentation.Presentation;public class ToPDF { public static void main(String[] args) throws Exception { //创建Presentation实例 Presentation presentation = new Presentation(); //加载PPT示例文档 presentation.loadFromFile("C:甥敳獲Test1DesktopSample.pptx"); //保存为PDF格式 presentation.saveToFile("output/toPDF.pdf", FileFormat.PDF); presentation.dispose(); }}
转换效果:
![aa17d7066fdbd08fb9c213c65b52cafe.png](https://img-blog.csdnimg.cn/img_convert/aa17d7066fdbd08fb9c213c65b52cafe.png)
示例2:PPT转XPS
import com.spire.presentation.*;public class ToXPS { public static void main(String[] args) throws Exception { //创建Presentation实例 Presentation ppt = new Presentation(); //加载PPT示例文档 ppt.loadFromFile("C:甥敳獲Test1DesktopSample.pptx"); //保存为 .xps格式 ppt.saveToFile("output/toXPS.xps", FileFormat.XPS); ppt.dispose(); }}
转换效果:
![eb276ac7461e48132fb336388d3b2a25.png](https://img-blog.csdnimg.cn/img_convert/eb276ac7461e48132fb336388d3b2a25.png)
示例3:PPT、PPTX格式互转
import com.spire.presentation.FileFormat;import com.spire.presentation.Presentation;public class ToPPT { public static void main(String[] args) throws Exception { //创建Presentation对象 Presentation ppt = new Presentation(); //加载PPTX文档 ppt.loadFromFile("C:甥敳獲Test1DesktopSample.pptx"); //保存为PPT文档 ppt.saveToFile("output/ToPPT.ppt", FileFormat.PPT); //PPT转PPTX //ppt.loadFromFile("C:/Users/Administrator/Desktop/example.ppt"); //ppt.saveToFile("output/ToPPTX.pptx",FileFormat.PPTX_2013); ppt.dispose(); }}