· 

Java - Add Text or Image Watermark to PowerPoint

A watermark is a faded background image that displays behind the text in a document. You can use them to indicate a document’s state (confidential, draft, etc.), or add a subtle company logo. PowerPoint doesn’t have a built-in watermark feature like Word, but you can still add them with a shape. In this article, I am going to introduce how to insert text and image watermark to PowerPoint by using Spire.Presentation for Java.

Installing Spire.Presentation.jar

If you create a Maven project, you can easily import the jar in your application using the following configurations. For non-Maven projects, download the jar file from this link and add it as a dependency in your application.

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

Example 1. Add text watermark to PowerPoint

import com.spire.presentation.*;
import com.spire.presentation.drawing.FillFormatType;

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

public class AddTextWatermark {

   
public static void main(String[] args) throws Exception {

       
//Load the sample file
       
Presentation presentation = new Presentation();
        presentation.loadFromFile(
"C:\\Users\\Administrator\\Desktop\\sample.pptx");

       
//Add text watermark to each slide
       
for (int i = 0; i < presentation.getSlides().getCount(); i++) {

            AddTextWatermark(presentation.getSlides().get(i),
"Internal Use Only");
        }

       
//Save the document
       
presentation.saveToFile("TextWatermark.pptx", FileFormat.PPTX_2013);
    }

   
private static void AddTextWatermark(ISlide slide, String text) throws Exception{

       
//Define a rectangle
       
int width= 300;
       
int height= 30;
        Presentation presentation = slide.getPresentation();
        Rectangle2D.Double rect =
new Rectangle2D.Double((presentation.getSlideSize().getSize().getWidth() - width) / 2,
                (presentation.getSlideSize().getSize().getHeight() - height) /
2, width, height);

       
//Create a shape based on the rectangle
       
IAutoShape shape = slide.getShapes().appendShape(ShapeType.RECTANGLE, rect);

       
//Set the style of shape
       
shape.getFill().setFillType(FillFormatType.NONE);
        shape.setRotation(-
45);
        shape.getLocking().setSelectionProtection(
true);
        shape.getLine().setFillType(FillFormatType.
NONE);

       
//Add text to shape
       
shape.getTextFrame().setText(text);
        PortionEx textRange = shape.getTextFrame().getTextRange();

       
//Set the style of the text range
       
textRange.getFill().setFillType(FillFormatType.SOLID);
        textRange.getFill().getSolidColor().setColor(
new Color(50,50,50,65));
        textRange.setFontHeight(
30);

 

 

        //Send the shape to back
       
shape.setShapeArrange(ShapeAlignmentEnum.ShapeArrange.SendToBack
);
    }

}

Example 2. Add image watermark to PowerPoint

import com.spire.presentation.*;
import com.spire.presentation.drawing.FillFormatType;
import com.spire.presentation.drawing.IImageData;
import com.spire.presentation.drawing.PictureFillType;

import javax.imageio.ImageIO;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.FileInputStream;

public class AddmageWatermark {

   
public static void main(String[] args) throws Exception {

       
//Create a Presentation instance
       
Presentation presentation = new Presentation();

       
//Load a sample PowerPoint file
       
presentation.loadFromFile("C:\\Users\\Administrator\\Desktop\\sample.pptx");

       
//Add text watermark to each slide
       
for (int i = 0; i < presentation.getSlides().getCount(); i++) {

            AddImageWatermark(presentation.getSlides().get(i),
"C:\\Users\\Administrator\\Desktop\\company-logo.png");
        }

       
//Save the document
       
presentation.saveToFile("ImageWatermark.pptx", FileFormat.PPTX_2013);
    }

   
private static void AddImageWatermark(ISlide slide, String picUrl) throws Exception {

       
//Load an image
       
BufferedImage bufferedImage = ImageIO.read(new FileInputStream(picUrl));
        IImageData imageData = slide.getPresentation().getImages().append(bufferedImage);

       
//Define a rectangle
       
int width = imageData.getWidth();
       
int height = imageData.getHeight();
        Presentation presentation = slide.getPresentation();
        Rectangle2D.Double rect =
new Rectangle2D.Double((presentation.getSlideSize().getSize().getWidth() - width) / 2,
                (presentation.getSlideSize().getSize().getHeight() - height) /
2, width, height);

       
//Create a shape based on the rectangle
       
IAutoShape shape = slide.getShapes().appendShape(ShapeType.RECTANGLE, rect);

       
//Fill the shape with picture
       
shape.getLine().setFillType(FillFormatType.NONE);
        shape.getLocking().setSelectionProtection(
true);
        shape.getFill().setFillType(FillFormatType.
PICTURE);
        shape.getFill().getPictureFill().setFillType(PictureFillType.
STRETCH);
        shape.getFill().getPictureFill().getPicture().setEmbedImage(imageData);

       
//Send the shape to back
       
shape.setShapeArrange(ShapeAlignmentEnum.ShapeArrange.SendToBack);
    }
}

Write a comment

Comments: 0