使用 Java REST API 将 PDF 转换为 PowerPoint 演示文稿

按照本文使用 Java REST API 将 PDF 转换为 PowerPoint 演示文稿。您将了解使用基于 Java 的 Cloud SDK 使用 Java Low Code API 将 PDF 插入 PowerPoint 的过程。将完整的 PDF 转换为演示文稿需要几次 API 调用。

先决条件

使用基于 Java 的 API 将 PDF 转换为 PowerPoint 演示文稿的步骤

  1. 使用客户端 ID 和密钥实例化 SlidesApi 对象,以将 PDF 转换为 PPTX
  2. 将源 PDF 文件加载到内存流中以转换为 PowerPoint 演示文稿
  3. 通过设置输出演示文稿名称和源 PDF 流来调用 ImportFromPdf() 方法
  4. 从云端下载演示文稿,其中包含 PDF 页面作为幻灯片

这些步骤说明如何使用 Java REST API* 将 *PDF 转换为 PowerPoint 演示文稿。初始化 SlidesApi 对象以使用此功能,将源 PDF 文件加载到内存流中,并通过提供输出演示文稿名称和包含 PDF 内容的流来调用 ImportFromPdf() 方法。您可以使用上面给出的输出演示文稿名称下载生成的演示文稿文件。

使用 Java REST API 将 PDF 导入 PowerPoint 的代码

import com.aspose.slides.ApiException;
import com.aspose.slides.api.SlidesApi;
import com.aspose.slides.model.Document;
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_ConvertPdfToPresentation {
protected static SlidesApi presentationApi;
public Example_ConvertPdfToPresentation() {
if (presentationApi == null) {
presentationApi = new SlidesApi("appSid", "appKey");
}
}
public void convertPdfToPresentation() throws ApiException, IOException {
String localPath = "/home/downloads/";
String fileName = "Sample.pptx";
String pdfFileName = "ShapeImage.pdf";
String storageFolderName = "TempTests";
Document result = presentationApi.importFromPdf(fileName, readFileToByteArray(localPath + pdfFileName),
null, null, storageFolderName, null); // Change PDF to PPTX
File presentationFile = presentationApi.downloadFile(storageFolderName+"/"+fileName, null, null);
// Copy the downloaded presentation with imported PDF to the local directory
copyFile(presentationFile, new File(localPath, fileName));
System.out.println("PDF imported to presentation and is copied to: " + localPath + fileName);
}
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 RESTful 服务* 将 *PDF 更改为 PowerPoint。您可以设置用于检测源 PDF 中的表格并在输出演示文稿中呈现的选项。如果受保护,还可以选择设置打开源 PDF 文件的密码。

本文向我们介绍了使用 Java REST Interface* 将 *PDF 保存为演示文稿的过程。要向演示文稿添加超链接,请参阅有关 使用 Java REST API 添加指向 PowerPoint 的超链接 的文章。

 简体中文