Adding a Watermark to PPT and PPTX files with Aspose.Slides

Is there a way to add a text watermark that would be on every slide in the PPT or PPTX file using Aspose.Slides?

Thanks, Shannon

Hi Shannon,

Here is sample code to add a simple water mark on a PPT slide:

Presentation pres = new Presentation();

Slide sld = pres.GetSlideByPosition(1);

TextFrame tf = sld.Shapes.AddRectangle(0, 0, 500, 500).AddTextFrame("Water Mark Text");

tf.LineFormat.ShowLines = false;

tf.Protection = ShapeProtection.LockSelect;

To learn how to apply formatting to the text in a TextFrame, visit here.

Thanks. After being sent to another project for a while, I am back on this and have two follow questions:


1) Is there a way to add a watermark that is on every slide or do I have to cycle through each slide and add the watermark?

2) I will need to find and replace or delete this watermark at a later date. Is there a way to tag or name this so it can be found and removed later?

Much thanks,

Shannon

Dear Shannon,

There are two options for adding the watermark. You can add a shape containing watermark in master slide that is used by every slide or most of slides. This way the watermark will also be added on every slide. Secondly, you may add the watermark shape on each slide. You can use the Shape.AlternativeText property to add the name to the shape alternative text name. So, in future, if you want to delete that shape, you may find that shape by comparing equivalent text as that of stored in Shape.AlternativeText.

Thanks and Regards,

Thanks. One last set of questions. I followed your example, but am having trouble figuring out how to center the watermark and bring it to the front so it is not getting hidden behind other shapes. Also, using the Portion object for font does not seem to have a property for opacity, is there a way to make the watermark semi-transparent and on top?


Thanks,

Shannon

Dear Shannon,

You can use the following code snippet to bring the shape forward and back ward from other shapes. You may use ZOrderCmd enumeration to for shapes to bring them forward and backward. Please follow this link for further details.

slide.Shapes[0].ZOrder(ZOrderCmd.BringToFront);

In order to know how to set the transparency to text in the portion, please proceed to this thread link.

Thanks and Regards,

Thanks,


1. As long as I use each Slide the zOrder works as expected. With the Master it does not bring it to the front.
2. As far as opacity, I want semi-transparent text not background. When applying the alpha channel to the Portion object, the opacity is unaffected.

More importantly, how does this work with .PPTX documents? There does not seem to be a strait translation to ShapeEx, there is no RectangleEx, AlternativeText is not a property of ShapeEx. Is there a better way to add watermarks with pptx docs?

thanks,

Shannon

private void WatermarkSlide(SlideEx slide, string watermarkName, string watermarkText)
{
//Calculate size and location
float x = 0;
float y = 0;
float h = slide.Parent.SlideSize.Size.Height;
float w = slide.Parent.SlideSize.Size.Width;
//Create a Rectangle
ShapesEx rect = slide.Shapes.(ShapeTypeEx.Rectangle,x,y,w,h);
//Hide the Rectangle Borders
rect.LineFormat.ShowLines = false;
//Create a TextFrame for the rectangle
TextFrameEx tf = rect.AddAutoShape(watermarkText);
//Set Name so we can find and remove it later
rect.AlternativeText = watermarkName;
//Set verticle alignment of the text
tf.AnchorText = AnchorText.MiddleCentered;
//Set WrapeText property
tf.WrapText = true;
//Alter shape according to the text
tf.FitShapeToText = true;
tf.Protection = ShapeProtection.LockSelect;
//Apply some formatting to the text
int alpha = 150, red = 200, green = 200, blue = 200;
Portion port = tf.Paragraphs[0].Portions[0];
port.FontHeight = 32;
port.FontBold = true;
port.FontColor = System.Drawing.Color.FromArgb(alpha, red, green, blue);
rect. = Convert.ToInt32((w - tf.Width) / 2);
rect.Y = Convert.ToInt32((h - tf.Height) / 2) + port.FontHeight/2;
rect.Rotation = calculateRotation(h, w);
rect.ZOrder(ZOrderCmd.BringToFront);
}
private int calculateRotation(int height, int width)
{
double pageHeight = Convert.ToDouble(height);
double pageWidth = Convert.ToDouble(width);
double rotation = Math.Atan((pageHeight / pageWidth)) * 180 / Math.PI;
return Convert.ToInt32(rotation + 180);
}

Hi Shannon,

I am sorry for the delayed response.

I have worked with Shape.zOrder property and have been able to observe the effect of zOrder with shapes. For your kind reference, I have shared the code snippet for you as well.

Presentation pres = new Presentation();
Slide sld = pres.GetSlideByPosition(1);

TextFrame tf1 = sld.Shapes.AddRectangle(0, 0, 500, 500).AddTextFrame("Water Mark Text");
tf1.LineFormat.ShowLines = false;
tf1.Protection = ShapeProtection.LockSelect;

TextFrame tf2 = sld.Shapes.AddRectangle(0, 0, 1100, 700).AddTextFrame("Hello World");

//sld.FollowMasterObjects = false;
// sld.Shapes[1].ZOrder(ZOrderCmd.SendToBack);

sld.Shapes[0].ZOrder(ZOrderCmd.BringToFront);
Shape shp = sld.Shapes[1];
shp.FillFormat.Type = FillType.Solid;
shp.FillFormat.ForeColor. = System.Drawing.Color.Blue;
shp = sld.Shapes[0];
shp.FillFormat.Type = FillType.Solid;
shp.FillFormat.ForeColor = System.Drawing.Color.Yellow;

pres.Write("D:\\lock.ppt");

The equivalent property to zOrder in PPTX is using ShapesEx.Reorder() method in PPTX. Please proceed to this link for further details. AlternativeText is also property of ShapeEx class. Please follow this link for further details. The rectangle shape is one of AutoShapeEx type. Please share with us, if there is anything I can offer you.

Thanks and Regards,