This small tutorial provides information about how to add Hyperlink to an Image in PowerPoint using C# along with all the required information about the configuration, step-wise process, and a runnable sample code to create Hyperlink for an Image in PPTX using C#. The sample code exhibits a complete scenario and can be used in any of the operating systems like Windows, macOS, or Linux.
Steps to add Hyperlink to an Image in PPT in C#
- Configure the environment to add Aspose.Slides for .NET into your application
- Create a new empty Presentation object
- Access the first slide in presentation slides collection
- Read the source PNG image as a byte array from the disk
- Add the image in the presentation Images collection and access that using IPPImage class object
- Insert a picture frame in the shapes collection of the selected slide using the added image above
- Add an external hyperlink for the picture frame shape using the Hyperlink class and set hyperlink properties
- Save the presentation as PPTX having a hyperlink for a PNG image in it
The aforementioned steps guide to insert hyperlink to an image in PPTX using C# where the process will commence by creating an empty presentation using the Presentation class and accessing the first default slide inside the slides collection of the presentation. Subsequently, a PNG image is added as a picture frame inside the selected slide, which is then followed by setting an external website hyperlink for the added image using the Hyperlink class object.
Code to Insert Hyperlink to an Image in PPTX using C#
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"); | |
} | |
} | |
} |
While working with this feature to insert hyperlink to an image in PPTX in C#, we have used the Slide object to hold the reference to the target slide and IPPImage object to hold the reference to the new image added inside the images collection. The Hyperlink class object is used to set the hyperlink for the added image shape by setting its properties like an external link and a tooltip text. You can also set the hyperlink to internal slides inside the presentation.
In this tutorial, we have learnt to add hyperlink to an image in PPT using C#. If you want to convert PDF to a PowerPoint presentation, refer to the article on how to convert PDF to Presentation using C#.