Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aspose-com-gists/e47b38aed78fc79a3803f2fb5b3fc1f3 to your computer and use it in GitHub Desktop.
Save aspose-com-gists/e47b38aed78fc79a3803f2fb5b3fc1f3 to your computer and use it in GitHub Desktop.
Add Watermark to PowerPoint Presentations in Java
// Open presentation
Presentation pres = new Presentation("presentation.pptx");
try {
// Access slide master
IMasterSlide master = pres.getMasters().get_Item(0);
Point2D.Float center = new Point2D.Float((float) pres.getSlideSize().getSize().getWidth() / 2,
(float) pres.getSlideSize().getSize().getHeight() / 2);
float width = 300;
float height = 300;
float x = (float) center.getX() - width / 2;
float y = (float) center.getY() - height / 2;
// Add shape
IAutoShape watermarkShape = master.getShapes().addAutoShape(ShapeType.Rectangle, x, y, width, height);
IPPImage image = pres.getImages().addImage(Files.readAllBytes(Paths.get("watermark.png")));
// Set fill type
watermarkShape.getFillFormat().setFillType(FillType.Picture);
watermarkShape.getFillFormat().getPictureFillFormat().getPicture().setImage(image);
watermarkShape.getFillFormat().getPictureFillFormat().setPictureFillMode(PictureFillMode.Stretch);
watermarkShape.getLineFormat().getFillFormat().setFillType(FillType.NoFill);
// Lock Shapes from modifying
watermarkShape.getAutoShapeLock().setSelectLocked(true);
watermarkShape.getAutoShapeLock().setSizeLocked(true);
watermarkShape.getAutoShapeLock().setTextLocked(true);
watermarkShape.getAutoShapeLock().setPositionLocked(true);
watermarkShape.getAutoShapeLock().setGroupingLocked(true);
// Save the presentation
pres.save("watermarked-presentation-image.pptx", SaveFormat.Pptx);
} finally {
if (pres != null)
pres.dispose();
}
// Open presentation
Presentation pres = new Presentation("presentation.pptx");
try {
// Access master
IMasterSlide master = pres.getMasters().get_Item(0);
Point2D.Float center = new Point2D.Float((float) pres.getSlideSize().getSize().getWidth() / 2,
(float) pres.getSlideSize().getSize().getHeight() / 2);
float width = 300;
float height = 300;
float x = (float) center.getX() - width / 2;
float y = (float) center.getY() - height / 2;
// Add shape
IAutoShape watermarkShape = master.getShapes().addAutoShape(ShapeType.Rectangle, x, y, width, height);
// Set fill type
watermarkShape.getFillFormat().setFillType(FillType.NoFill);
watermarkShape.getLineFormat().getFillFormat().setFillType(FillType.NoFill);
// Set rotation angle
watermarkShape.setRotation(-45);
// Set text
ITextFrame watermarkTextFrame = watermarkShape.addTextFrame("Watermark");
// Set font and color
IPortion watermarkPortion = watermarkTextFrame.getParagraphs().get_Item(0).getPortions().get_Item(0);
watermarkPortion.getPortionFormat().setFontHeight(52);
int alpha = 150, red = 200, green = 200, blue = 200;
watermarkPortion.getPortionFormat().getFillFormat().setFillType(FillType.Solid);
watermarkPortion.getPortionFormat().getFillFormat().getSolidFillColor()
.setColor(new Color(red, green, blue, alpha));
// Lock Shapes from modifying
watermarkShape.getAutoShapeLock().setSelectLocked(true);
watermarkShape.getAutoShapeLock().setSizeLocked(true);
watermarkShape.getAutoShapeLock().setTextLocked(true);
watermarkShape.getAutoShapeLock().setPositionLocked(true);
watermarkShape.getAutoShapeLock().setGroupingLocked(true);
// Save the presentation
pres.save("watermarked-presentation.pptx", SaveFormat.Pptx);
} finally {
if (pres != null)
pres.dispose();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment