请按照本指南了解如何使用 C# REST API 添加指向 PowerPoint 的超链接。您将学习如何使用基于 .NET 的 Cloud SDK(支持处理演示文稿的所有高级功能)自动使用 C# RESTful 服务在 PowerPoint 中插入超链接。它将帮助您选择特定的幻灯片和形状来添加自定义超链接。
先决条件
下载 Aspose.Slides Cloud SDK for Dotnet for inserting hyperlinks
使用上述 SDK 设置 C# 项目以添加形状链接
使用基于 C# .NET 的 API 添加到 PowerPoint 的链接的步骤
- 使用 API 密钥和密钥实例化 SlidesApi 以进行身份验证
- 将输入文件读入内存流并上传
- 设置幻灯片和形状的索引以添加超链接(从 1 开始的索引)
- 创建带有超链接的形状对象
- Update 带有超链接的指定幻灯片上的形状
- 将更新后的超链接的 URL 打印到控制台以进行验证
- 从云端以流的形式下载更新的演示文稿并将其保存在磁盘上
这些步骤说明如何使用 C# REST API 在 PowerPoint 中添加超链接。您可以通过定义操作类型和外部 URL 创建形状并添加超链接。最后,使用新创建的形状更新所需幻灯片中的目标形状,并根据需要将输出保存在磁盘上。
使用 C# 低代码 API 在 PowerPoint 中插入链接的代码
using Aspose.Slides.Cloud.Sdk; // Import the Aspose.Slides Cloud SDK to work with PowerPoint presentations. | |
using Aspose.Slides.Cloud.Sdk.Model; // Import models used by the SDK. | |
using System; // Import the System namespace for basic functionalities. | |
using System.IO; // Import System.IO for file input/output operations. | |
namespace AsposeTestCodes | |
{ | |
class Program | |
{ | |
static void Main(string[] args) // Main method - the program's entry point. | |
{ | |
// Instantiate the SlidesApi with the API key and secret for authentication. | |
SlidesApi slidesApi = new SlidesApi("Client ID", "Client Secret"); | |
// Name of the PowerPoint file to work with. | |
string fileName = "PresentationWithoutHyperlink.pptx"; | |
// Read the file into a memory stream and upload it to the Aspose.Slides cloud storage. | |
FilesUploadResult uploadedPresResult = slidesApi.UploadFile(fileName, new MemoryStream(File.ReadAllBytes(fileName))); | |
// 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 | |
{ | |
HyperlinkClick = new Hyperlink | |
{ | |
ActionType = Hyperlink.ActionTypeEnum.Hyperlink, // Set the action type as a hyperlink. | |
ExternalUrl = "https://docs.aspose.cloud/slides" // The URL for the hyperlink. | |
} | |
}; | |
// Update the shape on the specified slide with the hyperlink. | |
ShapeBase updatedShpWithHyperlink = slidesApi.UpdateShape(fileName, slideIndex, shapeIndex, shape); | |
// Print the updated hyperlink's URL to the console for verification. | |
Console.WriteLine(updatedShpWithHyperlink.HyperlinkClick.ExternalUrl); | |
// Download the updated presentation from the cloud as a stream. | |
Stream stream = slidesApi.DownloadFile(fileName); | |
// Save the downloaded presentation to the local file system with a new name. | |
var fs = new FileStream("PresWithHyperlinks.pptx", FileMode.Create, FileAccess.Write); | |
stream.CopyTo(fs); // Copy the content from the stream to the file. | |
} | |
} | |
} |
此代码演示如何使用 C# REST 接口在 PowerPoint 中插入超链接。您可以设置超链接的各种属性,例如用于启用/禁用超链接的标志、工具提示、历史记录、突出显示单击以及单击时停止声音。请注意,所有这些属性都可以通过将鼠标悬停在超链接上而不是单击它来使用。
本文教我们使用 C# 低代码 API 在 PowerPoint 中创建超链接。要在幻灯片中添加 SmartArt,请参阅有关 使用 C# REST API 将 SmartArt 添加到 PowerPoint 的文章。