Set Transparency & Opacity as ColorMatrix.Matrix33 & Add Text or Image Watermark to a Word Document C# .NET

Hey Team,

Shape watermark = new Shape(doc, Aspose.Words.Drawing.ShapeType.TextPlainText);
watermark.ZOrder = 2;
Just wondering how to set opacity in watermark?

@neethuan,

Please elaborate your inquiry further by providing complete details of your use-case along with simplified input Word document containing the watermark and the expected document showing the desired output here for our reference. You can create expected document by using MS Word or any other editor. We will then investigate the scenario on our end and provide you more information.

@awais.hafeez, I am attaching the screenshot of watermark placed over the text. I’m just wondering if I can set the transparency of the watermark(to 60%Capture.PNG (46.4 KB)
) so that I can read the text beneath it?

@neethuan,

Please check the following article to learn different ways of adding (transparent) watermarks in Word document:

@awais.hafeez…Thanks for this…I’m just wondering if it is possible to set the transparency value(say 60%) for the shape-based watermark. Is there any property opacity or transparency in Shape?

@neethuan,

For example, the following code of Aspose.Imaging for .NET API creates a blank canvas of type PNG with transparency and draws another image over it while specifying the desired opacity (ColorMatrix.Matrix33). Please note, 0.6f means 60% so you may set it to any desired value.

using (PngImage pngImage = new PngImage(1500, 1500, PngColorType.TruecolorWithAlpha))
{
    ColorMatrix cmxPic = new ColorMatrix();
    cmxPic.Matrix33 = 0.6f;
    ImageAttributes iaPic = new ImageAttributes();
    iaPic.SetColorMatrix(cmxPic, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);

    using (Image loaded = Image.Load(“d:/temp/logo.png”))
    {
        Graphics gr = new Graphics(pngImage);
        gr.DrawImage(loaded, loaded.Bounds, GraphicsUnit.Pixel, iaPic);
    }
    pngImage.Save(“D:/newPng.png”);
}

After that your Image is processed, you will then be able to insert image inside Word document and set its different parameters:

Document doc = new Document("E:\\Temp\\in.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
builder.MoveToDocumentEnd();
Shape shape = builder.InsertImage("E:\\Temp\\Aspose.Words.jpg");
shape.Fill.Opacity = 0.7;
shape.Stroke.On = true;
shape.Stroke.Color = doc.Theme.Colors.Accent1;
shape.Stroke.Weight = 5;
shape.Stroke.Opacity = 0.7;
doc.Save("E:\\Temp\\20.9.docx");

Please let us know if this solution works for you?

1 Like

That perfectly works for me…Thanks @awais.hafeez…I really appreciate that

@neethuan,

Thanks for your feedback. Please let us know any time you may have any further queries in future.