[go: up one dir, main page]

DEV Community

Alexis
Alexis

Posted on • Edited on

Java - Add or Remove Watermarks in PowerPoint

A watermark is a text or faded image that appears on the background of a document. You can use watermarks in PowerPoint documents for different purposes, such as protecting the copyright of the documents, specifying the status of the documents (like draft, approved, final, etc.), or advertising your brand. In this article, I will demonstrate how to add or remove watermarks in PowerPoint in Java using Spire.Presentation for Java.

Add Dependencies

Method 1: If you are using maven, you can easily import the JAR file of Spire.Presentation for Java API into 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>7.12.0</version>
    </dependency>
</dependencies>
Enter fullscreen mode Exit fullscreen mode

Method 2: If you are not using maven, you can download Spire.Presentation for Java from the official website, extract the zip file, then import the Spire.Presentation.jar file under the lib folder into your project as a dependency.

Add a Text Watermark to a PowerPoint Document in Java

The following are the main steps to add a text watermark to a PowerPoint document using Java:

  • Initialize an instance of the Presentation class.
  • Load a PowerPoint document using Presentation.loadFromFile() method.
  • Loop through all slides in the document.
  • Add a shape to each slide using ISlide.getShapes().appendShape() method.
  • Set shape name, fill type, rotation angle and border.
  • Locking the shape to protect it from selecting using IAutoShape.getLocking().setSelectionProtection() method.
  • Add watermark text to the shape using IAutoShape.getTextFrame().setText() method.
  • Set font name, font size and font color for the watermark text.
  • Save the result document using Presentation.saveToFile() method.
import com.spire.presentation.*;
import com.spire.presentation.drawing.FillFormatType;

import java.awt.*;
import java.awt.geom.Rectangle2D;

public class TextWatermark {
    public static void main(String []args) throws Exception {
        //Load a PowerPoint document
        Presentation ppt = new Presentation();
        ppt.loadFromFile("Sample.pptx");

        //Loop through all slides in the document
        for(int i = 0; i < ppt.getSlides().getCount(); i++)
        {
            ISlide slide = ppt.getSlides().get(i);

            //Add a shape to the slide
            IAutoShape shape = slide.getShapes().appendShape(ShapeType.RECTANGLE, new Rectangle2D.Double(340, 150, 300, 200));
            //Set shape name
            shape.setName("textWatermark");
            //Set shape fill type
            shape.getFill().setFillType(FillFormatType.NONE);
            //Set shape rotation angle
            shape.setRotation(-45);
            //Remove shape border
            shape.getLine().setFillType(FillFormatType.NONE);
            //Lock the shape to protect it from selecting and editing
            shape.getLocking().setSelectionProtection(true);

            //Add watermark text to the shape and set font color, font size and font name for the text
            shape.getTextFrame().setText("Confidential");
            PortionEx textRange = shape.getTextFrame().getTextRange();
            textRange.getFill().setFillType(FillFormatType.SOLID);
            textRange.getFill().getSolidColor().setColor(Color.GRAY);
            textRange.setFontHeight(40);
            textRange.setLatinFont(new TextFont("Arial"));
        }

        //Save the result document
        ppt.saveToFile("TextWatermark.pptx", FileFormat.PPTX_2013);
    }
}
Enter fullscreen mode Exit fullscreen mode

Add text watermark to PowerPoint using Java

Add an Image Watermark to a PowerPoint Document in Java

The following are the main steps to add an image watermark to a PowerPoint document:

  • Initialize an instance of the Presentation class.
  • Load a PowerPoint document using Presentation.loadFromFile() method.
  • Loop through all slides in the document.
  • Add a watermark image to each slide using ISlide.getShapes().appendEmbedImage() method.
  • Set image name, transparency and border.
  • Lock the image to protect it from selection using IEmbedImage.getShapeLocking().setSelectionProtection() method.
  • Save the result document using Presentation.saveToFile() method.
import com.spire.presentation.*;
import com.spire.presentation.drawing.FillFormatType;

import java.awt.geom.Rectangle2D;

public class ImageWatermark {
    public static void main(String []args) throws Exception {
        //Load a PowerPoint document
        Presentation ppt = new Presentation();
        ppt.loadFromFile("Sample.pptx");

        //Loop through all slides in the document
        for(int i = 0; i < ppt.getSlides().getCount(); i++) {
            ISlide slide = ppt.getSlides().get(i);
            //Add a watermark image to the slide
            IEmbedImage image = slide.getShapes().appendEmbedImage(ShapeType.RECTANGLE, "img.jpg", new Rectangle2D.Double(340, 150, 200, 200));
            //Set image name
            image.setName("imageWatermark");
            //Set image transparency
            image.getPictureFill().getPicture().setTransparency(70);
            //Remove image border
            image.getLine().setFillType(FillFormatType.NONE);
            //Lock the image to protect it from selection
            image.getShapeLocking().setSelectionProtection(true);
        }

        //Save the result document
        ppt.saveToFile("ImageWatermark.pptx", FileFormat.PPTX_2013);
    }
}
Enter fullscreen mode Exit fullscreen mode

Add image watermark to PowerPoint using Java

Remove Watermarks from a PowerPoint Document in Java

You can remove a watermark from a PowerPoint document using its shape name. The following are the main steps to remove a watermark from a PowerPoint document:

  • Initialize an instance of the Presentation class.
  • Load a PowerPoint document using Presentation.LoadFromFile() method.
  • Loop through all slides in the document.
  • Loop through all shapes on each slide.
  • Find the shape with a specific name using IShape.getName() method.
  • Remove the shape from the slide using ISlide.getShapes().removeAt() method.
  • Save the result document using Presentation.saveToFile() method.
import com.spire.presentation.*;

public class RemoveWatermark {
    public static void main(String []args) throws Exception {
        //Load a PowerPoint document
        Presentation ppt = new Presentation();
        ppt.loadFromFile("ImageWatermark.pptx");

        //Loop through all slides in the document
        for(int i = 0; i < ppt.getSlides().getCount(); i++)
        {
            ISlide slide = ppt.getSlides().get(i);
            //Loop through the shapes on the slide
            for (int j = slide.getShapes().getCount()-1; j >= 0; j--)
            {
                IShape shape = slide.getShapes().get(j);
                //Remove the shape named "imageWatermark"
                if (shape.getName().equals("imageWatermark"))
                {
                    slide.getShapes().removeAt(j);
                }
            }
        }

        //Save the result document
        ppt.saveToFile("RemoveWatermark.pptx", FileFormat.PPTX_2013);
    }
}
Enter fullscreen mode Exit fullscreen mode

Remove Watermark from PowerPoint using Java

Top comments (0)