Follow this guide on how to add hyperlink to PowerPoint with Java REST API. You will learn to automatically insert hyperlink in PowerPoint with Java RESTful Service using a Java-based Cloud SDK supporting all the advanced features for working with presentations. It will assist you in selecting a particular slide and shape for adding a custom hyperlink.
Prerequisite
- Create an account API credentials
- Download Aspose.Slides Cloud SDK for Java for inserting hyperlinks
- Setup Java project with the above SDK to add a link to a shape
Steps to Add a Link to PowerPoint with Java-based API
- Instantiate the SlidesApi with the API key and secret for authentication
- Read the input file into a memory stream and upload it
- Set indexes of the slides and shapes for adding hyperlink (1-based index)
- Create a shape object with a hyperlink
- Update the shape on the specified slide with the hyperlink
- Print the updated hyperlink’s URL to the console for verification
- Download the updated presentation from the cloud as a stream and save it on the disk
These steps explain how to add hyperlink in PowerPoint with Java REST API. You may create a Shape and add a hyperlink by defining the action type and external URL. Finally, update the target shape in the desired slide using the newly created shape and save the output on the disk if required.
Code to Insert Link in PowerPoint with Java Low Code API
import com.aspose.slides.ApiException; | |
import com.aspose.slides.api.SlidesApi; | |
import com.aspose.slides.model.*; | |
import java.io.File; | |
import java.io.IOException; | |
import java.nio.file.Files; | |
import java.nio.file.Path; | |
import java.nio.file.StandardCopyOption; | |
public class Example_AddHyperlinkInPresentation { | |
protected static SlidesApi presentationApi; | |
public Example_AddHyperlinkInPresentation() { | |
if (presentationApi == null) { | |
presentationApi = new SlidesApi("appSid", "appKey"); | |
} | |
} | |
public void addHyperlinkInSlide() throws ApiException, IOException { | |
String localPath = "/home/downloads/"; | |
String fileName = "PresentationWithHyperlink.pptx"; | |
String storageFolderName = "TempTests"; | |
// Index of the slide and shape where the target shape is located (1-based index). | |
int slideIndex = 2, shapeIndex = 2; | |
Shape shape = new Shape(); // Create a shape object with a hyperlink | |
Hyperlink hyperlink = new Hyperlink(); | |
hyperlink.setActionType(Hyperlink.ActionTypeEnum.HYPERLINK);// Set the action type as a hyperlink. | |
hyperlink.setExternalUrl("https://docs.aspose.cloud/slides"); // The URL for the hyperlink. | |
shape.setHyperlinkClick(hyperlink); | |
// Update the shape on the specified slide with the hyperlink | |
ShapeBase updatedShpWithHyperlink = presentationApi.updateShape(fileName, slideIndex, shapeIndex, shape, | |
null, storageFolderName, null, null); | |
// Print the updated hyperlink's URL to the console for verification. | |
System.out.println(updatedShpWithHyperlink.getHyperlinkClick().getExternalUrl()); | |
File presentationFile = presentationApi.downloadFile(storageFolderName+"/"+fileName, null, null); | |
// Copy the downloaded presentation with new image shape to the local directory | |
copyFile(presentationFile, new File(localPath, fileName)); | |
System.out.println("Presentation slide with image shape is copied to: " + localPath + fileName); | |
} | |
public static byte[] readFileToByteArray(String filePath) throws IOException { | |
Path path = new File(filePath).toPath(); | |
return Files.readAllBytes(path); | |
} | |
private void copyFile(File sourceFile, File targetFile) throws IOException { | |
if (sourceFile == null || !sourceFile.exists()) { | |
throw new IOException("Source file does not exist: " + sourceFile); | |
} | |
// Ensure the target directory exists | |
Path targetPath = targetFile.toPath(); | |
Files.createDirectories(targetPath.getParent()); | |
// Copy the file | |
Files.copy(sourceFile.toPath(), targetPath, StandardCopyOption.REPLACE_EXISTING); | |
} | |
} |
This code demonstrates how to insert hyperlink in PowerPoint with Java REST Interface. You can set various properties of a hyperlink such as a flag to enable/disable a hyperlink, tooltip, history, highlight click, and stop sound on click. Note that all these properties are available by hovering over the hyperlink rather than clicking it.
This article has taught us to create a hyperlink in PowerPoint with Java Low Code API. For adding a SmartArt in a slide, refer to the article on Add SmartArt to PowerPoint with Java REST API.