Skip to main content

How to Add, Edit, and Remove Hyperlinks in PowerPoint Using Java

 Hyperlinks in PowerPoint can point to websites, external resources, or other slides in the same presentation. For example, you might link a reference document from a project report or create a clickable table of contents for navigating between sections.

Java lets you add hyperlinks to text and images, create links between slides, and update or remove existing links.

This article walks through each of these operations with complete Java examples.

Add, Edit, and Remove Hyperlinks in PowerPoint Using Java

1. Add the Maven Dependency

First, add a PowerPoint processing library to your Java project.

If you're using Maven, include the following repository and dependency in your pom.xml file:

<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <name>e-iceblue</name>
        <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
    </repository>
</repositories>

<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.presentation</artifactId>
        <version>11.9.10</version>
    </dependency>
</dependencies>

The examples below work with PPTX files. Each program saves its result to a separate file, so you can test the operations independently.

2. Add a Hyperlink to Text in PowerPoint

To make a piece of text clickable, you can assign a hyperlink to a PortionEx object, which represents a text range within a paragraph.

The following example creates a slide containing the text "View Project Resources" and links it to a web page.

import com.spire.presentation.*;
import com.spire.presentation.drawing.FillFormatType;

import java.awt.geom.Rectangle2D;

public class AddTextHyperlink {

    public static void main(String[] args) throws Exception {

        Presentation presentation = new Presentation();

        try {
            // Get the first slide
            ISlide slide = presentation.getSlides().get(0);

            // Add a text shape
            Rectangle2D.Double rect =
                    new Rectangle2D.Double(60, 100, 400, 100);

            IAutoShape shape = slide.getShapes()
                    .appendShape(ShapeType.RECTANGLE, rect);

            // Remove the shape fill and border
            shape.getFill().setFillType(FillFormatType.NONE);
            shape.getLine().setWidth(0);
            shape.getTextFrame().getParagraphs().clear();

            // Create a paragraph and text range
            ParagraphEx paragraph = new ParagraphEx();
            PortionEx text = new PortionEx();

            text.setText("View Project Resources");
            text.setFontHeight(22);

            // Set the hyperlink URL
            text.getClickAction().setAddress(
                    "https://www.example.com/");

            // Add the text to the shape
            paragraph.getTextRanges().append(text);

            shape.getTextFrame()
                    .getParagraphs().append(paragraph);

            // Save the presentation
            presentation.saveToFile(
                    "text-hyperlink.pptx",
                    FileFormat.PPTX_2013);

        } finally {
            presentation.dispose();
        }
    }
}

The key statement is:

text.getClickAction().setAddress("https://www.example.com/");

getClickAction() accesses the text's click action, while setAddress() assigns the destination URL.

The generated text-hyperlink.pptx contains the linked text. When you click it in Slide Show mode, PowerPoint opens the specified web page.

The URL in this example is a placeholder. Replace it with the actual destination when using the code.

3. Add a Hyperlink to an Image

Images can also act as hyperlinks. For example, a slide might contain a website logo or a document thumbnail that opens a related page when clicked.

Unlike text hyperlinks, which are associated with text ranges, image hyperlinks are attached to the image shape itself.

The following example inserts an image and assigns it a web address.

import com.spire.presentation.*;
import com.spire.presentation.drawing.FillFormatType;

import java.awt.geom.Rectangle2D;

public class AddImageHyperlink {

    public static void main(String[] args) throws Exception {

        Presentation presentation = new Presentation();

        try {
            // Get the first slide
            ISlide slide = presentation.getSlides().get(0);

            // Define the image position and size
            Rectangle2D.Double rect =
                    new Rectangle2D.Double(80, 100, 240, 120);

            // Insert an image
            IEmbedImage image = slide.getShapes()
                    .appendEmbedImage(
                            ShapeType.RECTANGLE,
                            "logo.png",
                            rect);

            // Remove the image border
            image.getLine().setFillType(FillFormatType.NONE);

            // Create a hyperlink
            ClickHyperlink hyperlink =
                    new ClickHyperlink(
                            "https://www.example.com/");

            // Assign the hyperlink to the image
            image.setClick(hyperlink);

            // Save the presentation
            presentation.saveToFile(
                    "image-hyperlink.pptx",
                    FileFormat.PPTX_2013);

        } finally {
            presentation.dispose();
        }
    }
}

Here, appendEmbedImage() inserts logo.png into the slide, and setClick() assigns a ClickHyperlink object to the image.

The resulting image is clickable during the presentation.

Make sure logo.png exists in your application's working directory, or replace it with an absolute file path.

4. Link to Another Slide in the Same Presentation

Hyperlinks don't have to point to external websites. They can also navigate between slides within the same PowerPoint file.

This is useful for presentations with a table of contents or sections that users may want to visit in a different order.

To create an internal hyperlink, use ClickHyperlink(ISlide) to specify the destination slide.

The following example creates a two-slide presentation and adds a link on the first slide that opens the second.

import com.spire.presentation.*;
import com.spire.presentation.drawing.FillFormatType;

import java.awt.geom.Rectangle2D;

public class AddSlideHyperlink {

    public static void main(String[] args) throws Exception {

        Presentation presentation = new Presentation();

        try {
            // Get the first slide
            ISlide firstSlide =
                    presentation.getSlides().get(0);

            // Add a second slide
            ISlide secondSlide =
                    presentation.getSlides().append();

            // Add content to the second slide
            Rectangle2D.Double targetRect =
                    new Rectangle2D.Double(60, 100, 450, 100);

            IAutoShape targetShape = secondSlide.getShapes()
                    .appendShape(ShapeType.RECTANGLE, targetRect);

            targetShape.getFill()
                    .setFillType(FillFormatType.NONE);
            targetShape.getLine().setWidth(0);

            targetShape.appendTextFrame("Project Details");

            // Add a text shape to the first slide
            Rectangle2D.Double linkRect =
                    new Rectangle2D.Double(60, 100, 450, 100);

            IAutoShape linkShape = firstSlide.getShapes()
                    .appendShape(ShapeType.RECTANGLE, linkRect);

            linkShape.getFill()
                    .setFillType(FillFormatType.NONE);
            linkShape.getLine().setWidth(0);
            linkShape.getTextFrame().getParagraphs().clear();

            // Create the clickable text
            ParagraphEx paragraph = new ParagraphEx();
            PortionEx text = new PortionEx();

            text.setText("Go to Project Details");
            text.setFontHeight(22);

            // Link to the second slide
            ClickHyperlink hyperlink =
                    new ClickHyperlink(secondSlide);

            text.setClickAction(hyperlink);

            paragraph.getTextRanges().append(text);

            linkShape.getTextFrame()
                    .getParagraphs().append(paragraph);

            // Save the presentation
            presentation.saveToFile(
                    "slide-hyperlink.pptx",
                    FileFormat.PPTX_2013);

        } finally {
            presentation.dispose();
        }
    }
}

The important part is:

ClickHyperlink hyperlink =
        new ClickHyperlink(secondSlide);

text.setClickAction(hyperlink);

Instead of passing a URL, we pass the destination slide as an ISlide object.

When the presentation is in Slide Show mode, clicking "Go to Project Details" navigates directly to the second slide.

You can use the same approach with an existing presentation. Retrieve the destination slide using presentation.getSlides().get(index) and assign it to a hyperlink.

Remember that slide indexes start at zero, so get(0) refers to the first slide and get(2) refers to the third.

5. Edit an Existing Hyperlink in PowerPoint

Links sometimes need to be updated when a website address changes or a referenced resource moves.

Rather than recreating the linked text, you can retrieve its text range and update the display text or destination URL.

The following example opens text-hyperlink.pptx, created in Section 2, and changes both the displayed text and the web address.

import com.spire.presentation.*;

public class EditPptHyperlink {

    public static void main(String[] args) throws Exception {

        Presentation presentation = new Presentation();

        try {
            // Load the presentation
            presentation.loadFromFile(
                    "text-hyperlink.pptx");

            // Get the shape containing the hyperlink
            IAutoShape shape = (IAutoShape) presentation
                    .getSlides().get(0)
                    .getShapes().get(0);

            // Update the hyperlink display text
            shape.getTextFrame()
                    .getTextRange()
                    .setText("View Updated Project Resources");

            // Update the hyperlink URL
            shape.getTextFrame()
                    .getTextRange()
                    .getClickAction()
                    .setAddress(
                            "https://www.wikipedia.org/");

            // Save the updated presentation
            presentation.saveToFile(
                    "edited-hyperlink.pptx",
                    FileFormat.PPTX_2013);

        } finally {
            presentation.dispose();
        }
    }
}

The code uses setText() to change the displayed text and getClickAction().setAddress() to update the link destination.

The resulting edited-hyperlink.pptx contains the updated hyperlink.

One thing to watch for: This example accesses the first shape on the first slide because the structure of the input file is known.

In an existing presentation, a text box may contain several text ranges, each with different formatting or hyperlinks. In that case, locate the specific linked text range before editing it rather than assuming the first shape or entire text frame contains the intended link.

6. Remove a Hyperlink Without Deleting the Text

If a hyperlink is no longer needed, you can remove its click action while keeping the text.

For text hyperlinks, this can be done using setClickAction(null).

The following example removes the hyperlink from text-hyperlink.pptx, leaving the displayed text unchanged.

import com.spire.presentation.*;

public class RemovePptHyperlink {

    public static void main(String[] args) throws Exception {

        Presentation presentation = new Presentation();

        try {
            // Load the presentation
            presentation.loadFromFile(
                    "text-hyperlink.pptx");

            // Get the shape containing the hyperlink
            IAutoShape shape = (IAutoShape) presentation
                    .getSlides().get(0)
                    .getShapes().get(0);

            // Remove the hyperlink
            shape.getTextFrame()
                    .getTextRange()
                    .setClickAction(null);

            // Save the presentation
            presentation.saveToFile(
                    "removed-hyperlink.pptx",
                    FileFormat.PPTX_2013);

        } finally {
            presentation.dispose();
        }
    }
}

The key statement is:

shape.getTextFrame().getTextRange().setClickAction(null);

Setting the click action to null removes the hyperlink from the text range without deleting the text itself.

The text may still retain its previous formatting, such as an underline or a hyperlink color. If you want it to look like ordinary text again, you may also need to adjust its formatting.

As with the previous example, this code assumes that the text shape and hyperlink are already known. For more complex presentations, identify the appropriate text range before removing its click action.

7. Things to Keep in Mind

Test Hyperlinks in Slide Show Mode

In PowerPoint's editing view, clicking text or an image usually selects the object rather than immediately following its hyperlink.

To check whether a hyperlink works, open the presentation in Slide Show mode and click the linked element.

External Links Depend on Their Destinations

A hyperlink may be stored correctly in the PPTX file even if its target website is unavailable or its address has changed.

Links to local files can also stop working when a presentation is moved to another computer or the referenced files are relocated.

Text and Shape Hyperlinks Are Different

Text hyperlinks are associated with text ranges, while image and shape hyperlinks are typically associated with the shape itself.

This distinction matters when editing or removing links. You need to identify whether the link belongs to a text range or the containing shape before changing its click action.

Be Careful When Working with Existing Slides

The examples for editing and removing hyperlinks access a shape by its index because they use a presentation with a known structure.

For real-world presentations containing multiple text boxes, images, charts, or grouped shapes, don't assume the target hyperlink is always in the first shape. Locate the relevant object and text range before making changes.

Comments

Popular posts from this blog

3 Ways to Generate Word Documents from Templates in Java

A template is a document with pre-applied formatting like styles, tabs, line spacing and so on. You can quickly generate a batch of documents with the same structure based on the template. In this article, I am going to show you the different ways to generate Word documents from templates programmatically in Java using Free Spire.Doc for Java library. Prerequisite First of all, you need to add needed dependencies for including Free Spire.Doc for Java into your Java project. There are two ways to do that. If you use maven, you need to add the following code to your project’s pom.xml file. <repositories>               <repository>                   <id>com.e-iceblue</id>                   <name>e-iceblue</name>...

Insert and Extract OLE objects in Word in Java

You can use OLE (Object Linking and Embedding) to include content from other programs, such as another Word document, an Excel or PowerPoint document to an existing Word document. This article demonstrates how to insert and extract embedded OLE objects in a Word document in Java by using Free Spire.Doc for Java API.   Add dependencies First of all, you need to add needed dependencies for including Free Spire.Doc for Java into your Java project. There are two ways to do that. If you use maven, you need to add the following code to your project’s pom.xml file.     <repositories>               <repository>                   <id>com.e-iceblue</id>                   <name>e-iceblue</name>    ...

Simple Java Code to Convert Excel to PDF in Java

This article demonstrates a simple solution to convert an Excel file to PDF in Java by using free Excel API – Free Spire.XLS for Java . The following examples illustrate two possibilities to convert Excel to PDF:      Convert the whole Excel file to PDF     Convert a particular Excel Worksheet to PDF Before start with coding, you need to Download Free Spire.XLS for Java package , unzip it and import Spire.Xls.jar file from the lib folder in your project as a denpendency. 1. Convert the whole Excel file to PDF Spire.XLS for Java provides saveToFile method in Workbook class that enables us to easily save a whole Excel file to PDF. import com.spire.xls.FileFormat; import com.spire.xls.Workbook; public class ExcelToPDF {     public static void main(String[] args){         //Create a Workbook         Workbook workbook = new Workbook();   ...