Manage Hyperlinks
A hyperlink is a reference to an object or data or a place in something. These are common hyperlinks in PowerPoint Presentations:
- Links to websites inside texts, shapes, or media
- Links to slides
Aspose.Slides for .NET allows you to perform many tasks involving hyperlinks in presentations.
Adding URL Hyperlinks
Adding URL Hyperlinks to Texts
This C# code shows you how to add a website hyperlink to a text:
using (Presentation presentation = new Presentation())
{
IAutoShape shape1 = presentation.Slides[0].Shapes.AddAutoShape(ShapeType.Rectangle, 100, 100, 600, 50, false);
shape1.AddTextFrame("Aspose: File Format APIs");
shape1.TextFrame.Paragraphs[0].Portions[0].PortionFormat.HyperlinkClick = new Hyperlink("https://www.aspose.com/");
shape1.TextFrame.Paragraphs[0].Portions[0].PortionFormat.HyperlinkClick.Tooltip = "More than 70% Fortune 100 companies trust Aspose APIs";
shape1.TextFrame.Paragraphs[0].Portions[0].PortionFormat.FontHeight = 32;
presentation.Save("presentation-out.pptx", SaveFormat.Pptx);
}
Adding URL Hyperlinks to Shapes or Frames
This sample code in C# shows you how to add a website hyperlink to a shape:
using (Presentation pres = new Presentation())
{
IShape shape = pres.Slides[0].Shapes.AddAutoShape(ShapeType.Rectangle, 100, 100, 600, 50);
shape.HyperlinkClick = new Hyperlink("https://www.aspose.com/");
shape.HyperlinkClick.Tooltip = "More than 70% Fortune 100 companies trust Aspose APIs";
pres.Save("pres-out.pptx", SaveFormat.Pptx);
}
Adding URL Hyperlinks to Media
Aspose.Slides allows you to add hyperlinks to images, audio, and video files.
This sample code shows you how to add a hyperlink to an image:
using (Presentation pres = new Presentation())
{
// Adds image to presentation
IPPImage image = pres.Images.AddImage(File.ReadAllBytes("image.png"));
// Creates picture frame on slide 1 based on previously added image
IPictureFrame pictureFrame = pres.Slides[0].Shapes.AddPictureFrame(ShapeType.Rectangle, 10, 10, 100, 100, image);
pictureFrame.HyperlinkClick = new Hyperlink("https://www.aspose.com/");
pictureFrame.HyperlinkClick.Tooltip = "More than 70% Fortune 100 companies trust Aspose APIs";
pres.Save("pres-out.pptx", SaveFormat.Pptx);
}
This sample code shows you how to add a hyperlink to an audio file:
using (Presentation pres = new Presentation())
{
IAudio audio = pres.Audios.AddAudio(File.ReadAllBytes("audio.mp3"));
IAudioFrame audioFrame = pres.Slides[0].Shapes.AddAudioFrameEmbedded(10, 10, 100, 100, audio);
audioFrame.HyperlinkClick = new Hyperlink("https://www.aspose.com/");
audioFrame.HyperlinkClick.Tooltip = "More than 70% Fortune 100 companies trust Aspose APIs";
pres.Save("pres-out.pptx", SaveFormat.Pptx);
}
This sample code shows you how to add a hyperlink to a video:
using (Presentation pres = new Presentation())
{
IVideo video = pres.Videos.AddVideo(File.ReadAllBytes("video.avi"));
IVideoFrame videoFrame = pres.Slides[0].Shapes.AddVideoFrame(10, 10, 100, 100, video);
videoFrame.HyperlinkClick = new Hyperlink("https://www.aspose.com/");
videoFrame.HyperlinkClick.Tooltip = "More than 70% Fortune 100 companies trust Aspose APIs";
pres.Save("pres-out.pptx", SaveFormat.Pptx);
}
Tip
You may want to see Manage OLE.Using Hyperlinks to Create Table of Contents
Since hyperlinks allow you to add references to objects or places, you can use them to create a table of contents.
This sample code shows you how to create a table of contents with hyperlinks:
using (var presentation = new Presentation())
{
var firstSlide = presentation.Slides[0];
var secondSlide = presentation.Slides.AddEmptySlide(firstSlide.LayoutSlide);
var contentTable = firstSlide.Shapes.AddAutoShape(ShapeType.Rectangle, 40, 40, 300, 100);
contentTable.FillFormat.FillType = FillType.NoFill;
contentTable.LineFormat.FillFormat.FillType = FillType.NoFill;
contentTable.TextFrame.Paragraphs.Clear();
var paragraph = new Paragraph();
paragraph.ParagraphFormat.DefaultPortionFormat.FillFormat.FillType = FillType.Solid;
paragraph.ParagraphFormat.DefaultPortionFormat.FillFormat.SolidFillColor.Color = Color.Black;
paragraph.Text = "Title of slide 2 .......... ";
var linkPortion = new Portion();
linkPortion.Text = "Page 2";
linkPortion.PortionFormat.HyperlinkManager.SetInternalHyperlinkClick(secondSlide);
paragraph.Portions.Add(linkPortion);
contentTable.TextFrame.Paragraphs.Add(paragraph);
presentation.Save("link_to_slide.pptx", SaveFormat.Pptx);
}
Formatting Hyperlinks
Color
With the ColorSource property in the IHyperlink interface, you can set the color for hyperlinks and also get the color information from hyperlinks. The feature was first introduced in PowerPoint 2019, so changes involving the property do not apply to older PowerPoint versions.
This sample code demonstrates an operation where hyperlinks with different colors got added to the same slide:
using (Presentation presentation = new Presentation())
{
IAutoShape shape1 = presentation.Slides[0].Shapes.AddAutoShape(ShapeType.Rectangle, 100, 100, 450, 50, false);
shape1.AddTextFrame("This is a sample of colored hyperlink.");
shape1.TextFrame.Paragraphs[0].Portions[0].PortionFormat.HyperlinkClick = new Hyperlink("https://www.aspose.com/");
shape1.TextFrame.Paragraphs[0].Portions[0].PortionFormat.HyperlinkClick.ColorSource = HyperlinkColorSource.PortionFormat;
shape1.TextFrame.Paragraphs[0].Portions[0].PortionFormat.FillFormat.FillType = FillType.Solid;
shape1.TextFrame.Paragraphs[0].Portions[0].PortionFormat.FillFormat.SolidFillColor.Color = Color.Red;
IAutoShape shape2 = presentation.Slides[0].Shapes.AddAutoShape(ShapeType.Rectangle, 100, 200, 450, 50, false);
shape2.AddTextFrame("This is a sample of usual hyperlink.");
shape2.TextFrame.Paragraphs[0].Portions[0].PortionFormat.HyperlinkClick = new Hyperlink("https://www.aspose.com/");
presentation.Save("presentation-out-hyperlink.pptx", SaveFormat.Pptx);
}
Sound
Aspose.Slides provides these properties to allow you emphasize a hyperlink with a sound:
Add Hyperlink Sound
This C# code shows you how to set the hyperlink that plays a sound and stop it with another hyperlink:
using (Presentation pres = new Presentation())
{
// Adds new audio to the presentation audio collection
IAudio playSound = pres.Audios.AddAudio(File.ReadAllBytes("sampleaudio.wav"));
ISlide firstSlide = pres.Slides[0];
// Adds new shape with the hyperlink to the next slide
IShape firstShape = firstSlide.Shapes.AddAutoShape(ShapeType.SoundButton, 100, 100, 100, 50);
firstShape.HyperlinkClick = Hyperlink.NextSlide;
// Сhecks the hyperlink for "No Sound"
if (!firstShape.HyperlinkClick.StopSoundOnClick && firstShape.HyperlinkClick.Sound == null)
{
// Sets the hyperlink that plays sound
firstShape.HyperlinkClick.Sound = playSound;
}
// Adds the empty slide
ISlide secondSlide = pres.Slides.AddEmptySlide(firstSlide.LayoutSlide);
// Adds new shape with the NoAction hyperlink
IShape secondShape = secondSlide.Shapes.AddAutoShape(ShapeType.Rectangle, 100, 100, 100, 50);
secondShape.HyperlinkClick = Hyperlink.NoAction;
// Sets the hyperlink "Stop previous sound" flag
secondShape.HyperlinkClick.StopSoundOnClick = true;
pres.Save("hyperlink-sound.pptx", SaveFormat.Pptx);
}
Extract Hyperlink Sound
This C# code shows you how to extract the sound used in an hyperlink:
using (Presentation pres = new Presentation("hyperlink-sound.pptx"))
{
ISlide firstSlide = pres.Slides[0];
// Gets the first shape hyperlink
IHyperlink link = firstSlide.Shapes[0].HyperlinkClick;
if (link.Sound != null)
{
// Extracts the hyperlink sound in byte array
byte[] audioData = link.Sound.BinaryData;
}
}
Removing Hyperlinks in Presentations
Removing Hyperlinks from Texts
This C# code shows you how to remove the hyperlink from a text in a presentation slide:
using (Presentation pres = new Presentation("pres.pptx"))
{
ISlide slide = pres.Slides[0];
foreach (IShape shape in slide.Shapes)
{
IAutoShape autoShape = shape as IAutoShape;
if (autoShape != null)
{
foreach (IParagraph paragraph in autoShape.TextFrame.Paragraphs)
{
foreach (IPortion portion in paragraph.Portions)
{
portion.PortionFormat.HyperlinkManager.RemoveHyperlinkClick();
}
}
}
}
pres.Save("pres-removed-hyperlinks.pptx", SaveFormat.Pptx);
}
Removing Hyperlinks from Shapes or Frames
This C# code shows you how to remove the hyperlink from a shape in a presentation slide:
using (Presentation pres = new Presentation("demo.pptx"))
{
ISlide slide = pres.Slides[0];
foreach (IShape shape in slide.Shapes)
{
shape.HyperlinkManager.RemoveHyperlinkClick();
}
pres.Save("pres-removed-hyperlinks.pptx", SaveFormat.Pptx);
}
Mutable Hyperlink
The Hyperlink class is mutable. With this class, you can change the values for these properties:
The code snippet shows you how to add a hyperlink to a slide and edit its tooltip later:
using (Presentation presentation = new Presentation())
{
IAutoShape shape1 = presentation.Slides[0].Shapes.AddAutoShape(ShapeType.Rectangle, 100, 100, 600, 50, false);
shape1.AddTextFrame("Aspose: File Format APIs");
shape1.TextFrame.Paragraphs[0].Portions[0].PortionFormat.HyperlinkClick = new Hyperlink("https://www.aspose.com/");
shape1.TextFrame.Paragraphs[0].Portions[0].PortionFormat.HyperlinkClick.Tooltip = "More than 70% Fortune 100 companies trust Aspose APIs";
shape1.TextFrame.Paragraphs[0].Portions[0].PortionFormat.FontHeight = 32;
presentation.Save("presentation-out.pptx", SaveFormat.Pptx);
}
Supported Properties in IHyperlinkQueries
You can access IHyperlinkQueries from a presentation, slide, or text for which the hyperlink is defined.
The IHyperlinkQueries class supports these methods and properties: