A hyperlink is a clickable element, typically embedded within text or images, which allows users to navigate and access different webpages, documents, or resources. By adding hyperlinks in a PowerPoint presentation, users are able to easily visit related content while viewing or showing the slides, enhancing convenience during the presentation. In this article, we will show you how to add hyperlink to PowerPoint Presentation programmatically by using Spire.Presentation for .NET.
Install Spire.Presentation for .NET
To begin with, you need to add the DLL files included in the Spire.Presentation for.NET package as references in your .NET project. The DLLs files can be either downloaded from this link or installed via NuGet.
PM> Install-Package Spire.Presentation
Add Hyperlink to Text on Slide
Spire.Presentation for .NET allows users to insert hyperlink to text on slides easily by using TextRange.ClickAction.Address property. The following are detailed steps.
- Create a new PowerPoint presentation.
- Load a PowerPoint file using Presentation.LoadFromFile() method.
- Get the first slide using Presentation.Slides[] property.
- Add a rectangle shape to the slide by using ISlide.Shapes.AppendShape() method.
- Remove the default paragraphs in the shape.
- Create a TextParagraph instance to represent a text paragraph.
- Create a TextRange instance to represent a text range and set link address for it by TextRange.ClickAction.Address property.
- Create an another TextRange instance to represent the rest of the text.
- Append text ranges to paragraph using TextParagraph.TextRanges.Append() method.
- Append the paragraph to the shape using IAutoShape.TextFrame.Paragraphs.Append() method.
- Loop through all text ranges in each paragraph and set font for them.
- Save the result file using Presentation.SaveToFile() method.
- C#
- VB.NET
using Spire.Presentation; using Spire.Presentation.Drawing; using System.Drawing; namespace Hyperlink { internal class Program { static void Main(string[] args) { //Create a Presentation instance Presentation presentation = new Presentation(); //Load the PowerPoint file presentation.LoadFromFile("sample.pptx", FileFormat.Pptx2010); //Get the first slide of the presentation ISlide slide = presentation.Slides[0]; //Add a shape to the slide RectangleF rec = new RectangleF(presentation.SlideSize.Size.Width / 2 - 120, 200, 500, 150); IAutoShape shape = slide.Shapes.AppendShape(ShapeType.Rectangle, rec); shape.Fill.FillType = FillFormatType.None; shape.ShapeStyle.LineColor.Color = Color.White; //Remove the default paragraphs in the shape shape.TextFrame.Paragraphs.Clear(); //Create a TextParagraph instance TextParagraph para = new TextParagraph(); //Create a TextRange instance TextRange tr = new TextRange("Spire.Presentation for .NET"); //Set a hyperlink address for the text range tr.ClickAction.Address = "http://www.e-iceblue.com/Introduce/presentation-for-net-introduce.html"; //Append the text range to the paragraph para.TextRanges.Append(tr); //Create a TextRange instance tr = new TextRange("is a professional PowerPoint® compatible API that enables developers to create, read, modify, convert and Print PowerPoint documents on any .NET platform." +"As an independent PowerPoint .NET API, Spire.Presentation for .NET doesn't need Microsoft PowerPoint to be installed on machines."); //Append the text range to the paragraph para.TextRanges.Append(tr); //Append the paragraph to the shape shape.TextFrame.Paragraphs.Append(para); //Loop through the paragraphs in the shape foreach (TextParagraph textPara in shape.TextFrame.Paragraphs) { if (!string.IsNullOrEmpty(textPara.Text)) { //Loop through the text ranges in each paragraph foreach (TextRange textRange in textPara.TextRanges) { //Set font textRange.LatinFont = new TextFont("Calibri"); textRange.FontHeight = 24; textRange.Fill.FillType = FillFormatType.Solid; textRange.Fill.SolidColor.Color = Color.Black; } } } //Save the presentation presentation.SaveToFile("TextHyperlink.pptx", FileFormat.Pptx2013); presentation.Dispose(); } } }
Add Hyperlink to Image on Slide
Spire.Presentation for .NET also supports adding a hyperlink to an image. You can create a hyperlink by using ClickHyperlink class and then add the hyperlink to the image using the IEmbedImage.Click property. The related steps are as follows.
- Create a new PowerPoint presentation.
- Load a PowerPoint file using Presentation.LoadFromFile() method.
- Get the second slide by using Presentation.Slides[] property.
- Add a image to the slide using ISlide.Shapes.AppendEmbedImage() method.
- Create a ClickHyperlink object and append the hyperlink to added image using IEmbedImage.Click property.
- Save the result file using Presentation.SaveToFile() method.
- C#
- VB.NET
using Spire.Presentation; using System.Drawing; namespace ImageHyperlink { class Program { static void Main(string[] args) { //Initialize an object of Presentation class Presentation presentation = new Presentation(); //Load the PowerPoint file presentation.LoadFromFile("TextHyperlink.pptx", FileFormat.Pptx2010); //Get the second slide of the presentation ISlide slide = presentation.Slides[1]; //Add a image to slide RectangleF rect = new RectangleF(100, 50, 150, 150); IEmbedImage image = slide.Shapes.AppendEmbedImage(ShapeType.Rectangle, @"logo.png", rect); //Add hyperlink to image ClickHyperlink hyperlink = new ClickHyperlink("http://www.e-iceblue.com/Introduce/presentation-for-net-introduce.html"); image.Click = hyperlink; //Save the result file presentation.SaveToFile("ImageHyperlink.pptx", FileFormat.Pptx2010); presentation.Dispose(); } } }
Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.