Use a imagem como plano de fundo no PowerPoint com a API REST Java

Siga este artigo para usar imagem como plano de fundo em PowerPoint com Java REST API. Você aprenderá a alterar design de plano de fundo no PowerPoint com Java REST Interface usando Java-based Cloud SDK. Várias opções são discutidas para personalizar a imagem de plano de fundo no slide.

Pré-requisito

Etapas para definir fundos do PowerPoint com Java Low Code API

  1. Crie o objeto SlidesApi para definir o plano de fundo de um slide
  2. Carregue a apresentação do PowerPoint de origem no armazenamento em nuvem com um nome exclusivo
  3. Leia os dados do arquivo de imagem em uma matriz de bytes e converta-os em uma string base 64
  4. Crie o objeto SlideBackground e defina o formato de preenchimento para definir os parâmetros da imagem de fundo
  5. Chame o método SetBackground() para definir o plano de fundo do slide do PowerPoint
  6. Baixe a apresentação atualizada do PowerPoint após definir o plano de fundo

Estas etapas explicam como definir o plano de fundo para apresentação do PowerPoint com API baseada em Java. Crie o objeto SlidesApi, carregue a apresentação no armazenamento em nuvem, leia os dados da imagem, converta-os em uma string base 64 e use-os no objeto SlideBackground para definir o FillFormat. Por fim, chame o método SetBackground() para adicionar a imagem como plano de fundo e baixar o arquivo de saída no disco.

Código para adicionar fundo PPT com Java Low Code API

import com.aspose.slides.ApiException;
import com.aspose.slides.api.SlidesApi;
import com.aspose.slides.model.PictureFill;
import com.aspose.slides.model.SlideBackground;
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_AddPresentationBackgroundImage {
protected static SlidesApi presentationApi;
public Example_AddPresentationBackgroundImage() {
if (presentationApi == null) {
presentationApi = new SlidesApi("appSid", "appKey");
}
}
public void addBackgroundImage() throws ApiException, IOException {
String localPath = "/home/downloads/";
String fileName = "Sample.pptx";
String imageFileName = "Background.png";
String storageFolderName = "TempTests";
presentationApi.uploadFile(storageFolderName+"/"+fileName, readFileToByteArray(localPath + fileName),null);
SlideBackground pictureBackground = new SlideBackground();
PictureFill pictureFill = new PictureFill();//For customization of the background image
pictureFill.setBase64Data(Base64.getEncoder().encodeToString(readFileToByteArray(localPath + imageFileName)));
pictureFill.setPictureFillMode(PictureFill.PictureFillModeEnum.STRETCH);
pictureBackground.setFillFormat(pictureFill);
// Set slide background image
SlideBackground currentBackground = presentationApi.setBackground(fileName, 1, pictureBackground, null, storageFolderName, null);
File presentationFile = presentationApi.downloadFile(storageFolderName+"/"+fileName, null, null);
// Copy the downloaded presentation with new background image to the local directory
copyFile(presentationFile, new File(localPath, fileName));
System.out.println("Presentation slide background image set and 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 demonstrou como definir o fundo para slide de apresentação com Java Low Code API. Você pode definir várias propriedades da imagem, incluindo modo de preenchimento de imagem, brilho, sombra interna, sombra externa, borda suave e reflexão. Forneça a senha para a apresentação do PowerPoint carregada, se ela estiver protegida.

Este artigo nos ensinou a definir imagens de fundo para PPT com Java REST Interface. Se você quiser copiar slides dentro de uma apresentação ou para outra apresentação, consulte o artigo Copiar slide do PowerPoint com Java REST API.

 Português