You can add watermarks to a PowerPoint document to prevent it from illegal usage, or to specify if the document is a “draft” version. Here you will see how to work with watermarks, especially add, lock and remove watermarks in Java using Free Spire.Presentation for Java library.
Before running the following code example, you need to add dependencies for including Free Spire.Presentation for Java library in your Java project.
Add and Lock Watermarks
In general, there are two types of watermarks: text watermark and image watermark. Free Spire.Presentation library supports adding text and image watermarks to a PowerPoint document through the following steps:
- Load the document.
- Iterate through the slides in the document.
- Add a shape (ex. rectangle) to each slide and then insert text/image to the shape.
- Lock the shape to prevent them from selecting and editing
- Save the result document.
Add Text Watermark
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 sample document
Presentation ppt = new Presentation();
ppt.loadFromFile("Input.pptx");
//Iterate through the slides in the document
for(int i = 0; i < ppt.getSlides().getCount(); i++)
{
ISlide slide = ppt.getSlides().get(i);
//Add shape to each 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 as no fill
shape.getFill().setFillType(FillFormatType.NONE);
//Set shape rotation
shape.setRotation(-45);
//Lock the shape to protect it from selecting and editing
shape.getLocking().setSelectionProtection(true);
//Set shape border type as no border
shape.getLine().setFillType(FillFormatType.NONE);
//Add text to shape and set font of 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);
}
}
Output:
![](https://officedevblogs805426550.wordpress.com/wp-content/uploads/2021/08/ppttextwatermark.png?w=903)
Add Image Watermark
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 document
Presentation ppt = new Presentation();
ppt.loadFromFile("Input.pptx");
//Iterate through the slides in the document
for(int i = 0; i< ppt.getSlides().getCount(); i++)
{
ISlide slide = ppt.getSlides().get(i);
//Add 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);
//Lock image
image.getShapeLocking().setSelectionProtection(true);
image.getLine().setFillType(FillFormatType.NONE);
}
//Save the result document
ppt.saveToFile("ImageWatermark.pptx", FileFormat.PPTX_2013);
}
}
Output:
![](https://officedevblogs805426550.wordpress.com/wp-content/uploads/2021/08/pptimagewatermark.png?w=901)
Remove Watermarks
Use the following code, you’re able to remove either the text watermark shape or the image watermark shape by detecting shape name.
import com.spire.presentation.*;
public class RemoveWatermark {
public static void main(String []args) throws Exception {
//Load a document
Presentation ppt = new Presentation();
ppt.loadFromFile("imageWatermark.pptx");
//Iterate through the slides in the document
for(int i = 0; i < ppt.getSlides().getCount(); i++)
{
ISlide slide = ppt.getSlides().get(i);
//Iterate through the shapes on each 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);
}
}
Output:
![](https://officedevblogs805426550.wordpress.com/wp-content/uploads/2021/08/pptremovewatermark.png?w=897)