Free Support Forum - groupdocs.com

Adding image watermarks in an excel file using .NET

I am using this code to add watermark to files.

public static void AddWaterMarkToFile(string FilePath, string password, string ImagePath)
{
using (FileStream stream = File.Open(FilePath, FileMode.Open, FileAccess.ReadWrite))
{

            using (Watermarker watermarker = new Watermarker(stream, new GroupDocs.Watermark.Options.LoadOptions(password)))
            {

                // Use path to the image as constructor's parameter
                using (ImageWatermark watermark = new ImageWatermark(ImagePath))
                {
                    watermark.HorizontalAlignment = GroupDocs.Watermark.Common.HorizontalAlignment.Center;
                    watermark.VerticalAlignment = VerticalAlignment.Center;
                    watermark.SizingType = SizingType.ScaleToParentArea;
                    watermarker.Add(watermark);
                }
                // Save the resultant document
                watermarker.Save();
            }
        }
    }

for other file types it works adding watermark but excel file it does not adding the watermark.
do you have any idea about this.

Thanks
Tilanka

1 Like

@tilankak

We couldn’t reproduce this issue at our end. Please have a look at the sample XLSX, output and image files - XLSX Signature.zip (26.2 KB). We investigated this scenario using the code you shared.
Can you please share following details and we’ll further look into it:

  • API version (e.g 20.2, 20.5)
  • Sample Excel file (also share its password)
  • Image and output files

when I open the given file it shows like this. Just small image attached to excel not like watermarkCapture.JPG (33.5 KB)

My api version is v2.0.50727
here is the image filerotation.png (36.1 KB)
it is not allowing to attach the excel file to this message.

Please help me on this

Thanks
Tilanka

1 Like

@tilankak

Do you mean 20.5?
Have a look at these files .zip (71.2 KB). We can successfully add the image (rotation.png) in the sample XLSX file. Please try to compress (.zip) your Excel file and then share it here.

sorry my version is 20.2.0.0

Thanks it is working now
but the problem is
if the workbook is blank, the water mark is not visible.
also not like in word documents.
the watermark added to excel can be select and delete
Here is my project with all files no password added
ConsoleApp1.zip (52.4 KB)

1 Like

@tilankak

We are investigating this scenario. Your investigation ticket ID is WATERMARKNET-1276.

We have reproduced this issue. Hence, it’s been logged in our internal issue tracking system with ID WATERMARKNET-1277. As there’s any update, you’ll be notified.

Thanks
I am looking to see updates

@tilankak

This issue is caused by the zero-sized file. When we use Windows Explorer right-click menu
to create new file (e.g. New -> Excel Spreadsheet), the zero-sized file is created. It’s impossible to recognize type of the content for the zero-sized stream. However, if the document is created and saved within Microsoft Word/Excel application, the file could be watermarked without any problem.
Concerning the empty (but not zero-sized) Excel document, the watermark is added, but stays invisible.
This happens because the code uses watermark.SizingType = SizingType.ScaleToParentArea, which makes the watermark zero-sized just like the (empty) parent area.
Before adding a watermark you can check the size of parent area as explained in this documentation article. After that, you can choose the right size for the watermark.

To protect a watermark from editing/removing, you should pass the instance of SpreadsheetWatermarkShapeOptions with set IsLocked to true as follows:

SpreadsheetWatermarkShapeOptions options = new SpreadsheetWatermarkShapeOptions();
options.IsLocked = true;
watermarker.Add(watermark, options);

thanks for the help.
above code fix the problem of watermark protect.
but watermark applies to only part of excel sheet which having content.
the blank part of sheet not having watermark.
can we set it to apply to all the sheet (all pages of sheet)

1 Like

@tilankak

Good to know that your issue is resolved.

We’re investigating this scenario. We’ll let you know if further information is required from you.

@tilankak

Please share the sample code and problematic files. We’ll then further look into this scenario.