Använd bild som bakgrund i PowerPoint med Java REST API

Följ den här artikeln för att använda bild som bakgrund i PowerPoint med Java REST API. Du kommer att lära dig att ändra bakgrundsdesign i PowerPoint med Java REST Interface med Java-baserad Cloud SDK. Olika alternativ diskuteras för att anpassa bakgrundsbilden i bilden.

Nödvändig förutsättning

Steg för att ställa in PowerPoint-bakgrunder med Java Low Code API

  1. Skapa objektet SlidesApi för att ställa in bakgrunden för en bild
  2. Ladda upp käll PowerPoint-presentationen till molnlagringen med ett unikt namn
  3. Läs bildfilens data till en byte-array och konvertera den till en bas 64-sträng
  4. Skapa SlideBackground-objektet och ställ in fyllningsformatet för att ställa in parametrar för bakgrundsbild
  5. Anropa metoden SetBackground() för att ställa in PowerPoint-bildbakgrunden
  6. Ladda ner den uppdaterade PowerPoint-presentationen efter att ha ställt in bakgrunden

Dessa steg förklarar hur du ställer in bakgrunden för PowerPoint-presentation med Java-baserat API. Skapa SlidesApi-objektet, ladda upp presentationen till molnlagringen, läs bilddata, konvertera den till en bas 64-sträng och använd den i SlideBackground-objektet för att ställa in FillFormat. Till sist, anropa metoden SetBackground() för att lägga till bilden som bakgrund och ladda ner utdatafilen på disken.

Kod för att lägga till PPT-bakgrund med 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);
}
}

Den här koden har visat hur man ställer in bakgrunden för presentationsbilden med Java Low Code API. Du kan ställa in olika egenskaper för bilden, inklusive bildfyllningsläge, glöd, inre skugga, yttre skugga, mjuk kant och reflektion. Ange lösenordet för den uppladdade PowerPoint-presentationen om den är skyddad.

Den här artikeln har lärt oss att ställa in bakgrundsbilder för PPT med Java REST Interface. Om du vill kopiera bilder i en presentation eller till en annan presentation, se artikeln Kopiera PowerPoint Slide med Java REST API.

 Svenska