In various cases, we have to insert hyperlinks in the PowerPoint presentations, such as for providing the URL of a webpage. You can create a hyperlink of text, image, shape, or a media element in PowerPoint PPT slides. In this article, you will learn how to add hyperlinks to PowerPoint PPT/PPTX in Java. In the end, we will also discuss how to remove hyperlinks from PPT slides programmatically.
- Java API to Work with Hyperlinks in PowerPoint
- Add a Hyperlink in PowerPoint PPT/PPTX
- Remove Hyperlinks from PowerPoint PPT
Java API to Add or Remove Hyperlinks in PowerPoint PPT
To insert or remove hyperlinks in PowerPoint presentations, we will use Aspose.Slides for Java. The API provides a bunch of features to create and manipulate PPT/PPTX presentations seamlessly. Also, it allows you to convert the presentations to other document or image formats. You can download its JAR or install it using the following Maven configurations.
Repository:
<repository>
<id>AsposeJavaAPI</id>
<name>Aspose Java API</name>
<url>https://repository.aspose.com/repo/</url>
</repository>
Dependency:
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-slides</artifactId>
<version>22.2</version>
<classifier>jdk16</classifier>
</dependency>
Add a Hyperlink in PowerPoint PPT/PPTX in Java
The hyperlink in PowerPoint presentations can be added to a text, image, shape, audio, or video element. The following sections cover how to add hyperlinks to these elements in a PPT/PPTX presentation using Java.
Add Text Hyperlink in PowerPoint PPT
The following are the steps to add a text hyperlink in a PowerPoint PPT/PPTX in Java.
- First, load the presentation file or create a new one using Presentation class.
- Then, add a rectangular auto shape to the slide using addAutoShape(ShapeType, float, float, float, float) method.
- Add text to the shape using IAutoShape.addTextFrame(String) method.
- Get reference of IPortionFormat from the shape.
- Create hyperlink using IPortionFormat.setHyperlinkClick() method.
- Set other properties of hyperlink such as tooltip.
- Finally, save presentation using Presentation.save(String, SaveFormat) method.
The following code sample shows how to add a text hyperlink in a PowerPoint PPTX using Java.
Insert a Shape Hyperlink in PowerPoint
The following steps demonstrate how to insert a shape hyperlink in a PowerPoint presentation in Java.
- First, load the presentation file or create a new one using Presentation class.
- Then, add an auto shape of desired type using addAutoShape(ShapeType, float, float, float, float) method.
- Create hyperlink using IAutoShape.setHyperlinkClick() method.
- Finally, save presentation using Presentation.save(string, SaveFormat) method.
The following code sample shows how to create a hyperlink of shape in PowerPoint using Java.
Insert an Image Hyperlink in PowerPoint
The following are the steps to insert an image hyperlink in a PowerPoint PPT in Java.
- First, load the presentation file or create a new one using Presentation class.
- Then, add image to the collection using Presentation.getImages().addImage() method.
- After that, add an IPictureFrame to the desired slide using addPictureFrame() method.
- Create hyperlink using IPictureFrame.setHyperlinkClick() method.
- Finally, save presentation using Presentation.save(String, SaveFormat) method.
The following code sample shows how to create a hyperlink of an image in PowerPoint using Java.
Add an Audio Hyperlink in PPT
You can add an audio hyperlink to a PowerPoint PPT/PPTX by following the steps below:
- First, load the presentation file or create a new one using Presentation class.
- Then, add audio to the collection using Presentation.getAudios().addAudio() method.
- After that, add IAudioFrame to the desired slide using addAudioFrameEmbedded() method.
- Create hyperlink by using IAudioFrame.setHyperlinkClick() method.
- Finally, save presentation using Presentation.Save(String, SaveFormat) method.
The following code snippet demonstrates how to add an audio hyperlink in PowerPoint in Java.
Add a Video Hyperlink in PowerPoint PPT
To add a video hyperlink to a PowerPoint PPT/PPTX, follow the steps below:
- First, load the presentation file or create a new one using Presentation class.
- Then, add video to the collection using Presentation.getVideos().addVideo() method.
- Add IVideoFrame to the desired slide using addVideoFrame() method.
- Create hyperlink by using IVideoFrame.setHyperlinkClick() method.
- Finally, save presentation using Presentation.save(String, SaveFormat) method.
The following code snippet demonstrates how to add a video hyperlink in PowerPoint in Java:
Remove Hyperlinks from PowerPoint PPT in Java
In this section, we will demonstrate how to remove the hyperlinks from a PowerPoint slide. These hyperlinks could be of any type we have discussed above. The following are the steps to perform this operation.
- First, load the presentation file using Presentation class.
- Then, loop through all the shapes in the slide using ISlide.getShapes() method.
- After that, remove hyperlink from shape using IShape.getHyperlinkManager().removeHyperlinkClick() method.
- To remove text hyperlinks, cast shape to IAutoShape.
- Loop through the paragraphs of the shape.
- Loop through text portions of each paragraph.
- Remove text hyperlink using IPortion.getPortionFormat().getHyperlinkManager().removeHyperlinkClick() method.
- Finally, save presentation using Presentation.save(String, SaveFormat) method.
The following code sample shows how to remove hyperlinks from a slide in PowerPoint PPT in Java.
Get a Free License
Use Aspose.Slides for Java without evaluation limitations by getting a free temporary license.
Conclusion
In this article, you have learned how to add or remove hyperlinks in PowerPoint PPT in Java. Particularly, you have seen how to make hyperlinks of text, image, shape, audio, and video elements. Also, we have covered how to remove text or shape hyperlinks explicitly. Besides, you can explore more about Aspose.Slides for Java by visiting the documentation. Also, you can post your queries to our forum.