Search code examples
c#aspose-cells

How add Water Mark to complete sheet of Excel using Aspose library in C#


I'm using Aspose library to generate Excel report in this I'm facing issue to generate watermark to complete excel.I can able to generate watermark for only some cells but here I need to do for complete WorkSheet enter image description here

I'm using below code to generate watermark in some part

// Instantiate a new Workbook
Workbook workbook = new Workbook("Excel.xlsx");

// Get the first default sheet
Worksheet sheet = workbook.Worksheets[0];

// Add watermark
Aspose.Cells.Drawing.Shape wordart = sheet.Shapes.AddTextEffect(MsoPresetTextEffect.TextEffect1,
"CONFIDENTIAL", "Arial Black", 50, false, true
, 18, 8, 1, 1, 130, 800);

// Lock shape aspects
wordart.IsLocked = true;
wordart.SetLockedProperty(ShapeLockType.Selection, true);
wordart.SetLockedProperty(ShapeLockType.ShapeType, true);
wordart.SetLockedProperty(ShapeLockType.Move, true);
wordart.SetLockedProperty(ShapeLockType.Resize, true);
wordart.SetLockedProperty(ShapeLockType.Text, true);

// Get the fill format of the word art
FillFormat wordArtFormat = wordart.Fill;

// Set the transparency
wordArtFormat.Transparency = 0.9; 

// Save the watermarked Excel file
workbook.Save("Watermarked-locked.xlsx");

Solution

  • Your code will insert word art watermark one time and at your sepcified location in the worksheet. I think you want the watermarks should be pasted multiple times automatically at different places regularly in the worksheet. If so, you have to place watermarks multiple times at your desired locations in code accordingly. Please note, even in MS Excel you got to copy/paste wordarts and pictures manually at your desired places, so these shapes would not be inserted multiple times in MS Excel either. Another option can be, you can set the background picture for the sheet as watermark (this will be pasted multiple times automatically). First you may create an image for your desired watermark/wordart and save to image format. See the following sample code on how to set background (watermark) for the worksheet:

    e.g.

    Sample code:

    // Instantiate a new Workbook
    Workbook workbook = new Workbook("Excel.xlsx");
    // Get the first default sheet
    Worksheet sheet = workbook.Worksheets[0];
    
    // Define a string variable to store the (word-art, you have already created the picture of your watermark) image path.
    // Set the background image for the worksheet.
    sheet.BackgroundImage = File.ReadAllBytes("mywatermark.jpg");
    
    // Save the Excel file
    workbook.Save("outputWatermark.xlsx");
    

    You can also post your queries in the dedicated forum.

    PS. I am working as Support developer/ Evangelist at Aspose.