本文将介绍如何使用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 svgbytes =(arraylist) 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();