本文指导如何使用演示文稿设计模板和 Java REST API 创建演示文稿。您将学习使用基于 Java 的 SDK,使用幻灯片模板和 Java RESTful 服务创建 PowerPoint 演示文稿。它还将描述用于填充模板的 XML 脚本的创建。
先决条件
下载 Aspose.Slides Cloud SDK for Java to create a presentation from a template
使用上述 SDK 设置 Java 项目,以便从设计模板创建演示文稿
使用带有 Java REST 接口的模板创建演示文稿的步骤
- 通过使用客户端 ID 和密钥创建 SlidesApi 对象来设置环境
- 定义输入模板文件名和输出演示名称
- 使用UploadFile()方法将模板上传到云存储
- 创建或加载用于填充模板的 XML 文件
- 调用 CreatePresentationFromTemplate() 方法来使用 XML 数据并生成演示文稿
- 从云存储下载新创建的演示文稿文件
这些步骤总结了使用带有基于 Java 的 API 的模板创建演示文稿的过程。将模板文件上传到云存储,创建或加载模板的 XML 脚本,并通过提供输出演示文稿名称、上传的模板文件和 XML 数据来调用 CreatePresentationFromTemplate()。最后,通过从云存储下载新创建的演示文稿来保存输出文件。
使用带有 Java 低代码 API 的模板创建演示文稿的代码
import com.aspose.slides.ApiException; | |
import com.aspose.slides.api.SlidesApi; | |
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_CreatePresentationFromDesignTemplate { | |
protected static SlidesApi presentationApi; | |
public Example_CreatePresentationFromDesignTemplate() { | |
if (presentationApi == null) { | |
presentationApi = new SlidesApi("appSid", "appKey"); | |
} | |
} | |
public void CreatePresentationFromDesignTemplate() throws ApiException, IOException { | |
String localPath = "/home/downloads/"; | |
String templateFileName = "TemplatePres.pptx"; | |
String outputFileName = "GeneratedPres.pptx"; | |
String storageFolderName = "TempTests"; | |
String inputData = """ | |
<staff><person> | |
<staffName>Alice Smith</staffName> | |
<address><line1>25 Maple Avenue</line1><line2>New York</line2></address> | |
<phone>+789 654321</phone> | |
<bio>Hello, I'm Alice and this is my resume</bio> | |
<domains> | |
<domain><experience>Python</experience><grade>Expert</grade></domain> | |
<domain><experience>JavaScript</experience><grade>Intermediate</grade></domain> | |
<domain><experience>Ruby</experience><grade>Beginner</grade></domain> | |
</domains> | |
</person></staff> | |
"""; | |
var response = presentationApi.createPresentationFromTemplate( | |
outputFileName, templateFileName, inputData, null, null, false, | |
null, storageFolderName, null); | |
File presentationFile = presentationApi.downloadFile(storageFolderName+"/"+outputFileName, null, null); | |
// Copy the downloaded presentation with inserted HTML text to the local directory | |
copyFile(presentationFile, new File(localPath, outputFileName)); | |
System.out.println("Presentation created from Design Template is copied to: " + localPath + outputFileName); | |
} | |
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 API 的设计模板创建演示文稿。如果模板受密码保护,请在函数调用中设置密码。此外,如果需要,您还可以设置输出演示文件的密码。
本文教我们如何从模板创建演示文稿。要显示文档属性,请参阅文章使用 Java REST API 在 PowerPoint 中显示文档属性。