Add Hyperlinks to a PowerPoint Document in Java
Tags: java, powerpoint, hyperlink
Published: 2020-09-04 08:02:59

---
title: Add Hyperlinks to a PowerPoint Document in Java
published: true
description: This article introduces how to add hyperlinks to a PowerPoint document in Java.
tags: java, powerpoint, hyperlink
//cover_image: https://direct_url_to_image.jpg
---

In Microsoft PowerPoint, you can add hyperlinks to text or images to link to websites, email addresses or even link to a place in the same document such as a specific slide. In this article, I will introduce how to add hyperlinks to a PowerPoint document programmatically in Java by using Free Spire.Presentation for Java API.

**Contents Summary:**
1. Add hyperlink to text
2. Add hyperlink to image
3. Add hyperlink to link to a specific slide

##Add Dependencies##
First of all, you need to add needed dependencies for including Free Spire.Presentation 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.
```java
<repositories>  
        <repository>  
            <id>com.e-iceblue</id>  
            <name>e-iceblue</name>  
            <url>http://repo.e-iceblue.com/nexus/content/groups/public/</url>  
        </repository>  
</repositories>  
<dependencies>  
    <dependency>  
        <groupId>e-iceblue</groupId>  
        <artifactId>spire.presentation.free</artifactId>  
        <version>2.6.1</version>  
    </dependency>  
</dependencies>
```
For non-maven projects, download Free Spire.Presentation for Java pack from [this website](https://www.e-iceblue.com/Download/presentation-for-java-free.html), unzip the package and add Spire.Presentation.jar in the lib folder into your project as a dependency.
##Add hyperlink to text##
```java
import com.spire.presentation.*;
import com.spire.presentation.drawing.FillFormatType;

import java.awt.geom.Rectangle2D;

public class TextHyperlink {
    public static void main(String[] args) throws Exception {
        //create a Presentation instance
        Presentation presentation = new Presentation();

        //add a shape to the first slide 
        Rectangle2D.Double rec = new Rectangle2D.Double(presentation.getSlideSize().getSize().getWidth() / 2 - 255, 120, 500, 280);
        IAutoShape shape = presentation.getSlides().get(0).getShapes().appendShape(ShapeType.RECTANGLE, rec);
        shape.getFill().setFillType(FillFormatType.NONE);
        shape.getLine().setFillType(FillFormatType.NONE);

        //add some paragraphs with hyperlinks to the shape
        ParagraphEx para1 = new ParagraphEx();
        PortionEx tr1 = new PortionEx();
        tr1.setText("Click to visit Google.");
        //link to a website
        tr1.getClickAction().setAddress("https://www.google.com");
        para1.getTextRanges().append(tr1);
        shape.getTextFrame().getParagraphs().append(para1);
        shape.getTextFrame().getParagraphs().append(new ParagraphEx());

        ParagraphEx para2 = new ParagraphEx();
        PortionEx tr2 = new PortionEx();
        tr2.setText("Contact Google via email.");
        //link to an email address
        tr2.getClickAction().setAddress("mailto:contactxxx@google.com");
        para2.getTextRanges().append(tr2);
        shape.getTextFrame().getParagraphs().append(para2);
        shape.getTextFrame().getParagraphs().append(new ParagraphEx());

        //loop through the paragraphs in the shape, set font name and height for the text in each paragraph
        for (Object para : shape.getTextFrame().getParagraphs()) {
            ParagraphEx paragraph = (ParagraphEx) para;
            String text = paragraph.getText();
            if (text != null && text.length() != 0) {
                paragraph.getTextRanges().get(0).setLatinFont(new TextFont("Lucida Sans Unicode"));
                paragraph.getTextRanges().get(0).setFontHeight(20);
            }
        }

        //save the document
        presentation.saveToFile("AddHyperlinktoText.pptx", FileFormat.PPTX_2010);
    }
}
```
Output:
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/f0aeuw9xhyfqjplnvb3i.png) 
##Add hyperlink to image##
```java
import com.spire.presentation.*;

import java.awt.geom.Rectangle2D;

public class ImageHyperlink {
    public static void main(String[] args) throws Exception {
        //create a Presentation instance
        Presentation presentation = new Presentation();

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

        //add an image to the slide
        Rectangle2D.Double rect = new Rectangle2D.Double(100, 150, 200, 100);
        IEmbedImage image = slide.getShapes().appendEmbedImage(ShapeType.RECTANGLE, "logo.png", rect);

        //add hyperlink to the image
        ClickHyperlink hyperlink = new ClickHyperlink("https://www.google.com");
        image.setClick(hyperlink);

        //save the document
        presentation.saveToFile("AddHyperlinkToImage.pptx", FileFormat.PPTX_2010);
    }
}
```
Output:
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/659wl314aa4g61eum5dt.png) 
##Add hyperlink to link to a specific slide##
```java
import com.spire.presentation.*;
import com.spire.presentation.drawing.FillFormatType;

import java.awt.*;

public class SlideHyperlink {
    public static void main(String[] args) throws Exception {
        //create a Presentation instance
        Presentation presentation = new Presentation();

        //append a new slide
        presentation.getSlides().append();

        //add a shape to the new added slide
        Rectangle rec = new Rectangle((int)presentation.getSlideSize().getSize().getWidth() / 2 - 250, 120, 400, 100);
        IAutoShape shape = presentation.getSlides().get(1).getShapes().appendShape(ShapeType.RECTANGLE, rec);
        shape.getFill().setFillType(FillFormatType.NONE);
        shape.getLine().setFillType(FillFormatType.NONE);
        shape.getTextFrame().setText("Jump to the first slide.");

        //add a hyperlink to the shape to link to the first slide
        ClickHyperlink hyperlink = new ClickHyperlink(presentation.getSlides().get(0));
        shape.setClick(hyperlink);
        shape.getTextFrame().getTextRange().setClickAction(hyperlink);

        //save the document
        presentation.saveToFile("AddHyperlinkToSlide.pptx", FileFormat.PPTX_2010);
    }
}
```
Output:
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/dpx30z56we7m1fd3o1c0.png)