Hyperlinks in documents are used to navigate from one location to another. Generally, in PowerPoint presentations, they are used to insert URLs of webpages. You can create a hyperlink of text, image, shape, or a media element in PowerPoint PPT slides. In this article, you will learn how to add hyperlinks to PowerPoint PPT in C#. Moreover, we will cover how to remove hyperlinks from PPT slides programmatically.
- .NET API to Work with Hyperlinks in PowerPoint
- Add a Hyperlink in PowerPoint PPT/PPTX
- Remove Hyperlinks from PowerPoint PPT
C# .NET API to Add or Remove Hyperlinks in PowerPoint PPT
To work with hyperlinks in PowerPoint presentations, we will use Aspose.Slides for .NET. It is a popular API that is used to create and modify PPT/PPTX presentations. Moreover, it lets you convert the presentations to other popular formats. You can download its DLL or install it from NuGet.
PM> Install-Package Aspose.Slides.NET
Add a Hyperlink in PowerPoint PPT in C#
You can add different types of hyperlinks in PowerPoint presentations using Aspose.Slides for .NET. The hyperlink could be a text, image, shape, audio, or video element. In the following sections, we will cover how to add all these hyperlinks in a PPT/PPTX presentation.
C# Add Text Hyperlink in PowerPoint PPT
The following are the steps to add a text hyperlink in a PowerPoint PPT/PPTX in C#.
- First, load the presentation file or create a new one using Presentation class.
- Then, add a rectangular auto shape to the shapes collection using ISlide.Shapes.AddAutoShape(ShapeType, single, single, single, single) method.
- Add text to the shape using IAutoShape.AddTextFrame(string) method.
- Create hyperlink using IAutoShape.TextFrame.Paragraphs[0].Portions[0].PortionFormat.HyperlinkClick property.
- Set other properties of the hyperlink such as the tooltip.
- Finally, save the presentation using Presentation.Save(string, SaveFormat) method.
The following code sample shows how to add a text hyperlink in a PowerPoint PPTX using C#.
Insert a Shape Hyperlink in PPT in C#
The following steps demonstrate how to insert a shape hyperlink in a PowerPoint presentation in C#.
- First, load the presentation file or create a new one using Presentation class.
- Then, add an auto shape of the desired type using ISlide.Shapes.AddAutoShape(ShapeType, single, single, single, single) method.
- Create hyperlink using IAutoShape.HyperlinkClick property.
- Finally, save the presentation using Presentation.Save(string, SaveFormat) method.
The following code sample shows how to create a hyperlink of shape in PowerPoint using C#.
Insert an Image Hyperlink in PowerPoint PPT
The following are the steps to insert an image hyperlink in a PowerPoint PPT in C#.
- First, load the presentation file or create a new one using Presentation class.
- Then, add image to the collection using Presentation.Images.AddImage() method.
- Add a picture frame to the desired slide using Presentation.Slides[index].Shapes.AddPictureFrame() method.
- Create hyperlink by setting IPictureFrame.HyperlinkClick property.
- Finally, save presentation using Presentation.Save(string, SaveFormat) method.
The following code sample shows how to create a hyperlink of an image in PowerPoint using C#.
C# Add an Audio Hyperlink in PPT
You can add an audio hyperlink to a PowerPoint PPT/PPTX by following the steps below:
- First, load the presentation file or create a new one using Presentation class.
- Then, add audio to the collection using Presentation.Audios.AddAudio() method.
- After that, add audio frame to the desired slide using Presentation.Slides[index].Shapes.AddAudioFrameEmbedded() method.
- Create hyperlink by setting IAudioFrame.HyperlinkClick property.
- Finally, save the presentation using Presentation.Save(string, SaveFormat) method.
The following code snippet demonstrates how to add an audio hyperlink in PowerPoint in C#:
Add a Video Hyperlink in PowerPoint PPT
To add a video hyperlink to a PowerPoint PPT/PPTX, follow the steps below:
- First, load the presentation file or create a new one using Presentation class.
- Then, add video to the collection using Presentation.Videos.AddVideo() method.
- Add video frame to the desired slide using Presentation.Slides[index].Shapes.AddVideoFrame() method.
- Create hyperlink by setting IVideoFrame.HyperlinkClick property.
- Finally, save the presentation using Presentation.Save(string, SaveFormat) method.
The following code snippet demonstrates how to add a video hyperlink in PowerPoint in C#:
Remove Hyperlinks from PowerPoint PPT in C#
In this section, we will demonstrate how to remove hyperlinks from a PowerPoint slide. These hyperlinks could be of any type we have discussed above. The following are the steps to perform this operation.
- First, load the presentation file using Presentation class.
- Then, loop through all the shapes in the slide using ISlide.Shapes collection.
- After that, remove the hyperlink from the shape using IShape.HyperlinkManager.RemoveHyperlinkClick() method.
- To remove text hyperlinks, cast shape to IAutoShape.
- Loop through the paragraphs of the shape.
- Loop through text portions of each paragraph.
- Remove text hyperlink using IPortion.PortionFormat.HyperlinkManager.RemoveHyperlinkClick() method.
- Finally, save the presentation using Presentation.Save(string, SaveFormat) method.
The following code sample shows how to remove hyperlinks from a slide in PowerPoint PPT in C#.
C# PowerPoint API - Get a Free License
You can work with hyperlinks in PowerPoint presentations without evaluation limitations by getting a free temporary license.
Conclusion
In this article, you have learned how to add or remove hyperlinks in PowerPoint PPT in C#. Moreover, we have covered how to make hyperlinks of text, image, shape, audio, and video elements. Besides, you can explore more about Aspose.Slides for .NET by visiting the documentation. Also, you can post your queries to our forum.