Working with Hyperlinks

3 Aug 202623 minutes to read

A hyperlink is a reference that links to external content such as images, files, web pages, and more. In a PowerPoint slide, a hyperlink can target any of the following:

  • Webpage: Represents web content.
  • File: Represents a file in some location.
  • Email: Represents an email address.
  • Slide: Represents a slide in the same document.

You can navigate to any slide of the same PowerPoint document by attaching a slide-target hyperlink to a shape. The following code example demonstrates how to add a hyperlink for internal document navigation.

//Create a new PowerPoint Presentation instance.
IPresentation pptxDoc = Presentation.Create();
//Add a slide with a blank layout to the presentation.
ISlide slide1 = pptxDoc.Slides.Add(SlideLayoutType.Blank);
//Add a second slide that the hyperlink will target.
ISlide slide2 = pptxDoc.Slides.Add();
//Add a rectangle shape to the first slide.
IShape shape = slide1.Shapes.AddShape(AutoShapeType.Rectangle, 100, 20, 200, 100);
//Set the target slide index (index is valid from 0 to slides count – 1) as the hyperlink target.
IHyperLink hyperLink = shape.SetHyperlink("1");
//Get the target slide resolved by the hyperlink.
ISlide targetSlide = hyperLink.TargetSlide;
//Save the PowerPoint Presentation to a file.
pptxDoc.Save("Sample.pptx");
//Close the presentation.
pptxDoc.Close();
//Create a new PowerPoint Presentation instance.
IPresentation pptxDoc = Presentation.Create();
//Add a slide with a blank layout to the presentation.
ISlide slide1 = pptxDoc.Slides.Add(SlideLayoutType.Blank);
//Add a second slide that the hyperlink will target.
ISlide slide2 = pptxDoc.Slides.Add();
//Add a rectangle shape to the first slide.
IShape shape = slide1.Shapes.AddShape(AutoShapeType.Rectangle, 100, 20, 200, 100);
//Set the target slide index (index is valid from 0 to slides count – 1) as the hyperlink target.
IHyperLink hyperLink = shape.SetHyperlink("1");
//Get the target slide resolved by the hyperlink.
ISlide targetSlide = hyperLink.TargetSlide;
//Save the presentation.
pptxDoc.Save("Sample.pptx");
//Close the presentation.
pptxDoc.Close();
'Create a new PowerPoint Presentation instance.
Dim pptxDoc As IPresentation = Presentation.Create()
'Add a slide with a blank layout to the presentation.
Dim slide1 As ISlide = pptxDoc.Slides.Add(SlideLayoutType.Blank)
'Add a second slide that the hyperlink will target.
Dim slide2 As ISlide = pptxDoc.Slides.Add()
'Add a rectangle shape to the first slide.
Dim shape As IShape = slide1.Shapes.AddShape(AutoShapeType.Rectangle, 100, 20, 200, 100)
'Set the target slide index (index is valid from 0 to slides count – 1) as the hyperlink target.
Dim hyperLink As IHyperLink = shape.SetHyperlink("1")
'Get the target slide resolved by the hyperlink.
Dim targetSlide As ISlide = hyperLink.TargetSlide
'Save the presentation.
pptxDoc.Save("Sample.pptx")
'Close the presentation.
pptxDoc.Close()

You can download a complete working sample from GitHub.

You can link a shape’s text to a specified URL within a PowerPoint slide. The following code example demonstrates how to add a web URL as a hyperlink.

//Create a new PowerPoint Presentation instance.
IPresentation pptxDoc = Presentation.Create();
//Add a slide with a blank layout to the presentation.
ISlide slide = pptxDoc.Slides.Add(SlideLayoutType.Blank);
//Add a rectangle shape to the slide.
IShape shape = slide.Shapes.AddShape(AutoShapeType.Rectangle, 100, 20, 200, 100);
//Add a paragraph into the shape.
IParagraph paragraph = shape.TextBody.AddParagraph();
//Add text to the TextPart.
paragraph.Text = "Syncfusion";
//Set the web hyperlink to the TextPart.
IHyperLink hyperLink = paragraph.TextParts[0].SetHyperlink("http://www.syncfusion.com");
//Save the PowerPoint Presentation to a file.
pptxDoc.Save("Sample.pptx");
//Close the presentation.
pptxDoc.Close();
//Create a new PowerPoint Presentation instance.
IPresentation pptxDoc = Presentation.Create();
//Add a slide with a blank layout to the presentation.
ISlide slide = pptxDoc.Slides.Add(SlideLayoutType.Blank);
//Add a rectangle shape to the slide.
IShape shape = slide.Shapes.AddShape(AutoShapeType.Rectangle, 100, 20, 200, 100);
//Add a paragraph into the shape.
IParagraph paragraph = shape.TextBody.AddParagraph();
//Add text to the TextPart.
paragraph.Text = "Syncfusion";
//Set the web hyperlink to the TextPart.
IHyperLink hyperLink = paragraph.TextParts[0].SetHyperlink("http://www.syncfusion.com");
//Save the presentation.
pptxDoc.Save("Sample.pptx");
//Close the presentation.
pptxDoc.Close();
'Create a new PowerPoint Presentation instance.
Dim pptxDoc As IPresentation = Presentation.Create()
'Add a slide with a blank layout to the presentation.
Dim slide As ISlide = pptxDoc.Slides.Add(SlideLayoutType.Blank)
'Add a rectangle shape to the slide.
Dim shape As IShape = slide.Shapes.AddShape(AutoShapeType.Rectangle, 100, 20, 200, 100)
'Add a paragraph into the shape.
Dim paragraph As IParagraph = shape.TextBody.AddParagraph()
'Add text to the TextPart.
paragraph.Text = "Syncfusion"
'Set the web hyperlink to the TextPart.
Dim hyperLink As IHyperLink = paragraph.TextParts(0).SetHyperlink("http://www.syncfusion.com")
'Save the presentation.
pptxDoc.Save("Sample.pptx")
'Close the presentation.
pptxDoc.Close()

You can download a complete working sample from GitHub.

The following code example demonstrates how to add an email hyperlink to a picture.

//Create a new PowerPoint Presentation instance.
IPresentation pptxDoc = Presentation.Create();
//Add a slide with a blank layout to the presentation.
ISlide slide = pptxDoc.Slides.Add(SlideLayoutType.Blank);
//Open the picture file as a stream.
FileStream pictureStream = new FileStream("Image.png", FileMode.Open);
//Add the picture to the slide by specifying its size and position.
IPicture picture = slide.Pictures.AddPicture(pictureStream, 0, 0, 250, 250);
//Set the email hyperlink to the picture.
IHyperLink hyperLink = (picture as IShape).SetHyperlink("mailto:[email protected]");
//Save the PowerPoint Presentation to a file.
pptxDoc.Save("Sample.pptx");
//Close the presentation.
pptxDoc.Close();
//Create a new PowerPoint Presentation instance.
IPresentation pptxDoc = Presentation.Create();
//Add a slide with a blank layout to the presentation.
ISlide slide = pptxDoc.Slides.Add(SlideLayoutType.Blank);
//Open the picture file as a stream.
Stream pictureStream = File.Open("Image.png", FileMode.Open);
//Add the picture to the slide by specifying its size and position.
IPicture picture = slide.Pictures.AddPicture(pictureStream, 0, 0, 250, 250);
//Set the email hyperlink to the picture.
IHyperLink hyperLink = (picture as IShape).SetHyperlink("mailto:[email protected]");
//Save the presentation.
pptxDoc.Save("Sample.pptx");
//Close the presentation.
pptxDoc.Close();
'Create a new PowerPoint Presentation instance.
Dim pptxDoc As IPresentation = Presentation.Create()
'Add a slide with a blank layout to the presentation.
Dim slide As ISlide = pptxDoc.Slides.Add(SlideLayoutType.Blank)
'Open the picture file as a stream.
Dim pictureStream As Stream = File.Open("Image.png", FileMode.Open)
'Add the picture to the slide by specifying its size and position.
Dim picture As IPicture = slide.Pictures.AddPicture(pictureStream, 0, 0, 250, 250)
'Set the email hyperlink to the picture.
Dim hyperLink As IHyperLink = TryCast(picture, IShape).SetHyperlink("mailto:[email protected]")
'Save the presentation.
pptxDoc.Save("Sample.pptx")
'Close the presentation.
pptxDoc.Close()

You can download a complete working sample from GitHub.

You can open external documents like images, text files, PDF, etc. from the PowerPoint slide. The following code example demonstrates how to add a file hyperlink to the picture.

//Create a new PowerPoint Presentation instance.
IPresentation pptxDoc = Presentation.Create();
//Add a slide with a blank layout to the presentation.
ISlide slide = pptxDoc.Slides.Add(SlideLayoutType.Blank);
//Open the picture file as a stream.
FileStream pictureStream = new FileStream("Image.png", FileMode.Open);
//Add the picture to the slide by specifying its size and position.
IPicture picture = slide.Pictures.AddPicture(pictureStream, 0, 0, 250, 250);
//Set the file path as the hyperlink target.
IHyperLink hyperLink = (picture as IShape).SetHyperlink("WordDocument.docx");
//Save the PowerPoint Presentation to a file.
pptxDoc.Save("Sample.pptx");
//Close the presentation.
pptxDoc.Close();
//Create a new PowerPoint Presentation instance.
IPresentation pptxDoc = Presentation.Create();
//Add a slide with a blank layout to the presentation.
ISlide slide = pptxDoc.Slides.Add(SlideLayoutType.Blank);
//Open the picture file as a stream.
Stream pictureStream = File.Open("Image.png", FileMode.Open);
//Add the picture to the slide by specifying its size and position.
IPicture picture = slide.Pictures.AddPicture(pictureStream, 0, 0, 250, 250);
//Set the file path as the hyperlink target.
IHyperLink hyperLink = (picture as IShape).SetHyperlink("WordDocument.docx");
//Save the presentation.
pptxDoc.Save("Sample.pptx");
//Close the presentation.
pptxDoc.Close();
'Create a new PowerPoint Presentation instance.
Dim pptxDoc As IPresentation = Presentation.Create()
'Add a slide with a blank layout to the presentation.
Dim slide As ISlide = pptxDoc.Slides.Add(SlideLayoutType.Blank)
'Open the picture file as a stream.
Dim pictureStream As Stream = File.Open("Image.png", FileMode.Open)
'Add the picture to the slide by specifying its size and position.
Dim picture As IPicture = slide.Pictures.AddPicture(pictureStream, 0, 0, 250, 250)
'Set the file path as the hyperlink target.
Dim hyperLink As IHyperLink = TryCast(picture, IShape).SetHyperlink("WordDocument.docx")
'Save the presentation.
pptxDoc.Save("Sample.pptx")
'Close the presentation.
pptxDoc.Close()

You can download a complete working sample from GitHub.

NOTE

The above link makes use of the absolute path of the file for navigation. So, moving the files to another machine or location may lead to file not found error in PowerPoint reader applications.

You can read the hyperlink details from a shape or from the text of a shape in a PowerPoint slide.

The following code example demonstrates how to get the details about the hyperlink from a shape.

//Open an existing PowerPoint presentation.
IPresentation pptxDoc = Presentation.Open("Sample.pptx");
//Retrieve the first slide from the presentation.
ISlide slide = pptxDoc.Slides[0];
//Retrieve the first shape from the slide.
IShape shape = slide.Shapes[0] as IShape;
//Get the hyperlink from the shape.
IHyperLink hyperlink = shape.Hyperlink;
//Get the type of action performed when the hyperlink is clicked.
HyperLinkType hyperlinkType = hyperlink.Action;
//Get the target slide of the hyperlink.
ISlide targetSlide = hyperlink.TargetSlide;
//Get the URL address of the hyperlink.
string url = hyperlink.Url;
//Get the screen tip text of the hyperlink.
string screenTip = hyperlink.ScreenTip;
//Save the PowerPoint Presentation to a file.
pptxDoc.Save("Result.pptx");
//Close the presentation.
pptxDoc.Close();
//Open an existing PowerPoint presentation.
IPresentation pptxDoc = Presentation.Open("Sample.pptx");
//Retrieve the first slide from the presentation.
ISlide slide = pptxDoc.Slides[0];
//Retrieve the first shape from the slide.
IShape shape = slide.Shapes[0] as IShape;
//Get the hyperlink from the shape.
IHyperLink hyperlink = shape.Hyperlink;
//Get the type of action performed when the hyperlink is clicked.
HyperLinkType hyperlinkType = hyperlink.Action;
//Get the target slide of the hyperlink.
ISlide targetSlide = hyperlink.TargetSlide;
//Get the URL address of the hyperlink.
string url = hyperlink.Url;
//Get the screen tip text of the hyperlink.
string screenTip = hyperlink.ScreenTip;
//Save the presentation.
pptxDoc.Save("Result.pptx");
//Close the presentation.
pptxDoc.Close();
'Open an existing PowerPoint presentation.
Dim pptxDoc As IPresentation = Presentation.Open("Sample.pptx")
'Retrieve the first slide from the presentation.
Dim slide As ISlide = pptxDoc.Slides(0)
'Retrieve the first shape from the slide.
Dim shape As IShape = TryCast(slide.Shapes(0), IShape)
'Get the hyperlink from the shape.
Dim hyperlink As IHyperLink = shape.Hyperlink
'Get the type of action performed when the hyperlink is clicked.
Dim hyperlinkType As HyperLinkType = hyperlink.Action
'Get the target slide of the hyperlink.
Dim targetSlide As ISlide = hyperlink.TargetSlide
'Get the URL address of the hyperlink.
Dim url As String = hyperlink.Url
'Get the screen tip text of the hyperlink.
Dim screenTip As String = hyperlink.ScreenTip
'Save the presentation.
pptxDoc.Save("Result.pptx")
'Close the presentation.
pptxDoc.Close()

You can download a complete working sample from GitHub.

The following code example demonstrates how to get the details about the hyperlink from text.

//Open an existing PowerPoint presentation.
IPresentation pptxDoc = Presentation.Open("Sample.pptx");
//Retrieve the first slide from the presentation.
ISlide slide = pptxDoc.Slides[0];
//Retrieve the first shape from the slide.
IShape shape = slide.Shapes[0] as IShape;
//Retrieve the first paragraph of the shape.
IParagraph paragraph = shape.TextBody.Paragraphs[0];
//Retrieve the first TextPart of the shape.
ITextPart textPart = paragraph.TextParts[0];
//Get the hyperlink from the text.
IHyperLink hyperlink = textPart.Hyperlink;
//Get the type of action performed when the hyperlink is clicked.
HyperLinkType hyperlinkType = hyperlink.Action;
//Get the target slide of the hyperlink.
ISlide targetSlide = hyperlink.TargetSlide;
//Get the URL address of the hyperlink.
string url = hyperlink.Url;
//Get the screen tip text of the hyperlink.
string screenTip = hyperlink.ScreenTip;
//Save the PowerPoint Presentation to a file.
pptxDoc.Save("Result.pptx");
//Close the presentation.
pptxDoc.Close();
//Open an existing PowerPoint presentation.
IPresentation pptxDoc = Presentation.Open("Sample.pptx");
//Retrieve the first slide from the presentation.
ISlide slide = pptxDoc.Slides[0];
//Retrieve the first shape from the slide.
IShape shape = slide.Shapes[0] as IShape;
//Retrieve the first paragraph of the shape.
IParagraph paragraph = shape.TextBody.Paragraphs[0];
//Retrieve the first TextPart of the shape.
ITextPart textPart = paragraph.TextParts[0];
//Get the hyperlink from the text.
IHyperLink hyperlink = textPart.Hyperlink;
//Get the type of action performed when the hyperlink is clicked.
HyperLinkType hyperlinkType = hyperlink.Action;
//Get the target slide of the hyperlink.
ISlide targetSlide = hyperlink.TargetSlide;
//Get the URL address of the hyperlink.
string url = hyperlink.Url;
//Get the screen tip text of the hyperlink.
string screenTip = hyperlink.ScreenTip;
//Save the presentation.
pptxDoc.Save("Result.pptx");
//Close the presentation.
pptxDoc.Close();
'Open an existing PowerPoint presentation.
Dim pptxDoc As IPresentation = Presentation.Open("Sample.pptx")
'Retrieve the first slide from the presentation.
Dim slide As ISlide = pptxDoc.Slides(0)
'Retrieve the first shape from the slide.
Dim shape As IShape = TryCast(slide.Shapes(0), IShape)
'Retrieve the first paragraph of the shape.
Dim paragraph As IParagraph = shape.TextBody.Paragraphs(0)
'Retrieve the first TextPart of the shape.
Dim textPart As ITextPart = paragraph.TextParts(0)
'Get the hyperlink from the text.
Dim hyperlink As IHyperLink = textPart.Hyperlink
'Get the type of action performed when the hyperlink is clicked.
Dim hyperlinkType As HyperLinkType = hyperlink.Action
'Get the target slide of the hyperlink.
Dim targetSlide As ISlide = hyperlink.TargetSlide
'Get the URL address of the hyperlink.
Dim url As String = hyperlink.Url
'Get the screen tip text of the hyperlink.
Dim screenTip As String = hyperlink.ScreenTip
'Save the presentation.
pptxDoc.Save("Result.pptx")
'Close the presentation.
pptxDoc.Close()

You can download a complete working sample from GitHub.

You can remove the hyperlink from the shape or text in a PowerPoint slide.

The following code example demonstrates how to remove a hyperlink from a shape in a PowerPoint slide.

//Open an existing PowerPoint presentation.
IPresentation pptxDoc = Presentation.Open("Sample.pptx");
//Retrieve the first slide from the presentation.
ISlide slide = pptxDoc.Slides[0];
//Retrieve the first shape from the slide.
IShape shape = slide.Shapes[0] as IShape;
//Remove the hyperlink from the shape.
shape.RemoveHyperlink();
//Save the PowerPoint Presentation to a file.
pptxDoc.Save("Result.pptx");
//Close the presentation.
pptxDoc.Close();
//Open an existing PowerPoint presentation.
IPresentation pptxDoc = Presentation.Open("Sample.pptx");
//Retrieve the first slide from the presentation.
ISlide slide = pptxDoc.Slides[0];
//Retrieve the first shape from the slide.
IShape shape = slide.Shapes[0] as IShape;
//Remove the hyperlink from the shape.
shape.RemoveHyperlink();
//Save the presentation.
pptxDoc.Save("Result.pptx");
//Close the presentation.
pptxDoc.Close();
'Open an existing PowerPoint presentation.
Dim pptxDoc As IPresentation = Presentation.Open("Sample.pptx")
'Retrieve the first slide from the presentation.
Dim slide As ISlide = pptxDoc.Slides(0)
'Retrieve the first shape from the slide.
Dim shape As IShape = TryCast(slide.Shapes(0), IShape)
'Remove the hyperlink from the shape.
shape.RemoveHyperlink()
'Save the presentation.
pptxDoc.Save("Result.pptx")
'Close the presentation.
pptxDoc.Close()

You can download a complete working sample from GitHub.

The following code example demonstrates how to remove a hyperlink from the text in a PowerPoint slide.

//Open an existing PowerPoint presentation.
IPresentation pptxDoc = Presentation.Open("Sample.pptx");
//Retrieve the first slide from the presentation.
ISlide slide = pptxDoc.Slides[0];
//Retrieve the first shape from the slide.
IShape shape = slide.Shapes[0] as IShape;
//Retrieve the first paragraph of the shape.
IParagraph paragraph = shape.TextBody.Paragraphs[0];
//Retrieve the first TextPart of the shape.
ITextPart textPart = paragraph.TextParts[0];
//Remove the hyperlink from the TextPart.
textPart.RemoveHyperLink();
//Save the PowerPoint Presentation to a file.
pptxDoc.Save("Result.pptx");
//Close the presentation.
pptxDoc.Close();
//Open an existing PowerPoint presentation.
IPresentation pptxDoc = Presentation.Open("Sample.pptx");
//Retrieve the first slide from the presentation.
ISlide slide = pptxDoc.Slides[0];
//Retrieve the first shape from the slide.
IShape shape = slide.Shapes[0] as IShape;
//Retrieve the first paragraph of the shape.
IParagraph paragraph = shape.TextBody.Paragraphs[0];
//Retrieve the first TextPart of the shape.
ITextPart textPart = paragraph.TextParts[0];
//Remove the hyperlink from the TextPart.
textPart.RemoveHyperLink();
//Save the presentation.
pptxDoc.Save("Result.pptx");
//Close the presentation.
pptxDoc.Close();
'Open an existing PowerPoint presentation.
Dim pptxDoc As IPresentation = Presentation.Open("Sample.pptx")
'Retrieve the first slide from the presentation.
Dim slide As ISlide = pptxDoc.Slides(0)
'Retrieve the first shape from the slide.
Dim shape As IShape = TryCast(slide.Shapes(0), IShape)
'Retrieve the first paragraph of the shape.
Dim paragraph As IParagraph = shape.TextBody.Paragraphs(0)
'Retrieve the first TextPart of the shape.
Dim textPart As ITextPart = paragraph.TextParts(0)
'Remove the hyperlink from the TextPart.
textPart.RemoveHyperLink()
'Save the presentation.
pptxDoc.Save("Result.pptx")
'Close the presentation.
pptxDoc.Close()

You can download a complete working sample from GitHub.

See also