Spire.Cloud.PowerPoint 转换 PPT(X) 为 PDF、XPS、图片等格式

Spire.Cloud.PowerPoint提供的DocumentApi接口,可以将PowerPoint文档保存为其他格式文档。若要将整个PowerPoint文档转换为PDF、PPT、PPTX、PPS、PPSX、XPS、ODP格式,使用DocumentApi接口下的convertPptToPath方法即可,如示例1、2所示;若要将PowerPoint文档中的每一页幻灯片分别保存为JPEG、PNG、GIF、BMP、PPTX、PPSX、PPT、PPS、 ODP、PDF、XPS、PS、PCL、SVG格式文档,则需要使用DocumentApi接口下的splitPpt方法,如示例3所示。

步骤一:创建一个Maven程序,通过Maven仓库安装Spire.Cloud.SDK,详细步骤可参考这篇文章

步骤二:通过冰蓝云官网(https://cloud.e-iceblue.cn/)注册账号并登陆,在“我的应用”版块创建应用程序,获得App ID及App Key。

Spire.Cloud.PowerPoint 转换 PPT(X) 为 PDF、XPS、图片等格式

步骤三:上传PowerPoint文档至冰蓝云官网的“文档管理”版块。为了便于文档管理,您也可以先创建文件夹“input”和“output”,然后将需要转换的PowerPoint文档上传至input文件夹下,output文件夹用于存放生成的文档。本教程将示例文档(sample.pptx)上传到了input文件夹下。

Spire.Cloud.PowerPoint 转换 PPT(X) 为 PDF、XPS、图片等格式

步骤四:在Maven程序中编写测试类操作input文件夹下的文档。


示例1、将PowerPoint转换为PDF

import spire.cloud.powerpoint.sdk.*;
import spire.cloud.powerpoint.sdk.model.*;
import spire.cloud.powerpoint.sdk.api.DocumentApi;

public class ConvertPptToOtherFormats {

    static String appId = "App ID";
    static String appKey = "App Key";
    static String baseUrl = "https://api.e-iceblue.cn";

    //配置App ID和App Key
    static Configuration configuration = new Configuration(appId, appKey, baseUrl);

    //初始化DocumentApi对象
    static DocumentApi documentApi = new DocumentApi(configuration);

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

        //获取需要转换的源文档名称
        String name = "sample.pptx";

        //设置整个文档的导出格式为PDF
        String destFilePath = "output/PptToPdf.pdf";

        //指定转换格式为PDF
        String format = ExportFormat.PDF.toString();
        
        //将ExportOptions设置为null
        ExportOptions options = null;
        
        //指定文档打开密码,无密码则为null
        String password = null;
        
        //使用冰蓝云默认的存储空间,将storage设置为null
        String storage = null;
        
        //获取存储源文档的文件夹
        String folder = "input";

        //调用convertPptToPath方法将PowerPoint文档转换为指定格式的文档,并存放到指定位置
        documentApi.convertPptToPath(name, destFilePath, format, options, password, storage, folder);
    }
}

Spire.Cloud.PowerPoint 转换 PPT(X) 为 PDF、XPS、图片等格式

示例2、将PowerPoint转换为XPS

import spire.cloud.powerpoint.sdk.*;
import spire.cloud.powerpoint.sdk.model.*;
import spire.cloud.powerpoint.sdk.api.DocumentApi;

public class ConvertPptToOtherFormats {

    static String appId = "App ID";
    static String appKey = "App Key";
    static String baseUrl = "https://api.e-iceblue.cn";

    //配置App ID和App Key
    static Configuration configuration = new Configuration(appId, appKey, baseUrl);

    //初始化DocumentApi对象
    static DocumentApi documentApi = new DocumentApi(configuration);

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

        //获取需要转换的源文档名称
        String name = "sample.pptx";

        //指定转换后的文档的路径的名称
        String destFilePath = "output/PptToXps.xps";

        //设置整个文档的导出格式为XPS
        String format = ExportFormat.XPS.toString();

        //将ExportOptions设置为null
        ExportOptions options = null;

        //指定文档打开密码,无密码则为null
        String password = null;

        //使用冰蓝云默认的存储空间,将storage设置为null
        String storage = null;

        //获取存储源文档的文件夹
        String folder = "input";

        //调用convertPptToPath方法将PowerPoint文档转换为指定格式的文档,并存放到指定位置
        documentApi.convertPptToPath(name, destFilePath, format, options, password, storage, folder);
    }
}

Spire.Cloud.PowerPoint 转换 PPT(X) 为 PDF、XPS、图片等格式

示例3、将PowerPoint转换为PNG

import spire.cloud.powerpoint.sdk.ApiException;
import spire.cloud.powerpoint.sdk.Configuration;
import spire.cloud.powerpoint.sdk.api.DocumentApi;
import spire.cloud.powerpoint.sdk.model.ExportOptions;
import spire.cloud.powerpoint.sdk.model.SlideExportFormat;

public class ConvertPptToPng {

    static String appId = "App ID";
    static String appKey = "App Key";
    static String baseUrl = "https://api.e-iceblue.cn";

    //配置App ID和App Key
    static Configuration configuration = new Configuration(appId, appKey, baseUrl);

    //初始化DocumentApi对象
    static DocumentApi documentApi = new DocumentApi(configuration);

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

        //获取需要转换的源文档名称
        String name = "sample.pptx";

        //设置ExportOptions为null
        ExportOptions options = null;

        //设置幻灯片的导出格式为PNG
        String format = SlideExportFormat.PNG.toString();

        //指定图片大小
        Integer width = Math.round(72*33.867f*0.3937f);
        Integer height = Math.round(72*19.05f*0.3937f);

        //指定转换为图片的幻灯片页码范围(幻灯片页码从0开始计数)
        Integer from = 0;
        Integer to = 2;

        //指定结果文档存放的文件夹
        String destFolder = "output";

        ///指定文档打开密码,无密码则为null
        String password = null;

        //使用冰蓝云默认的存储空间,将storage设置为null
        String storage = null;

        //指定存储源文档的文件夹
        String folder = "input";
        
        //调用splitPpt方法将指定页码范围的幻灯片保存为图片
        documentApi.splitPpt(name, options, format, width, height, to, from, destFolder, password, storage, folder);
    }
}

Spire.Cloud.PowerPoint 转换 PPT(X) 为 PDF、XPS、图片等格式