这个小教程提供了有关如何使用 C#** 将超链接添加到 PowerPoint 中的图像的信息,以及有关配置、逐步过程和可运行示例代码的所有必需信息以创建超链接使用 C# 获取 PPTX 中的图像。示例代码展示了一个完整的场景,可用于任何操作系统,如 Windows、macOS 或 Linux。
在 C# 中将超链接添加到 PPT 中的图像的步骤
- 配置环境以将 Aspose.Slides for .NET 添加到您的应用程序中
- 创建一个新的空 Presentation 对象
- 访问演示幻灯片集合中的第一个 slide
- 从磁盘读取源 PNG 图像作为字节数组
- 在演示图像集合中添加图像并使用 IPPImage 类对象访问它
- 使用上面添加的图像在所选幻灯片的形状集合中插入相框
- 使用 Hyperlink 类为相框形状添加外部超链接并设置超链接属性
- 将演示文稿另存为 PPTX,其中包含 PNG 图像的超链接
上述步骤指导使用 C# 将超链接插入到 PPTX 中的图像,该过程将通过使用 Presentation 类创建一个空演示文稿并访问演示文稿的幻灯片集合中的第一张默认幻灯片来开始。随后,将 PNG 图像作为相框添加到所选幻灯片中,然后使用 Hyperlink 类对象为添加的图像设置外部网站超链接。
使用 C# 将超链接插入到 PPTX 中的图像的代码
using System; | |
using System.IO; | |
using Aspose.Slides; | |
using Aspose.Slides.Export; | |
namespace TestSlides | |
{ | |
public class InsertHyperlink | |
{ | |
public static void AddImageHyperlink() // Function to add hyperlink to an image in PPTX in C# | |
{ | |
// Load the product license | |
Aspose.Slides.License lic = new Aspose.Slides.License(); | |
lic.SetLicense("Aspose.Total.lic"); | |
// Using Presentation class object create an empty presentation | |
using (Presentation presentationWithHyperlink = new Presentation()) | |
{ | |
// Access the first slide inside the slides collection | |
ISlide slideForPng = presentationWithHyperlink.Slides[0]; | |
// Add the Image from the disk in the images collection of the presentation | |
IPPImage imageFromDisk = presentationWithHyperlink.Images.AddImage(File.ReadAllBytes("aspose_logo.png")); | |
// Insert a picture frame in the shapes collection of the slide | |
IPictureFrame pictureFrame = slideForPng.Shapes.AddPictureFrame(ShapeType.Rectangle, 20, 20, 90, 90, imageFromDisk); | |
// Insert the hyperlink for the added picture frame | |
pictureFrame.HyperlinkClick = new Hyperlink("https://www.aspose.com/"); | |
// Add a tooltip for the hyperlink | |
pictureFrame.HyperlinkClick.Tooltip = "More than 75% of Fortune 100 companies show trust in Aspose APIs"; | |
// Save the presentation with hyperlinked image on the disk | |
presentationWithHyperlink.Save("preswithHyperlink.pptx", SaveFormat.Pptx); | |
} | |
System.Console.WriteLine("Done"); | |
} | |
} | |
} |
在使用此功能在 C# 中插入到 PPTX 中的图像的超链接时,我们使用 Slide 对象来保存对目标幻灯片的引用,并使用 IPPImage 对象来保存对添加到图像集合中的新图像的引用。 Hyperlink 类对象用于通过设置其属性(如外部链接和工具提示文本)来设置添加的图像形状的超链接。您还可以将超链接设置为演示文稿内的内部幻灯片。
在本教程中,我们学习了使用 C# 将超链接添加到 PPT 中的图像。如果您想将 PDF 转换为 PowerPoint 演示文稿,请参阅 如何使用 C# 将 PDF 转换为演示文稿 上的文章。