Aggiungere un'immagine a PowerPoint con Java REST API

Segui questa guida per imparare come aggiungere un’immagine a PowerPoint con Java REST API. Imparerai come aggiungere un’immagine a un PowerPoint con Java Low Code API utilizzando un Cloud SDK basato su Java. Questo articolo illustra varie proprietà per personalizzare l’immagine prima di aggiungerla alla diapositiva.

Prerequisito

Passaggi per aggiungere un’immagine in PowerPoint con API basata su Java

  1. Crea un’istanza dell’oggetto SlidesApi per inserire un’immagine in una diapositiva
  2. Carica il file di presentazione di destinazione in cui verrà aggiunta l’immagine
  3. Preparare i dati dell’immagine nel formato richiesto
  4. Crea l’oggetto PictureFrame da inserire in una diapositiva
  5. Chiama il metodo CreateShape per inserire l’immagine in una diapositiva specifica
  6. Scarica il file dopo aver aggiunto un’immagine

Questi passaggi riassumono come aggiungere un’immagine a PowerPoint con Java REST Interface. Carica la presentazione sorgente sullo storage Cloud, prepara l’immagine usando il metodo ToBase64String() nello spazio dei nomi Convert e usa questa immagine per creare un oggetto PictureFrame. Infine, chiama il metodo CreateShape() per aggiungere l’immagine alla diapositiva specificata e scarica la presentazione aggiornata.

Codice per aggiungere immagini alle diapositive di PowerPoint con Java REST 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;
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);
}
}

Questo codice di esempio dimostra come mettere un’immagine su PowerPoint con Java RESTful Service. È possibile definire la posizione iniziale dell’immagine impostando le proprietà X e Y nell’oggetto PictureFrame e definire la modalità di riempimento dell’immagine utilizzando l’oggetto classe PictureFill. La proprietà PictureFrame definisce la dimensione dell’immagine sulla diapositiva indipendentemente dalla dimensione originale.

Questo articolo ci ha insegnato ad aggiungere immagini. Per aggiungere note a una presentazione, fare riferimento all’articolo su Aggiungere note alla diapositiva di PowerPoint con Java REST API.

 Italiano