请按照本指南了解如何使用 Java REST API 添加指向 PowerPoint 的超链接。您将学习使用基于 Java 的 Cloud SDK(支持处理演示文稿的所有高级功能)自动使用 Java RESTful 服务在 PowerPoint 中插入超链接。它将帮助您选择特定的幻灯片和形状来添加自定义超链接。
先决条件
下载 Aspose.Slides Cloud SDK for Java for inserting hyperlinks
使用上述 SDK 设置 Java 项目以添加形状链接
使用基于 Java 的 API 添加 PowerPoint 链接的步骤
- 使用 API 密钥和密钥实例化 SlidesApi 以进行身份验证
- 将输入文件读入内存流并上传
- 设置幻灯片和形状的索引以添加超链接(从 1 开始的索引)
- 创建带有超链接的形状对象
- Update 带有超链接的指定幻灯片上的形状
- 将更新后的超链接的 URL 打印到控制台以进行验证
- 从云端以流的形式下载更新的演示文稿并将其保存在磁盘上
这些步骤说明如何使用 Java REST API 在 PowerPoint 中添加超链接。您可以通过定义操作类型和外部 URL 创建形状并添加超链接。最后,使用新创建的形状更新所需幻灯片中的目标形状,并根据需要将输出保存在磁盘上。
使用 Java 低代码 API 在 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_AddHyperlinkInPresentation { | |
protected static SlidesApi presentationApi; | |
public Example_AddHyperlinkInPresentation() { | |
if (presentationApi == null) { | |
presentationApi = new SlidesApi("appSid", "appKey"); | |
} | |
} | |
public void addHyperlinkInSlide() throws ApiException, IOException { | |
String localPath = "/home/downloads/"; | |
String fileName = "PresentationWithHyperlink.pptx"; | |
String storageFolderName = "TempTests"; | |
// Index of the slide and shape where the target shape is located (1-based index). | |
int slideIndex = 2, shapeIndex = 2; | |
Shape shape = new Shape(); // Create a shape object with a hyperlink | |
Hyperlink hyperlink = new Hyperlink(); | |
hyperlink.setActionType(Hyperlink.ActionTypeEnum.HYPERLINK);// Set the action type as a hyperlink. | |
hyperlink.setExternalUrl("https://docs.aspose.cloud/slides"); // The URL for the hyperlink. | |
shape.setHyperlinkClick(hyperlink); | |
// Update the shape on the specified slide with the hyperlink | |
ShapeBase updatedShpWithHyperlink = presentationApi.updateShape(fileName, slideIndex, shapeIndex, shape, | |
null, storageFolderName, null, null); | |
// Print the updated hyperlink's URL to the console for verification. | |
System.out.println(updatedShpWithHyperlink.getHyperlinkClick().getExternalUrl()); | |
File presentationFile = presentationApi.downloadFile(storageFolderName+"/"+fileName, null, null); | |
// Copy the downloaded presentation with new image shape to the local directory | |
copyFile(presentationFile, new File(localPath, fileName)); | |
System.out.println("Presentation slide with image shape 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 REST 接口在 PowerPoint 中插入超链接。您可以设置超链接的各种属性,例如用于启用/禁用超链接的标志、工具提示、历史记录、突出显示单击以及单击时停止声音。请注意,所有这些属性都可以通过将鼠标悬停在超链接上而不是单击它来使用。
本文教我们如何使用 Java 低代码 API 在 PowerPoint 中创建超链接。要在幻灯片中添加 SmartArt,请参阅有关 使用 Java REST API 将 SmartArt 添加到 PowerPoint 的文章。