Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Last active July 14, 2021 04:39
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/ce0af01df1fa0f11a5387d9543c6f709 to your computer and use it in GitHub Desktop.
Save aspose-com-gists/ce0af01df1fa0f11a5387d9543c6f709 to your computer and use it in GitHub Desktop.
Add Watermark to PowerPoint in C#
// Load presentation
Presentation presentation = new Presentation("presentation.pptx");
// Get reference of the slide
ISlide slide = presentation.Slides[0];
// Get the center of the slide and calculate watermark's position
PointF center = new PointF(presentation.SlideSize.Size.Width / 2, presentation.SlideSize.Size.Height / 2);
float width = 300;
float height = 300;
float x = center.X - width / 2;
float y = center.Y - height / 2;
// Load image
IPPImage image = presentation.Images.AddImage(File.ReadAllBytes("watermark.png"));
// Add watermark shape and set image
IAutoShape watermarkShape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, x, y, width, height);
watermarkShape.FillFormat.FillType = FillType.Picture;
watermarkShape.FillFormat.PictureFillFormat.Picture.Image = image;
watermarkShape.FillFormat.PictureFillFormat.PictureFillMode = PictureFillMode.Stretch;
watermarkShape.LineFormat.FillFormat.FillType = FillType.NoFill;
// Lock Shapes from modifying
watermarkShape.ShapeLock.SelectLocked = true;
watermarkShape.ShapeLock.SizeLocked = true;
watermarkShape.ShapeLock.TextLocked = true;
watermarkShape.ShapeLock.PositionLocked = true;
watermarkShape.ShapeLock.GroupingLocked = true;
// Save the presentation
presentation.Save("watermarked-presentation.pptx", SaveFormat.Pptx);
// Load presentation
Presentation presentation = new Presentation("presentation.pptx");
// Get reference of the slide
ISlide slide = presentation.Slides[0];
// Get the center of the slide and calculate watermark's position
PointF center = new PointF(presentation.SlideSize.Size.Width / 2, presentation.SlideSize.Size.Height / 2);
float width = 300;
float height = 300;
float x = center.X - width / 2;
float y = center.Y - height / 2;
// Add watermark shape
IAutoShape watermarkShape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, x, y, width, height);
// Set fill type
watermarkShape.FillFormat.FillType = FillType.NoFill;
watermarkShape.LineFormat.FillFormat.FillType = FillType.NoFill;
// Set rotation angle
watermarkShape.Rotation = -45;
// Set text
ITextFrame watermarkTextFrame = watermarkShape.AddTextFrame("Watermark");
IPortion watermarkPortion = watermarkTextFrame.Paragraphs[0].Portions[0];
// Set font size and fill type of the watermark
watermarkPortion.PortionFormat.FontHeight = 52;
watermarkPortion.PortionFormat.FillFormat.FillType = FillType.Solid;
int alpha = 150, red = 200, green = 200, blue = 200;
watermarkPortion.PortionFormat.FillFormat.SolidFillColor.Color = System.Drawing.Color.FromArgb(alpha, red, green, blue);
// Lock Shapes from modifying
watermarkShape.ShapeLock.SelectLocked = true;
watermarkShape.ShapeLock.SizeLocked = true;
watermarkShape.ShapeLock.TextLocked = true;
watermarkShape.ShapeLock.PositionLocked = true;
watermarkShape.ShapeLock.GroupingLocked = true;
// Save the presentation
presentation.Save("watermarked-presentation.pptx", SaveFormat.Pptx);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment