Add Watermark to PowerPoint Java

Watermarks are commonly used to specify the ownership or to prevent unauthorized usage of the documents. On the other hand, they are also used to display the status of a document such as a manuscript, draft, etc. In this article, you will learn how to add text or image watermark to PowerPoint slides programmatically using Java.

Java API to Add Watermark to PowerPoint Slides

For adding watermarks to the PowerPoint slides, we’ll use Aspose.Slides for Java. It is a presentation manipulation API that lets you create and manipulate presentation documents from within the Java applications. You can either download the API or install it using the following Maven configurations.

<repository>
    <id>AsposeJavaAPI</id>
    <name>Aspose Java API</name>
    <url>http://repository.aspose.com/repo/</url>
</repository>
<dependency>
    <groupId>com.aspose</groupId>
    <artifactId>aspose-slides</artifactId>
    <version>21.7</version>
    <classifier>jdk16</classifier>
</dependency>

Add Text Watermark to PowerPoint Slides in Java

The following are the steps to add a text watermark to the PowerPoint slides using Java.

  • First, load the PowerPoint presentation using the Presentation class.
  • Get reference of the slide master in an IMasterSlide object.
  • Calculate the position of the watermark according to the dimensions of presentation.
  • Add a new auto shape to the Shapes collection of the slide and get its reference in IAutoShape object.
  • Add text frame to the shape and set its text using IAutoShape.addTextFrame(string) method.
  • Set font size, color and rotation angle of the watermark.
  • Lock watermark to avoid removal or modification.
  • Finally, save the updated PowerPoint file using Presentation.save(string, SaveFormat) method.

The following code sample shows how to add a text watermark to the PowerPoint slides.

// Open presentation
Presentation pres = new Presentation("presentation.pptx");
try {
// Access master
IMasterSlide master = pres.getMasters().get_Item(0);
Point2D.Float center = new Point2D.Float((float) pres.getSlideSize().getSize().getWidth() / 2,
(float) pres.getSlideSize().getSize().getHeight() / 2);
float width = 300;
float height = 300;
float x = (float) center.getX() - width / 2;
float y = (float) center.getY() - height / 2;
// Add shape
IAutoShape watermarkShape = master.getShapes().addAutoShape(ShapeType.Rectangle, x, y, width, height);
// Set fill type
watermarkShape.getFillFormat().setFillType(FillType.NoFill);
watermarkShape.getLineFormat().getFillFormat().setFillType(FillType.NoFill);
// Set rotation angle
watermarkShape.setRotation(-45);
// Set text
ITextFrame watermarkTextFrame = watermarkShape.addTextFrame("Watermark");
// Set font and color
IPortion watermarkPortion = watermarkTextFrame.getParagraphs().get_Item(0).getPortions().get_Item(0);
watermarkPortion.getPortionFormat().setFontHeight(52);
int alpha = 150, red = 200, green = 200, blue = 200;
watermarkPortion.getPortionFormat().getFillFormat().setFillType(FillType.Solid);
watermarkPortion.getPortionFormat().getFillFormat().getSolidFillColor()
.setColor(new Color(red, green, blue, alpha));
// Lock Shapes from modifying
watermarkShape.getAutoShapeLock().setSelectLocked(true);
watermarkShape.getAutoShapeLock().setSizeLocked(true);
watermarkShape.getAutoShapeLock().setTextLocked(true);
watermarkShape.getAutoShapeLock().setPositionLocked(true);
watermarkShape.getAutoShapeLock().setGroupingLocked(true);
// Save the presentation
pres.save("watermarked-presentation.pptx", SaveFormat.Pptx);
} finally {
if (pres != null)
pres.dispose();
}

Output

The following is the screenshot of the PowerPoint slide after adding the watermark.

Add Text Watermark To PPT Java

Add Image Watermark to PowerPoint Slides in Java

The following are the steps to add an image watermark to the PowerPoint slides in Java.

The following code sample shows how to add an image watermark to PowerPoint slides.

// Open presentation
Presentation pres = new Presentation("presentation.pptx");
try {
// Access slide master
IMasterSlide master = pres.getMasters().get_Item(0);
Point2D.Float center = new Point2D.Float((float) pres.getSlideSize().getSize().getWidth() / 2,
(float) pres.getSlideSize().getSize().getHeight() / 2);
float width = 300;
float height = 300;
float x = (float) center.getX() - width / 2;
float y = (float) center.getY() - height / 2;
// Add shape
IAutoShape watermarkShape = master.getShapes().addAutoShape(ShapeType.Rectangle, x, y, width, height);
IPPImage image = pres.getImages().addImage(Files.readAllBytes(Paths.get("watermark.png")));
// Set fill type
watermarkShape.getFillFormat().setFillType(FillType.Picture);
watermarkShape.getFillFormat().getPictureFillFormat().getPicture().setImage(image);
watermarkShape.getFillFormat().getPictureFillFormat().setPictureFillMode(PictureFillMode.Stretch);
watermarkShape.getLineFormat().getFillFormat().setFillType(FillType.NoFill);
// Lock Shapes from modifying
watermarkShape.getAutoShapeLock().setSelectLocked(true);
watermarkShape.getAutoShapeLock().setSizeLocked(true);
watermarkShape.getAutoShapeLock().setTextLocked(true);
watermarkShape.getAutoShapeLock().setPositionLocked(true);
watermarkShape.getAutoShapeLock().setGroupingLocked(true);
// Save the presentation
pres.save("watermarked-presentation-image.pptx", SaveFormat.Pptx);
} finally {
if (pres != null)
pres.dispose();
}

The following is the screenshot of the presentation after adding an image watermark.

Add Image Watermark To PPT Java

Get a Free API License

You can use Aspose.Slides for Java without evaluation limitations by requesting a temporary license.

Try Online

Try the following online watermarking tool which is developed using Aspose.Slides.

Conclusion

In this article, you have learned how to add a watermark to the PowerPoint slides using Java. The step-by-step guide and code samples have demonstrated how to add text and image watermarks to PowerPoint presentations. Furthermore, you can consult the documentation to explore other features of the API. Also, you can feel free to let us know about your queries via our forum.

See Also