本文将介绍如何使用Spire.Presentation for Java将PowerPoint文件转换为PNG, PDF和SVG, 以及将PPT和PPTX进行格式互转。
原PowerPoint文档:
PowerPoint转PNG
//创建Presentation对象
Presentation ppt = new Presentation();
//加载示例文档
ppt.loadFromFile("C:/Users/Administrator/Desktop/example.pptx");
//遍历幻灯片
for (int i = 0; i < ppt.getSlides().getCount(); i++) {
//将幻灯片保存为BufferedImage对象
BufferedImage image = ppt.getSlides().get(i).saveAsImage();
//将BufferedImage保存为PNG格式文件
String fileName = String.format("output/ToImage-%1$s.png", i);
ImageIO.write(image, "PNG",new File(fileName));
}
ppt.dispose();
PowerPoint转PDF
//创建Presentation对象
Presentation ppt = new Presentation();
//加载示例文档
ppt.loadFromFile("C:/Users/Administrator/Desktop/example.pptx");
//保存为PDF文档
ppt.saveToFile("output/ToPDF.pdf", FileFormat.PDF);
ppt.dispose();
PowerPoint转SVG
//创建Presentation对象
Presentation ppt = new Presentation();
//加载示例文档
ppt.loadFromFile("C:/Users/Administrator/Desktop/example.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("output/ToSVG-%d.svg", i));
stream.write(bytes);
}
ppt.dispose();
PPT、PPTX格式互转
//创建Presentation对象
Presentation ppt = new Presentation();
//加载PPTX文档
ppt.loadFromFile("C:/Users/Administrator/Desktop/example.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();