Java: Add Text Watermark to PowerPoint

Text watermark refers to a transparent or translucent logo or information added in the form of text on documents, images or other media. This feature is also applicable to PowerPoint presentations. Overlaying text watermarks on slides helps users identify copyright ownership, protect document security, or convey other information. In this article, we will show you how to add text watermark to PowerPoint slide using Spire.Presentation for Java.

Install Spire.Presentation for Java

First of all, you're required to add the Spire.Presentation.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml file.

<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <name>e-iceblue</name>
        <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.presentation</artifactId>
        <version>9.4.5</version>
    </dependency>
</dependencies>
    

Add a Single Text Watermark to Slide

Spire.Presentation for Java supports adding a single text watermark by inserting a shape to slide using  Presentation.getSlides().get().getShapes().appendShape() method. And Then set text of it by calling  IAutoShape.getTextFrame().setText() method. The following are detailed steps.

  • Create a  Presentation object and load a sample file using Presentation.loadFromFile() method.
  • Set the width and height of watermark string.
  • Create a Rectangle2D.Double object and specify the position and size of the rectangle shape by calling Presentation.getSlideSize().getSize().getWidth() and Presentation.getSlideSize().getSize().getHeight() methods.
  • Add the rectangle shape to the first slide using Presentation.getSlides().get().getShapes().appendShape() method.
  • Set the style of the shape.
  • Set text of the shape by using IAutoShape.getTextFrame().setText() and get the specific range using IAutoShape.getTextFrame().getTextRange() method.
  • Set the style of the text range.
  • Save the result file using Presentation.saveToFile() method.
  • Java
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);
        textRange.getFill().getSolidColor().setColor(color);


        //Save the result document
        presentation.saveToFile("output/SingleWatermark.pptx", FileFormat.PPTX_2010);
        presentation.dispose();
    }

}

Java: Add Text Watermark to PowerPoint

Add a Tiled Text Watermark to Slide

By calling Presentation.getSlides().get().getShapes().appendShape() method multiple times in a loop, multiple rectangle shapes can be added at different positions to achieve the effect of tiled watermarks. The following are detailed steps.

  • Create a  Presentation object and load a sample file using Presentation.loadFromFile() method.
  • Set the text of watermarks and measure the size of the watermark text.
  • Initialize x and y coordinate of the watermark. And then add multiple rectangle shapes to slides by calling Presentation.getSlides().get().getShapes().appendShape() method for multiple times in a loop.
  • Set the style of the shapes.
  • Add text to the shapes by using IAutoShape.getTextFrame().setText() method and get the specific ranges using IAutoShape.getTextFrame().getTextRange() method.
  • Set the style of the text ranges.
  • Save the result file using Presentation.saveToFile() method.
  • Java
import com.spire.pdf.graphics.PdfTrueTypeFont;
import com.spire.presentation.*;
import com.spire.presentation.drawing.FillFormatType;
import java.awt.*;
import java.awt.geom.Dimension2D;
import java.awt.geom.Rectangle2D;

public class InsertTiledWatermark {
    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 text of watermarks
        String watermarkText = "Confidential";

        //Measure the size of the watermark text
        Font font = new java.awt.Font("Arial", java.awt.Font.BOLD, 20);
        PdfTrueTypeFont trueTypeFont = new PdfTrueTypeFont(font);
        Dimension2D strSize = trueTypeFont.measureString(watermarkText);

        //Initialize x and y coordinate
        float x = 30;
        float y = 80;

        for (int rowNum = 0; rowNum < 4; rowNum++) {
            for (int colNum = 0; colNum < 4; colNum++) {

                //Add rectangle shapes to the first slide
                Rectangle2D rect = new Rectangle2D.Float(x, y, (float) strSize.getWidth() + 10, (float) strSize.getHeight());
                IAutoShape shape = presentation.getSlides().get(0).getShapes().appendShape(ShapeType.RECTANGLE, rect);

                //Set the style of the shapes
                shape.getFill().setFillType(FillFormatType.NONE);
                shape.getShapeStyle().getLineColor().setColor(new Color(1, 1, 1, 0));
                shape.setRotation(-45);
                shape.getLocking().setSelectionProtection(true);
                shape.getLine().setFillType(FillFormatType.NONE);

                //Add watermark text to the shapes
                shape.getTextFrame().setText(watermarkText);
                PortionEx textRange = shape.getTextFrame().getTextRange();

                //Set the style of the text ranges
                textRange.getFill().setFillType(FillFormatType.SOLID);
                textRange.setLatinFont(new TextFont(trueTypeFont.getName()));
                textRange.setFontMinSize(trueTypeFont.getSize());
                Color color = new Color(237,129,150,200);
                textRange.getFill().getSolidColor().setColor(color);

                x += (100 + strSize.getWidth());

            }
            x = 30;
            y += (100 + strSize.getHeight());
        }

        //Save the result document
        presentation.saveToFile("output/TiledWatermark.pptx", FileFormat.PPTX_2013);
        presentation.dispose();
    }
}

Java: Add Text Watermark to PowerPoint

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.