使用 Java REST API 拆分幻灯片

按照本文操作即可使用 Java REST API 拆分幻灯片。您将学习使用基于 Java 的 Cloud SDK 自动使用基于 Java 的 API 拆分 PPTX。它共享转换过程自定义所需的所有属性。

先决条件

使用 Java Low Code API 拆分 PPT 的步骤

  1. 通过设置客户端 ID 和密钥创建 SlidesApi 类的对象
  2. 通过设置唯一名称将源演示文稿上传到云存储
  3. 使用上传的文件名、输出图像格式、起始幻灯片和最后一张幻灯片编号调用 Split() 方法
  4. 解析响应对象中的所有幻灯片
  5. 使用 Href 字符串中的图像名称下载每张幻灯片图像
  6. 将下载的镜像保存在磁盘上

这些步骤说明如何使用 Java REST 接口开发 PowerPoint 拆分工具。创建 SlidesApi 对象,上传源演示文稿文件,并通过提供所需信息来调用 Split() 方法。解析 API 响应并将针对各个幻灯片创建的所有图像保存在本地磁盘上。

使用 Java RESTful 服务的 PowerPoint 拆分器代码

import com.aspose.slides.ApiException;
import com.aspose.slides.api.SlidesApi;
import com.aspose.slides.model.*;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
public class Example_SplitPresentationSlides {
protected static SlidesApi presentationApi;
public Example_SplitPresentationSlides() {
if (presentationApi == null) {
presentationApi = new SlidesApi("appSid", "appKey");
}
}
public void SplitPresentation() throws ApiException, IOException {
String localPath = "/home/downloads/";
String fileName = "TestPresentation.pptx";
String storageFolderName = "TempTests";
presentationApi.uploadFile(storageFolderName+"/"+fileName, readFileToByteArray(storageFolderName+localPath + fileName),null);
ExportOptions options = new ExportOptions();
// Split the 2nd and 3rd slides and save them to PNG format.
SplitDocumentResult response = presentationApi.split(fileName, options, SlideExportFormat.PNG,720, 540,2, 3,
storageFolderName, null, storageFolderName, null, null);
for (ResourceUri slide : response.getSlides())
{
System.out.println(slide.getHref());
String imageName = slide.getHref().substring(slide.getHref().lastIndexOf('/') + 1);
File imageFile = presentationApi.downloadFile(storageFolderName + imageName, null, null);
// Copy the downloaded presentation to the local directory
copyFile(imageFile, new File(localPath, imageName));
}
System.out.println("Presentation splitted to image copied to: " + localPath);
}
public static byte[] readFileToByteArray(String filePath) throws IOException {
Path path = new File(filePath).toPath();
return Files.readAllBytes(path);
}
private void copyFile(File sourceFile, File targetFile) throws IOException {
if (sourceFile == null || !sourceFile.exists()) {
throw new IOException("Source file does not exist: " + sourceFile);
}
// Ensure the target directory exists
Path targetPath = targetFile.toPath();
Files.createDirectories(targetPath.getParent());
// Copy the file
Files.copy(sourceFile.toPath(), targetPath, StandardCopyOption.REPLACE_EXISTING);
}
}

此代码演示了如何使用 Java REST 接口在线开发 PowerPoint 拆分器。Split() 方法需要上传的文件名、使用 SlideExportFormat 枚举器的输出图像格式、起始索引和终止索引来定义幻灯片的范围。幻灯片集合中每个项目的 Href 包含对 API 创建的在线图像的引用,可用于下载图像。

本文教我们如何使用 Java RESTful Service 开发 PPTX 拆分器。如果您想合并演示文稿,请参阅 使用 Java REST API 合并演示文稿 上的文章。

 简体中文