Add watermarks to spreadsheet documents

Adding watermark to a particular worksheet 

GroupDocs.Watermark provides an easy way to add watermark to the worksheets of any Excel document. Adding watermark to a particular Excel worksheet using GroupDocs.Watermark consists of following steps.

  1. Load the document
  2. Create and initialize watermark object
  3. Set watermark properties
  4. Create SpreadsheetWatermarkShapeOptions and set property WorksheetIndex
  5. Add watermark to the worksheet
  6. Save the document

Following code shows how to add watermark to a particular worksheet.

AdvancedUsage.AddingWatermarks.AddWatermarksToSpreadsheets.SpreadsheetAddWatermarkToWorksheet

SpreadsheetLoadOptions loadOptions = new SpreadsheetLoadOptions();
// Constants.InSpreadsheetXlsx is an absolute or relative path to your document. Ex: @"C:\Docs\spreadsheet.xlsx"
using (Watermarker watermarker = new Watermarker(Constants.InSpreadsheetXlsx, loadOptions))
{
    // Add text watermark to the first worksheet
    TextWatermark textWatermark = new TextWatermark("Test watermark", new Font("Arial", 8));
    SpreadsheetWatermarkShapeOptions textWatermarkOptions = new SpreadsheetWatermarkShapeOptions();
    textWatermarkOptions.WorksheetIndex = 0;
    watermarker.Add(textWatermark, textWatermarkOptions);

    // Add image watermark to the second worksheet
    using (ImageWatermark imageWatermark = new ImageWatermark(Constants.LogoJpg))
    {
        SpreadsheetWatermarkShapeOptions imageWatermarkOptions = new SpreadsheetWatermarkShapeOptions();
        imageWatermarkOptions.WorksheetIndex = 1;
        watermarker.Add(imageWatermark, imageWatermarkOptions);
    }

    watermarker.Save(Constants.OutSpreadsheetXlsx);
}

Getting size of content area

If for some reasons you want to use absolute sizing and positioning, you may also need to get the width and the height of the content area (range of cells which contains data).

AdvancedUsage.AddingWatermarks.AddWatermarksToSpreadsheets.SpreadsheetGetContentAreaDimensions

SpreadsheetLoadOptions loadOptions = new SpreadsheetLoadOptions();
// Constants.InSpreadsheetXlsx is an absolute or relative path to your document. Ex: @"C:\Docs\spreadsheet.xlsx"
using (Watermarker watermarker = new Watermarker(Constants.InSpreadsheetXlsx, loadOptions))
{
    SpreadsheetContent content = watermarker.GetContent<SpreadsheetContent>();

    // Get the size of content area
    Console.WriteLine(content.Worksheets[0].ContentAreaHeight);
    Console.WriteLine(content.Worksheets[0].ContentAreaWidth);

    // Get the size of particular cell
    Console.WriteLine(content.Worksheets[0].GetColumnWidth(0));
    Console.WriteLine(content.Worksheets[0].GetRowHeight(0));
}

Adding watermark to the images from a particular worksheet

Using GroupDocs.Watermark, you can add watermark to the images that belong to a particular worksheet using method FindImages() of SpreadsheetWorksheet.

AdvancedUsage.AddingWatermarks.AddWatermarksToSpreadsheets.SpreadsheetAddWatermarkToWorksheetImages

SpreadsheetLoadOptions loadOptions = new SpreadsheetLoadOptions();
// Constants.InSpreadsheetXlsx is an absolute or relative path to your document. Ex: @"C:\Docs\spreadsheet.xlsx"
using (Watermarker watermarker = new Watermarker(Constants.InSpreadsheetXlsx, loadOptions))
{
    TextWatermark watermark = new TextWatermark("Protected image", new Font("Arial", 8));
    watermark.HorizontalAlignment = HorizontalAlignment.Center;
    watermark.VerticalAlignment = VerticalAlignment.Center;
    watermark.RotateAngle = 45;
    watermark.SizingType = SizingType.ScaleToParentDimensions;
    watermark.ScaleFactor = 1;

    // Get all images from the first worksheet
    SpreadsheetContent content = watermarker.GetContent<SpreadsheetContent>();
    WatermarkableImageCollection images = content.Worksheets[0].FindImages();

    // Add watermark to all found images
    foreach (WatermarkableImage image in images)
    {
        image.Add(watermark);
    }

    watermarker.Save(Constants.OutSpreadsheetXlsx);
}

Different types of watermark in excel documents

Shapes

When you’re calling Add method of Watermarker class with SpreadsheetWatermarkShapeOptions parameter, simple shape is added to an Excel document. Besides SpreadsheetWatermarkShapeOptions there is SpreadsheetWatermarkModernWordArtOptions type which is used only with TextWatermark. Both options add watermark to an Excel document as a shape, however, there are some differences. When TextWatermark is added with SpreadsheetWatermarkShapeOptions there options, it looks and behaves like WordArt object added in Excel’2003, and SpreadsheetWatermarkModernWordArtOptions option adds text watermark that looks and behaves like Excel’2013 WordArt object.

The code sample below shows how to add modern WordArt watermark to Excel document worksheet.

AdvancedUsage.AddingWatermarks.AddWatermarksToSpreadsheets.SpreadsheetAddModernWordArdWatermark

SpreadsheetLoadOptions loadOptions = new SpreadsheetLoadOptions();
// Constants.InSpreadsheetXlsx is an absolute or relative path to your document. Ex: @"C:\Docs\spreadsheet.xlsx"
using (Watermarker watermarker = new Watermarker(Constants.InSpreadsheetXlsx, loadOptions))
{
    TextWatermark textWatermark = new TextWatermark("Test watermark", new Font("Arial", 8));
    SpreadsheetWatermarkModernWordArtOptions options = new SpreadsheetWatermarkModernWordArtOptions();
    options.WorksheetIndex = 0;

    watermarker.Add(textWatermark, options);
    watermarker.Save(Constants.OutSpreadsheetXlsx);
}

Shape additional options

The API also provides the feature to set some additional options (Name, AlternativeText and IsLocked) when adding shape watermark to Excel worksheet (as shown in the below sample).

AdvancedUsage.AddingWatermarks.AddWatermarksToSpreadsheets.SpreadsheetAddWatermarkUsingShapeSettings

SpreadsheetLoadOptions loadOptions = new SpreadsheetLoadOptions();
// Constants.InSpreadsheetXlsx is an absolute or relative path to your document. Ex: @"C:\Docs\spreadsheet.xlsx"
using (Watermarker watermarker = new Watermarker(Constants.InSpreadsheetXlsx, loadOptions))
{
    TextWatermark watermark = new TextWatermark("Test watermark", new Font("Segoe UI", 19));
    SpreadsheetWatermarkShapeOptions options = new SpreadsheetWatermarkShapeOptions();

    // Set the shape name
    options.Name = "Shape 1";

    // Set the descriptive (alternative) text that will be associated with the shape
    options.AlternativeText = "Test watermark";

    // Editing of the shape in Excel is forbidden
    options.IsLocked = true;

    watermarker.Add(watermark, options);
    watermarker.Save(Constants.OutSpreadsheetXlsx);
}

Text effects

You can also apply text effects when adding shape watermark in Excel worksheet as shown in below code sample.

AdvancedUsage.AddingWatermarks.AddWatermarksToSpreadsheets.SpreadsheetAddWatermarkWithTextEffects

SpreadsheetLoadOptions loadOptions = new SpreadsheetLoadOptions();
// Constants.InSpreadsheetXlsx is an absolute or relative path to your document. Ex: @"C:\Docs\spreadsheet.xlsx"
using (Watermarker watermarker = new Watermarker(Constants.InSpreadsheetXlsx, loadOptions))
{
    TextWatermark watermark = new TextWatermark("Test watermark", new Font("Segoe UI", 19));

    SpreadsheetTextEffects effects = new SpreadsheetTextEffects();
    effects.LineFormat.Enabled = true;
    effects.LineFormat.Color = Color.Red;
    effects.LineFormat.DashStyle = OfficeDashStyle.DashDotDot;
    effects.LineFormat.LineStyle = OfficeLineStyle.Triple;
    effects.LineFormat.Weight = 1;

    SpreadsheetWatermarkShapeOptions options = new SpreadsheetWatermarkShapeOptions();
    options.Effects = effects;

    watermarker.Add(watermark, options);
    watermarker.Save(Constants.OutSpreadsheetXlsx);
}

Image effects

The API also allows you to apply image effects to the shape watermark using below code sample.

AdvancedUsage.AddingWatermarks.AddWatermarksToSpreadsheets.SpreadsheetAddWatermarkWithImageEffects

SpreadsheetLoadOptions loadOptions = new SpreadsheetLoadOptions();
// Constants.InSpreadsheetXlsx is an absolute or relative path to your document. Ex: @"C:\Docs\spreadsheet.xlsx"
using (Watermarker watermarker = new Watermarker(Constants.InSpreadsheetXlsx, loadOptions))
{
    using (ImageWatermark watermark = new ImageWatermark(Constants.LogoPng))
    {
        SpreadsheetImageEffects effects = new SpreadsheetImageEffects();
        effects.Brightness = 0.7;
        effects.Contrast = 0.6;
        effects.ChromaKey = Color.Red;
        effects.BorderLineFormat.Enabled = true;
        effects.BorderLineFormat.Weight = 1;

        SpreadsheetWatermarkShapeOptions options = new SpreadsheetWatermarkShapeOptions();
        options.Effects = effects;

        watermarker.Add(watermark, options);
    }

    watermarker.Save(Constants.OutSpreadsheetXlsx);
}

Worksheet backgrounds

Microsoft Office documentation says that Excel does not support adding of watermarks, however, it offers some workarounds. One of them is using worksheet background images as watermarks.

Use the following code sample to add background watermark to all worksheets of Excel document.

AdvancedUsage.AddingWatermarks.AddWatermarksToSpreadsheets.SpreadsheetAddWatermarkAsBackground

SpreadsheetLoadOptions loadOptions = new SpreadsheetLoadOptions();
// Constants.InSpreadsheetXlsx is an absolute or relative path to your document. Ex: @"C:\Docs\spreadsheet.xlsx"
using (Watermarker watermarker = new Watermarker(Constants.InSpreadsheetXlsx, loadOptions))
{
    using (ImageWatermark watermark = new ImageWatermark(Constants.LogoGif))
    {
        SpreadsheetBackgroundWatermarkOptions options = new SpreadsheetBackgroundWatermarkOptions();
        watermarker.Add(watermark, options);
    }

    watermarker.Save(Constants.OutSpreadsheetXlsx);
}

Worksheet background image size

You can also define the size (width and height) of the background image on which your watermark will be drawn. This feature allows you to mimic watermark relative size and position.

AdvancedUsage.AddingWatermarks.AddWatermarksToSpreadsheets.SpreadsheetAddWatermarkAsBackgroundWithRelativeSizeAndPosition

SpreadsheetLoadOptions loadOptions = new SpreadsheetLoadOptions();
// Constants.InSpreadsheetXlsx is an absolute or relative path to your document. Ex: @"C:\Docs\spreadsheet.xlsx"
using (Watermarker watermarker = new Watermarker(Constants.InSpreadsheetXlsx, loadOptions))
{
    using (ImageWatermark watermark = new ImageWatermark(Constants.LogoGif))
    {
        watermark.HorizontalAlignment = HorizontalAlignment.Center;
        watermark.VerticalAlignment = VerticalAlignment.Center;
        watermark.RotateAngle = 90;
        watermark.SizingType = SizingType.ScaleToParentDimensions;
        watermark.ScaleFactor = 0.5;

        SpreadsheetContent content = watermarker.GetContent<SpreadsheetContent>();
        SpreadsheetBackgroundWatermarkOptions options = new SpreadsheetBackgroundWatermarkOptions();
        options.BackgroundWidth = content.Worksheets[0].ContentAreaWidthPx; /* set background width */
        options.BackgroundHeight = content.Worksheets[0].ContentAreaHeightPx; /* set background height */

        watermarker.Add(watermark, options);
    }

    watermarker.Save(Constants.OutSpreadsheetXlsx);
}

TextWatermark as background

Excel does not support text backgrounds but you still can pass TextWatermark instance with the SpreadsheetBackgroundWatermarkOptions option. The text will be converted to image preserving formatting. The following code sample demonstrates this feature.

AdvancedUsage.AddingWatermarks.AddWatermarksToSpreadsheets.SpreadsheetAddTextWatermarkAsBackground

SpreadsheetLoadOptions loadOptions = new SpreadsheetLoadOptions();
// Constants.InSpreadsheetXlsx is an absolute or relative path to your document. Ex: @"C:\Docs\spreadsheet.xlsx"
using (Watermarker watermarker = new Watermarker(Constants.InSpreadsheetXlsx, loadOptions))
{
    TextWatermark watermark = new TextWatermark("Test watermark", new Font("Segoe UI", 19));
    watermark.HorizontalAlignment = HorizontalAlignment.Center;
    watermark.VerticalAlignment = VerticalAlignment.Center;
    watermark.RotateAngle = 45;
    watermark.SizingType = SizingType.ScaleToParentDimensions;
    watermark.ScaleFactor = 0.5;
    watermark.Opacity = 0.5;

    SpreadsheetContent content = watermarker.GetContent<SpreadsheetContent>();

    SpreadsheetBackgroundWatermarkOptions options = new SpreadsheetBackgroundWatermarkOptions();
    options.BackgroundWidth = content.Worksheets[0].ContentAreaWidthPx; /* set background width */
    options.BackgroundHeight = content.Worksheets[0].ContentAreaHeightPx; /* set background height */

    watermarker.Add(watermark, options);
    watermarker.Save(Constants.OutSpreadsheetXlsx);
}

Another way to mimic watermark in Excel is to use Headers and Footers. You can add watermark to worksheet’s header or footer using below code sample.

AdvancedUsage.AddingWatermarks.AddWatermarksToSpreadsheets.SpreadsheetAddImageWatermarkIntoHeaderFooter

SpreadsheetLoadOptions loadOptions = new SpreadsheetLoadOptions();
// Constants.InSpreadsheetXlsx is an absolute or relative path to your document. Ex: @"C:\Docs\spreadsheet.xlsx"
using (Watermarker watermarker = new Watermarker(Constants.InSpreadsheetXlsx, loadOptions))
{
    using (ImageWatermark watermark = new ImageWatermark(Constants.LogoPng))
    {
        watermark.VerticalAlignment = VerticalAlignment.Top;
        watermark.HorizontalAlignment = HorizontalAlignment.Center;
        watermark.SizingType = SizingType.ScaleToParentDimensions;
        watermark.ScaleFactor = 1;

        SpreadsheetWatermarkHeaderFooterOptions options = new SpreadsheetWatermarkHeaderFooterOptions();
        options.WorksheetIndex = 0;

        watermarker.Add(watermark, options);
    }

    watermarker.Save(Constants.OutSpreadsheetXlsx);
}

You can also add text watermark in header or footer as shown in the below code sample.

AdvancedUsage.AddingWatermarks.AddWatermarksToSpreadsheets.SpreadsheetAddTextWatermarkIntoHeaderFooter

SpreadsheetLoadOptions loadOptions = new SpreadsheetLoadOptions();
// Constants.InSpreadsheetXlsx is an absolute or relative path to your document. Ex: @"C:\Docs\spreadsheet.xlsx"
using (Watermarker watermarker = new Watermarker(Constants.InSpreadsheetXlsx, loadOptions))
{
    TextWatermark watermark = new TextWatermark("Test watermark", new Font("Segoe UI", 19, FontStyle.Bold));
    watermark.ForegroundColor = Color.Red;
    watermark.BackgroundColor = Color.Aqua;
    watermark.VerticalAlignment = VerticalAlignment.Top;
    watermark.HorizontalAlignment = HorizontalAlignment.Center;

    SpreadsheetWatermarkHeaderFooterOptions options = new SpreadsheetWatermarkHeaderFooterOptions();
    options.WorksheetIndex = 0;

    watermarker.Add(watermark, options);
    watermarker.Save(Constants.OutSpreadsheetXlsx);
}

Advanced use cases

More resources

GitHub examples

You may easily run the code above and see the feature in action in our GitHub examples:

Free online document watermarking App

Along with full featured .NET library we provide simple, but powerful free Apps.

You are welcome to add watermark to PDF, DOC, DOCX, PPT, PPTX, XLS, XLSX, Emails and more with our free online Free Online Document Watermarking App.