This simple topic focuses on how to add Hyperlink to an Image in PowerPoint using Java by providing the step wise details to configure the environment and use the sample code. You can easily create Hyperlink for an Image in PPTX using Java use this application in any operating system environment configured with Java including Windows, macOS, or Linux with the help of simple API calls.
Steps to Create Hyperlink for an Image in PPTX using Java
- Configure Aspose.Slides from the Maven repository to insert a hyperlink in the presentation
- Instantiate the Presentation class object to add an empty Presentation object
- Access the first slide in the presentation slides collection
- Load the source PNG image from the disk and add it to the presentation image collection
- Insert the added image to a picture frame inside the selected slide shapes collection
- Add a hyperlink to an external slide for the picture frame shape using the Hyperlink class and set its properties
- Save the presentation with a hyperlink as PPTX on the disk
The above steps guide to insert hyperlink to an image in PPTX using Java where after creating a default empty presentation using the Presentation class, we will access its default first slide from the slides collection. You can load any image format including JPEG, PNG, GIF, BMP or TIFF from the disk and add it to the presentation image collection. Finally, the added image is associated with a picture frame inside the slide and an external hyperlink is set to that along with its respective properties before saving the presentation on the disk.
Code to Insert Hyperlink to an Image in PPTX using Java
import com.aspose.slides.Hyperlink; | |
import com.aspose.slides.IPPImage; | |
import com.aspose.slides.IPictureFrame; | |
import com.aspose.slides.ISlide; | |
import com.aspose.slides.License; | |
import com.aspose.slides.Presentation; | |
import com.aspose.slides.SaveFormat; | |
import com.aspose.slides.ShapeType; | |
import java.io.File; | |
import java.io.FileInputStream; | |
import java.io.FileNotFoundException; | |
public class InsertHyperlink { | |
public static void main2(String[] args) throws Exception { // Main function to add a hyperlink to an image in PPTX | |
// Load the product license to add a hyperlink | |
License licHyperlink = new License(); | |
licHyperlink.setLicense("Aspose.Total.lic"); | |
// Generate an empty presentation using the Presentation class object to add hyperlink to an image | |
Presentation presentationWithHyperlink = new Presentation(); | |
// Access the first slide inside the slides collection of the presentation | |
ISlide slideForPng = presentationWithHyperlink.getSlides().get_Item(0); | |
// Add the PNG Image from the disk in the images collection of the presentation | |
IPPImage imageFromDisk = presentationWithHyperlink.getImages().addImage(new FileInputStream(new File("aspose_logo.png"))); | |
// Insert a picture frame in the shapes collection of the slide | |
IPictureFrame pictureFrame = slideForPng.getShapes().addPictureFrame(ShapeType.Rectangle, | |
20, 20, 90, 90, imageFromDisk); | |
// Insert the hyperlink for the added picture frame | |
pictureFrame.setHyperlinkClick(new Hyperlink("https://www.aspose.com/")); | |
// Add a tooltip for the hyperlink | |
pictureFrame.getHyperlinkClick().setTooltip("More than 75% of Fortune 100 companies show trust in Aspose APIs"); | |
// Save the presentation with hyperlinked image on the disk | |
presentationWithHyperlink.save("preswithHyperlink.pptx", SaveFormat.Pptx); | |
System.out.println("Done"); | |
} | |
} |
The above example insert hyperlink to an image in PPTX in Java using the simple API interface. You can add a hyperlink to any shape inside the presentation including autoshapes, video frames and table cells. While inserting the hyperlink using the Hyperlink class for any shape, you can set different properties for the added hyperlink including a tooltip text, highlight click, color source and history. In the given code, we have set an external hyperlink but you can also set the internal hyperlinks to navigate between different slides of the presentation as well.
In this example, we have learnt to insert hyperlink to an image inside PPTX, however you can use the same code to add hyperlink to an image in PPT using Java. If you are interested in learning to convert PDF to a PowerPoint presentation, refer to the article on how to convert PDF to Presentation using Java.