Read the complete article on adding watermark to PowerPoint presentations in C#: https://blog.aspose.com/2021/07/13/add-watermark-to-powerpoint-using-csharp/
Last active
July 14, 2021 04:39
-
-
Save aspose-com-gists/ce0af01df1fa0f11a5387d9543c6f709 to your computer and use it in GitHub Desktop.
Add Watermark to PowerPoint in C#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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