Agregar imagen a PowerPoint con la API REST de Java

Sigue esta guía para aprender a agregar una imagen a PowerPoint con la API REST de Java. Aprenderás a agregar una imagen a una presentación de PowerPoint con la API de código reducido de Java utilizando un SDK de la nube basado en Java. Este artículo analiza varias propiedades para personalizar la imagen antes de agregarla a la diapositiva.

Requisito previo

Pasos para agregar una imagen en PowerPoint con una API basada en Java

  1. Cree una instancia del objeto SlidesApi para insertar una imagen en una diapositiva
  2. Sube el archivo de presentación de destino donde se agregará la imagen
  3. Prepare los datos de la imagen en el formato requerido
  4. Crea el objeto PictureFrame para colocarlo en una diapositiva
  5. Llame al método CreateShape para insertar la imagen en una diapositiva específica
  6. Descargue el archivo después de agregarle una imagen

Estos pasos resumen cómo agregar una imagen a PowerPoint con la interfaz REST de Java. Cargue la presentación de origen en el almacenamiento en la nube, prepare la imagen utilizando el método ToBase64String() en el espacio de nombres Convert y utilice esta imagen para crear un objeto PictureFrame. Por último, llame al método CreateShape() para agregar la imagen a la diapositiva especificada y descargar la presentación actualizada.

Código para agregar imágenes a diapositivas de PowerPoint con API REST de Java

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;
import java.util.Base64;
public class Example_AddPictureInPresentation {
protected static SlidesApi presentationApi;
public Example_AddPictureInPresentation() {
if (presentationApi == null) {
presentationApi = new SlidesApi("appSid", "appKey");
}
}
public void addPictureInSlide() throws ApiException, IOException {
String localPath = "/home/downloads/";
String fileName = "Sample.pptx";
String imageFileName = "ShapeImage.png";
String storageFolderName = "TempTests";
presentationApi.uploadFile(storageFolderName+"/"+fileName, readFileToByteArray(localPath + fileName),null);
PictureFrame imageFrame = new PictureFrame();
imageFrame.setX(50.0);
imageFrame.setY(50.0);
imageFrame.setWidth(350.0);
imageFrame.setHeight(250.0);
PictureFill pictureFill1 = new PictureFill();
pictureFill1.setPictureFillMode(PictureFill.PictureFillModeEnum.STRETCH);
pictureFill1.setBase64Data(Base64.getEncoder().encodeToString(readFileToByteArray(localPath + imageFileName)));
imageFrame.setPictureFillFormat(pictureFill);
// Add the image to the third slide of the presentation.
ShapeBase shapeResponse = presentationApi.createShape(fileName, 3, imageFrame, null, null,
null,storageFolderName, null, null);
// Output the URI of the newly added image shape.
System.out.println("Image added at: "+ shapeResponse.getSelfUri().getHref());
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 de ejemplo demuestra cómo colocar una imagen en PowerPoint con el servicio RESTful de Java. Puede definir la posición inicial de la imagen configurando las propiedades X e Y en el objeto PictureFrame y definir el modo de relleno de la imagen utilizando el objeto de clase PictureFill. La propiedad PictureFrame define el tamaño de la imagen en la diapositiva independientemente del tamaño original.

Este artículo nos ha enseñado a añadir imágenes. Para añadir notas a una presentación, consulta el artículo sobre Agregar notas a diapositivas de PowerPoint con la API REST de Java.

 Español