Java PowerPoint转图片、PDF、XPS和SVG等格式文件

版权声明:本文为博主原创文章,转载请注明出处。 https://blog.csdn.net/Eiceblue/article/details/85164737

这篇文章介绍如何在Java应用程序中将PowerPoint文档转换为图片、PDF、XPS和SVG等格式。

使用组件: Free Spire.Presentation for Java

组件简介:Free Spire.Presentation for Java是一个免费Java PowerPoint组件,支持丰富的格式转换,例如将PPT和PPTX格式互转,PPT/PPTX转图片、PDF、XPS和SVG等。

在使用以下代码前,需要下载Free Spire.Presentation for Java包并解压缩,然后从lib文件夹下导入jar包到应用程序中。

示例代码

原PowerPoint文档如下:

PowerPoint转图片

通过ISlide.SaveAsImage()方法,可以将整个PowerPoint文档或指定幻灯片转换为图片。

import com.spire.presentation.Presentation;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;

public class PPTToImage {
    public static void main(String[] args) throws Exception {
        //加载PPT
        Presentation ppt = new Presentation();
        ppt.loadFromFile("Input.pptx");

        //保存为图片
        for (int i = 0; i < ppt.getSlides().getCount(); i++) {
            BufferedImage image = ppt.getSlides().get(i).saveAsImage();
            String fileName = "output" + "/" + String.format("ToImage-%1$s.png", i);
            ImageIO.write(image, "PNG",new File(fileName));
        }
        ppt.dispose();
    }
}

PowerPointPDFXPS

通过Presentation.saveToFile(string filepath, FileFormat fileformat)方法,可以将PowerPoint文档保存为PPT、Pptx2007、Pptx2010、Pptx2013、Ppsx2007、 Ppsx2010、Ppsx2013、PPS、ODP、XPS、PDF、HTML、TIFF等格式。

以转PDF为例:

import com.spire.presentation.FileFormat;
import com.spire.presentation.Presentation;

public class PPTToPDF {

    public static void main(String[] args) throws Exception {

        //加载PPT 
        Presentation ppt = new Presentation();
        ppt.loadFromFile("Input.ppt");

        //保存为PDF
        ppt.saveToFile("ToPDF.pdf", FileFormat.PDF);
    }
}

PowerPoint转SVG

通过Presentation.saveToSVG()方法,可以将PowerPoint文档转换为SVG。

import com.spire.presentation.Presentation;

import java.io.FileOutputStream;
import java.util.ArrayList;

public class PPTToSVG {

    public static void main(String[] args) throws Exception {

        //加载PPT 
        Presentation ppt = new Presentation();
        ppt.loadFromFile("Input.ppt");

        //保存为SVG
        ArrayList<byte[]> svgBytes =(ArrayList<byte[]>) ppt.saveToSVG();
        int count = svgBytes.size();
        int len = svgBytes.size();
        for (int i = 0; i < len; i++)
        {
            byte[] bytes = svgBytes.get(i);
            FileOutputStream stream = new FileOutputStream(String.format("output" + "/" + "ToSVG-%d.svg", i));
            stream.write(bytes);
        }

        ppt.dispose();
    }
}

 

猜你喜欢

转载自blog.csdn.net/Eiceblue/article/details/85164737