Java 将PPT转为PNG/PDF/SVG/PPTX
1、方法1:从官网(https://www.e-iceblue.cn/Downloads/Spire-Presentation-JAVA.html)下载jar包。在程序下新建一个directory目录,并命名(本示例中命名为lib);将控件包lib文件夹下的jar(如下图1)拷贝到程序中新建的目录下。复制jar文件后,鼠标右键点击jar文件,选择”Add as Library”。完成导入(如下图2)。
2、方法2:通过maven导入。参考导入方法(https://www.e-iceblue.cn/licensing/install-spirepdf-for-java-from-maven-repository.html )。
1、import com.spire.presentation.FileFormat;
import com.spire.presentation.Presentation;
public class PPTtoPDF {
public static void main(String[] args) throws Exception{
//创建Presentation对象
Presentation ppt = new Presentation();
//加载示例文档
ppt.loadFromFile("sample.pptx");
//保存为PDF文档
ppt.saveToFile("ToPDF.pdf", FileFormat.PDF);
ppt.dispose();
}
}
2、PPT转PDF效果:
1、import com.spire.presentation.*;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
public class PPTtoPNG {
public static void main(String[] args) throws Exception{
//创建Presentation对象
Presentation ppt = new Presentation();
//加载示例文档
ppt.loadFromFile("sample.pptx");
//遍历幻灯片
for (int i = 0; i < ppt.getSlides().getCount(); i++) {
//将幻灯片保存为BufferedImage对象
BufferedImage image = ppt.getSlides().get(i).saveAsImage();
//将BufferedImage保存为PNG格式文件
String fileName = String.format("ToImage.png", i);
ImageIO.write(image, "PNG",new File(fileName));
}
ppt.dispose();
}
}
2、PPT转PNG图片效果:
1、import com.spire.presentation.*;
import java.io.FileOutputStream;
import java.util.ArrayList;
public class PPTtoSVG {
public static void main(String[] args) throws Exception{
//创建Presentation对象
Presentation ppt = new Presentation();
//加载示例文档
ppt.loadFromFile("sample.pptx");
//将PowerPoint文档转换为SVG格式,并以byte数组的形式保存于ArrayList
ArrayList<byte[]> svgBytes =(ArrayList<byte[]>) ppt.saveToSVG();
//遍历ArrayList中的byte数组
for (int i = 0; i < svgBytes.size(); i++)
{
//将byte数组保存为SVG格式文件
byte[] bytes = svgBytes.get(i);
FileOutputStream stream = new FileOutputStream(String.format("ToSVG.svg", i));
stream.write(bytes);
}
ppt.dispose();
}
}
2、PPT转SVG效果:
1、import com.spire.presentation.*;
public class PPTXtoPPT {
public static void main( String[] args) throws Exception{
//创建Presentation对象
Presentation ppt = new Presentation();
//加载PPTX文档
ppt.loadFromFile("sample.pptx");
//保存为PPT文档
ppt.saveToFile("ToPPT.ppt", FileFormat.PPT);
//PPT转PPTX
// ppt.loadFromFile("sample.ppt");
//ppt.saveToFile("oPPTX.pptx",FileFormat.PPTX_2013);
ppt.dispose();
}
}
2、PPTX转PPT效果: