How to Add Text Watermarks to PowerPoint Presentations using Java
Source: Dev.to
Spire.Presentation for Java: Introduction and Installation
Spire.Presentation for Java is a powerful, feature‑rich API that allows Java applications to create, read, write, and convert PowerPoint presentations. It provides extensive capabilities for managing presentation elements, including slides, shapes, text, images, and the ability to add watermarks. This library streamlines complex PowerPoint automation tasks, making it an invaluable tool for developers.
To integrate Spire.Presentation into your project, the simplest method is to add its Maven dependency. This ensures all necessary libraries are automatically managed and included in your build.
Maven Dependency
<!-- Repository definition -->
<repository>
<id>e-iceblue</id>
<url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
</repository>
<!-- Dependency -->
<dependency>
<groupId>com.e-iceblue</groupId>
<artifactId>spire.presentation</artifactId>
<version>10.11.4</version>
</dependency>
For those not using Maven, you can also download the JAR file directly from the E‑iceblue website and manually add it to your project’s build path. However, Maven is generally recommended for easier dependency management.
Adding a Single‑Line Text Watermark to PowerPoint using Java
A single‑line text watermark is ideal for subtle yet clear branding or content‑status indicators such as “CONFIDENTIAL,” “DRAFT,” or “INTERNAL USE ONLY.” This method typically places the watermark diagonally across the slide, ensuring visibility without obscuring the main content.
Steps
- Create and load the PowerPoint file – instantiate a
Presentationobject and callPresentation.loadFromFile(). - Define the watermark size – set the width and height for the text box.
- Create a rectangle shape – use
Rectangle2D.Doubleto position the shape relative to the slide dimensions. - Add the shape to the slide – append the rectangle to the first slide with
Presentation.getSlides().get(0).getShapes().appendShape(). - Customize the shape style – adjust border, fill transparency, rotation angle, and alignment.
- Add and format the watermark text – set the text via
IAutoShape.getTextFrame().setText(), then style theTextRange(font, size, color, transparency). - Save the presentation – export the updated file with
Presentation.saveToFile().
Code Example
import com.spire.presentation.*;
import com.spire.presentation.drawing.FillFormatType;
import java.awt.*;
import java.awt.geom.Rectangle2D;
public class InsertSingleWatermark {
public static void main(String[] args) throws Exception {
// Create a Presentation object and load a sample file
Presentation presentation = new Presentation();
presentation.loadFromFile("sample.pptx");
// Set the width and height of watermark string
int width = 400;
int height = 300;
// Define the position and size of shape
Rectangle2D.Double rect = new Rectangle2D.Double(
(presentation.getSlideSize().getSize().getWidth() - width) / 2,
(presentation.getSlideSize().getSize().getHeight() - height) / 2,
width, height);
// Add the shape to the first slide
IAutoShape shape = presentation.getSlides().get(0).getShapes()
.appendShape(ShapeType.RECTANGLE, rect);
// Set the style of shape
shape.getFill().setFillType(FillFormatType.NONE);
shape.getShapeStyle().getLineColor().setColor(Color.white);
shape.setRotation(-45);
shape.getLocking().setSelectionProtection(true);
shape.getLine().setFillType(FillFormatType.NONE);
// Add text to shape
shape.getTextFrame().setText("Confidential");
PortionEx textRange = shape.getTextFrame().getTextRange();
// Set the style of the text range
textRange.getFill().setFillType(FillFormatType.SOLID);
textRange.setFontHeight(50);
Color color = new Color(237, 129, 150, 200); // RGBA with transparency
textRange.getFill().getSolidColor().setColor(color);
// Save the result document
presentation.saveToFile("output/SingleWatermark.pptx", FileFormat.PPTX_2010);
presentation.dispose();
}
}
The snippet demonstrates loading a presentation, creating a text box, setting its text and formatting (including color, font size, and transparency), rotating it, and finally saving the presentation. The transparency and rotation are crucial for achieving a proper watermark effect.
Adding a Tiled Text Watermark (Multi‑line) to PowerPoint using Java
For more robust content protection or pervasive branding, a tiled watermark can cover the entire slide with repeating text. This makes it harder to crop out or ignore the watermark and is especially useful for highly sensitive documents.
Steps
- Load the PowerPoint presentation – create a
Presentationobject and callloadFromFile(). - Prepare watermark text and measure its size – define the string to repeat and calculate its dimensions.
- Add repeated watermark shapes – initialize starting coordinates and loop through the slide layout, inserting rectangle shapes with
appendShape()to distribute multiple watermarks. - Format the rectangle shapes – apply fill transparency, remove borders, set rotation, etc., to keep the watermark subtle.
- Apply text to each shape – set the text via
IAutoShape.getTextFrame().setText()and retrieve theTextRangefor further styling. - Format the watermark text – customize font size, color, opacity, spacing, and rotation.
- Save the file – export the final version with
Presentation.saveToFile().
Code Example
import com.spire.pdf.graphics.PdfTrueTypeFont;
import com.spire.presentat
// (The remainder of the example code should follow the same pattern as the single‑line watermark,
// creating multiple shapes in a loop, setting their text, styling, and then saving the file.)
Note: The provided snippet is incomplete; developers should extend it by looping over the slide area, creating shapes, applying the desired text and styling, and then saving the presentation as shown in the single‑line example.