How to add hyperlink to the existing text on presentation slides in C#

With the help of Spire.Presentation, developers can insert new hyperlink into PowerPoint Presentation and edit the existing hyperlinks in C# and VB.NET. This article will show you how to add hyperlink to the existing text on presentation slides.

Firstly check the screenshot of the original presentation slides.

How to add hyperlink to the existing text on presentation slides in C#

Here comes to the steps of how to adding hyperlink to an existing text in Presentation slide:

Step 1: Create a new presentation document and load from file.

Presentation ppt = new Presentation();
ppt.LoadFromFile("Sample.pptx", FileFormat.Pptx2010);

Step 2: Find the text we want to add link to it.

IAutoShape shape = ppt.Slides[0].Shapes[0] as IAutoShape;
TextParagraph tp = shape.TextFrame.TextRange.Paragraph;
string temp = tp.Text;

Step 3: Adding the hyperlink to the existing text.

//split the original text
string textToLink = "Spire.Presentation";
string[] strSplit = temp.Split(new string[] { "Spire.Presentation" }, StringSplitOptions.None);

//Clear all text
tp.TextRanges.Clear();

//Add new text
TextRange tr = new TextRange(strSplit[0]);
tp.TextRanges.Append(tr);

//Add the hyperlink
tr = new TextRange(textToLink);
tr.ClickAction.Address = "http://www.e-iceblue.com/Introduce/presentation-for-net-introduce.html";

tp.TextRanges.Append(tr);

Step 4: Save the document to file.

ppt.SaveToFile("Result.pptx", FileFormat.Pptx2010);

Effective screenshot after adding the hyperlink to the existing text:

How to add hyperlink to the existing text on presentation slides in C#

Full codes:

static void Main(string[] args)
{
    Presentation ppt = new Presentation();
    ppt.LoadFromFile("Sample.pptx", FileFormat.Pptx2010);

    IAutoShape shape = ppt.Slides[0].Shapes[0] as IAutoShape;
    TextParagraph tp = shape.TextFrame.TextRange.Paragraph;
    string temp = tp.Text;
    
    //split the original text
    string textToLink = "Spire.Presentation";
    string[] strSplit = temp.Split(new string[] { "Spire.Presentation" }, StringSplitOptions.None);

    //Clear all text
    tp.TextRanges.Clear();

    //Add new text
    TextRange tr = new TextRange(strSplit[0]);
    tp.TextRanges.Append(tr);

    //Add the hyperlink
    tr = new TextRange(textToLink);
    tr.ClickAction.Address = "http://www.e-iceblue.com/Introduce/presentation-for-net-introduce.html";

    tp.TextRanges.Append(tr);

    ppt.SaveToFile("Result.pptx", FileFormat.Pptx2010);

}