Siga esta guía sobre cómo agregar un hipervínculo a PowerPoint con la API REST de Java. Aprenderá a insertar hipervínculos en PowerPoint automáticamente con el servicio RESTful de Java utilizando un SDK de nube basado en Java que admite todas las funciones avanzadas para trabajar con presentaciones. Le ayudará a seleccionar una diapositiva y una forma particulares para agregar un hipervínculo personalizado.
Requisito previo
Descargar Aspose.Slides Cloud SDK for Java for inserting hyperlinks
Configure el proyecto Java con el SDK anterior para agregar un enlace a una forma
Pasos para agregar un enlace a PowerPoint con API basada en Java
- Cree una instancia de SlidesApi con la clave API y el secreto para la autenticación.
- Lea el archivo de entrada en una secuencia de memoria y cárguelo
- Establecer índices de las diapositivas y formas para agregar hipervínculos (índice basado en 1)
- Crear un objeto de forma con un hipervínculo
- Update la forma en la diapositiva especificada con el hipervínculo
- Imprima la URL del hipervínculo actualizado a la consola para su verificación.
- Descargue la presentación actualizada desde la nube como una secuencia y guárdela en el disco.
Estos pasos explican cómo agregar un hipervínculo en PowerPoint con la API REST de Java. Puede crear una forma y agregar un hipervínculo definiendo el tipo de acción y la URL externa. Finalmente, actualice la forma de destino en la diapositiva deseada usando la forma recién creada y guarde el resultado en el disco si es necesario.
Código para insertar enlace en PowerPoint con 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); | |
} | |
} |
Este código demuestra cómo insertar un hipervínculo en PowerPoint con la interfaz REST de Java. Puede configurar varias propiedades de un hipervínculo, como una bandera para habilitar/deshabilitar un hipervínculo, información sobre herramientas, historial, resaltar el clic y detener el sonido al hacer clic. Tenga en cuenta que todas estas propiedades están disponibles al pasar el cursor sobre el hipervínculo en lugar de hacer clic en él.
Este artículo nos ha enseñado a crear un hipervínculo en PowerPoint con Java Low Code API. Para agregar un SmartArt en una diapositiva, consulte el artículo sobre Agregue SmartArt a PowerPoint con la API REST de Java.