Add Watermark to PPT with Java REST API

This article guides how to add watermark to PPT with Java REST API. You will learn the insertion of PowerPoint watermark with Java Low Code API using a Java-based Cloud SDK. Various options will be discussed to customize the watermark in the presentation.

Prerequisite

Steps to Insert Watermark in PPT with Java REST API

  1. Initialize the SlidesApi object with a client ID and secret to add a watermark
  2. Create a Shape object by setting the Text and TextFrameFormat properties
  3. Read the input presentation file into a stream
  4. Call the CreateWatermarkOnline() method by providing the input file stream and shape
  5. Save the returned stream into a local file on the disk

These steps describe how to insert a watermark in PowerPoint with Java RESTful Service. Create a shape for defining the watermark by setting the text and rotation angle. Call the CreateWatermarkOnline() method to add a watermark by providing the input presentation and the Shape object containing the watermark parameters.

Code to Create a Watermark in PowerPoint with Java REST Interface

package KbExamples;
import com.aspose.slides.ApiException;
import com.aspose.slides.api.SlidesApi;
import com.aspose.slides.model.Shape;
import com.aspose.slides.model.TextFrameFormat;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
public class Example_AddWatermarkTextInPresentation {
protected static SlidesApi presentationApi;
public Example_AddWatermarkTextInPresentation() {
if (presentationApi == null) {
presentationApi = new SlidesApi("appSid", "appKey");
}
}
public void watermarkText() throws ApiException, IOException {
String localPath = "/home/downloads/";
String fileName = "Sample.pptx";
String outputFileName = "WatermarkedPresentation.pptx";
String storageFolderName = "TempTests";
Shape shape = new Shape();
shape.setText("Powered by Aspose.");
TextFrameFormat textFrameFormat = new TextFrameFormat();
textFrameFormat.setRotationAngle((double)45);
shape.setTextFrameFormat(textFrameFormat);
File presentationFile = presentationApi.createWatermarkOnline(readFileToByteArray(localPath + fileName),shape,(double)15,"Watermark", "Arial", "Red",null );
// Copy the watermarked presentation with new image shape to the local directory
copyFile(presentationFile, new File(localPath, outputFileName));
System.out.println("Presentation with Watermarked text is copied to: " + localPath + outputFileName);
}
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 demonstrates how to put a watermark in PowerPoint with Java Java-based API. The Shape object contains a lot of characteristics that you can set to customize the watermark. Options are also available to add an image watermark in the presentation.

This article has guided us to add a watermark. If you want to generate a PowerPoint from an HTML file, refer to the article Convert HTML to PowerPoint with Java REST API.

 English