Follow this article to use picture as background in PowerPoint with Java REST API. You will learn to change background design in PowerPoint with Java REST Interface using Java-based Cloud SDK. Various options are discussed to customize the background image in the slide.
Prerequisite
- Create an account API credentials
- Download Aspose.Slides Cloud SDK for Java for setting slides background
- Setup Java project with the above SDK to set an image as background
Steps to Set PowerPoint Backgrounds with Java Low Code API
- Create the SlidesApi object for setting the background of a slide
- Upload the source PowerPoint presentation into the Cloud storage with a unique name
- Read the image file data into a byte array and convert it to a base 64 string
- Create the SlideBackground object and set the fill format for setting background image parameters
- Call the SetBackground() method to set the PowerPoint slide background
- Download the updated PowerPoint presentation after setting the background
These steps explain how to set the background for PowerPoint presentation with Java based API. Create the SlidesApi object, upload the presentation to the Cloud storage, read the image data, convert it to a base 64 string, and use it in the SlideBackground object for setting the FillFormat. Finally, call the SetBackground() method to add the image as background and download the output file on the disk.
Code to Add PPT background with 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); | |
} | |
} |
This code has demonstrated how to set the background for presentation slide with Java Low Code API. You may set various properties of the image including picture fill mode, glow, inner shadow, outer shadow, soft edge, and reflection. Provide the password for the uploaded PowerPoint presentation if it is protected.
This article has taught us to set background images for PPT with Java REST Interface. If you want to copy slides within a presentation or to another presentation, refer to the article Copy PowerPoint Slide with Java REST API.