Adicionar imagem ao PowerPoint com Java REST API

Siga este guia para aprender como adicionar imagem a PowerPoint com Java REST API. Você aprenderá como adicionar uma imagem a um PowerPoint com Java Low Code API usando um Cloud SDK baseado em Java. Este artigo discute várias propriedades para personalizar a imagem antes de adicioná-la ao slide.

Pré-requisito

Etapas para adicionar imagem no PowerPoint com API baseada em Java

  1. Instanciar o objeto SlidesApi para inserir uma imagem em um slide
  2. Carregue o arquivo de apresentação de destino onde a imagem será adicionada
  3. Prepare os dados da imagem no formato necessário
  4. Crie o objeto PictureFrame para colocar em um slide
  5. Chame o método CreateShape para inserir a imagem em um slide específico
  6. Baixe o arquivo após adicionar uma imagem a ele

Estas etapas resumem como adicionar uma imagem ao PowerPoint com a interface Java REST. Carregue a apresentação de origem no armazenamento em nuvem, prepare a imagem usando o método ToBase64String() no namespace Convert e use essa imagem para criar um objeto PictureFrame. Por fim, chame o método CreateShape() para adicionar a imagem ao slide especificado e baixar a apresentação atualizada.

Código para adicionar imagens a slides do PowerPoint com 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);
}
}

Este código de exemplo demonstra como colocar uma imagem no PowerPoint com o Java RESTful Service. Você pode definir a posição inicial da imagem definindo as propriedades X e Y no objeto PictureFrame e definir o modo de preenchimento de imagem usando o objeto de classe PictureFill. A propriedade PictureFrame define o tamanho da imagem no slide, independentemente do tamanho original.

Este artigo nos ensinou a adicionar imagens. Para adicionar notas a uma apresentação, consulte o artigo em Adicionar notas ao slide do PowerPoint com Java REST API.

 Português