Watermarks are characters, logos, and figures that display copyright to prevent theft of still images and videos. This time, I will show you how to add/remove image watermarks and text watermarks to PowerPoint in Java using Spire.Presentation for Java.
1. From the official website of E-iceblue Free Spire. Presentation for Java Download the free version.
2. Launch the IDE to create a new project, then add the appropriate Spire. Presentation.jar to the reference that was in the installed file.
public class watermark { public static void main(String[] args) throws Exception {
//Create a presentation object.
Presentation presentation = new Presentation();
presentation.loadFromFile("C:\\Users\\Administrator\\Desktop\\Sample.pptx");
//Sets the width and length of the watermark.
int width= 400;
int height= 300;
//Create a rectangular field.
Rectangle2D.Double rect = new Rectangle2D.Double((presentation.getSlideSize().getSize().getWidth() - width) / 2,
(presentation.getSlideSize().getSize().getHeight() - height) / 2, width, height);
//Add a shape to the rectangular field.
IAutoShape shape = presentation.getSlides().get(0).getShapes().appendShape(ShapeType.RECTANGLE, rect);
/Place the shape design.
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("Copying prohibited");
PortionEx textRange = shape.getTextFrame().getTextRange();
//Place the watermark design.
textRange.getFill().setFillType(FillFormatType.SOLID);
textRange.getFill().getSolidColor().setColor(Color.pink);
textRange.setFontHeight(50);
//you save.
presentation.saveToFile("output/result.pptx", FileFormat.PPTX_2010);
}
}
<h4> <strong> Completion example </strong> </h4>
<p><img src="https://cdn-ak.f.st-hatena.com/images/fotolife/l/lendoris/20210106/20210106113430.png " alt="f:id:lendoris:20210106113430p:plain" title="" class="hatena-fotolife" itemprop="image" /></p>
<h4> <strong> Image watermark </strong> </h4>
```java
import com.spire.presentation.*;
import com.spire.presentation.drawing.*;
import javax.imageio.ImageIO;
import java.io.File;
public class addImageWatermark {
public static void main(String[] args) throws Exception {
//Load the document.
Presentation presentation = new Presentation();
presentation.loadFromFile("Sample.pptx");
//Get the image watermark.
File file =new File("logo.png ");
IImageData image = presentation.getImages().append(ImageIO.read(file));
//Gets the background attributes of the slide and places the image fill.
presentation.getSlides().get(0).getSlideBackground().setType(BackgroundType.CUSTOM);
presentation.getSlides().get(0).getSlideBackground().getFill().setFillType(FillFormatType.PICTURE);
presentation.getSlides().get(0).getSlideBackground().getFill().getPictureFill().setFillType(PictureFillType.STRETCH);
presentation.getSlides().get(0).getSlideBackground().getFill().getPictureFill().getPicture().setEmbedImage(image);
//you save
presentation.saveToFile("addImageWatermark.pptx";, FileFormat.PPTX_2013);
}
}
import com.spire.presentation.*;
import com.spire.presentation.drawing.*;
public class removeTextOrImageWatermark {
public static void main(String[] args) throws Exception {
//Load the document.
Presentation presentation = new Presentation();
presentation.loadFromFile("Sample.pptx");
//Remove the watermark in the textbook.
for (int i = 0; i < presentation.getSlides().getCount(); i++)
{
for (int j = 0; j < presentation.getSlides().get(i).getShapes().getCount(); j++)
{
if (presentation.getSlides().get(i).getShapes().get(j) instanceof IAutoShape)
{
IAutoShape shape = (IAutoShape)presentation.getSlides().get(i).getShapes().get(j);
if (shape.getTextFrame().getText().contains("E-iceblue"))
{
presentation.getSlides().get(i).getShapes().remove(shape);
}
}
}
}
//Remove the watermark in the image.
for (int i = 0; i < presentation.getSlides().getCount(); i++)
{
presentation.getSlides().get(i).getSlideBackground().getFill().setFillType(FillFormatType.NONE);
}
//you save.
presentation.saveToFile("removeTextOrImageWatermark.pptx";, FileFormat.PPTX_2013);
}
}
Recommended Posts