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 a SpreadsheetWatermarkShapeOptions class and set the WorksheetIndex property
  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();
// Specify an absolute or relative path to your document. Ex: @"C:\Docs\spreadsheet.xlsx"
using (Watermarker watermarker = new Watermarker("spreadsheet.xlsx", 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("logo.jpg"))
    {
        SpreadsheetWatermarkShapeOptions imageWatermarkOptions = new SpreadsheetWatermarkShapeOptions();
        imageWatermarkOptions.WorksheetIndex = 1;
        watermarker.Add(imageWatermark, imageWatermarkOptions);
    }

    watermarker.Save("spreadsheet.xlsx");
}

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();
// Specify an absolute or relative path to your document. Ex: @"C:\Docs\spreadsheet.xlsx"
using (Watermarker watermarker = new Watermarker("spreadsheet.xlsx", 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 the FindImages() method of the SpreadsheetWorksheet class.

AdvancedUsage.AddingWatermarks.AddWatermarksToSpreadsheets.SpreadsheetAddWatermarkToWorksheetImages

SpreadsheetLoadOptions loadOptions = new SpreadsheetLoadOptions();
// Specify an absolute or relative path to your document. Ex: @"C:\Docs\spreadsheet.xlsx"
using (Watermarker watermarker = new Watermarker("spreadsheet.xlsx", 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("spreadsheet.xlsx");
}

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();
// Specify an absolute or relative path to your document. Ex: @"C:\Docs\spreadsheet.xlsx"
using (Watermarker watermarker = new Watermarker("spreadsheet.xlsx", loadOptions))
{
    TextWatermark textWatermark = new TextWatermark("Test watermark", new Font("Arial", 8));
    SpreadsheetWatermarkModernWordArtOptions options = new SpreadsheetWatermarkModernWordArtOptions();
    options.WorksheetIndex = 0;

    watermarker.Add(textWatermark, options);
    watermarker.Save("spreadsheet.xlsx");
}

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();
// Specify an absolute or relative path to your document. Ex: @"C:\Docs\spreadsheet.xlsx"
using (Watermarker watermarker = new Watermarker("spreadsheet.xlsx", 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("spreadsheet.xlsx");
}

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();
// Specify an absolute or relative path to your document. Ex: @"C:\Docs\spreadsheet.xlsx"
using (Watermarker watermarker = new Watermarker("spreadsheet.xlsx", 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("spreadsheet.xlsx");
}

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();
// Specify an absolute or relative path to your document. Ex: @"C:\Docs\spreadsheet.xlsx"
using (Watermarker watermarker = new Watermarker("spreadsheet.xlsx", loadOptions))
{
    using (ImageWatermark watermark = new ImageWatermark("logo.png"))
    {
        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("spreadsheet.xlsx");
}

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();
// Specify an absolute or relative path to your document. Ex: @"C:\Docs\spreadsheet.xlsx"
using (Watermarker watermarker = new Watermarker("spreadsheet.xlsx", loadOptions))
{
    using (ImageWatermark watermark = new ImageWatermark("logo.gif"))
    {
        SpreadsheetBackgroundWatermarkOptions options = new SpreadsheetBackgroundWatermarkOptions();
        watermarker.Add(watermark, options);
    }

    watermarker.Save("spreadsheet.xlsx");
}
Note
Backgrounds are viewable in Normal View in the worksheet and are invisible in Page Layout mode. The image is automatically tiled on the background of the worksheet. Excel formats don’t support background image customization. Using properties of image watermark (size, rotation etc) will cause image redrawing. This may lead to the decrease in performance.

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();
// Specify an absolute or relative path to your document. Ex: @"C:\Docs\spreadsheet.xlsx"
using (Watermarker watermarker = new Watermarker("spreadsheet.xlsx", loadOptions))
{
    using (ImageWatermark watermark = new ImageWatermark("logo.gif"))
    {
        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("spreadsheet.xlsx");
}
Warning
This method assumes that watermark absolute coordinates and size are measured in pixels (if they are assigned).

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();
// Specify an absolute or relative path to your document. Ex: @"C:\Docs\spreadsheet.xlsx"
using (Watermarker watermarker = new Watermarker("spreadsheet.xlsx", 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("spreadsheet.xlsx");
}

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();
// Specify an absolute or relative path to your document. Ex: @"C:\Docs\spreadsheet.xlsx"
using (Watermarker watermarker = new Watermarker("spreadsheet.xlsx", loadOptions))
{
    using (ImageWatermark watermark = new ImageWatermark("logo.png"))
    {
        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("spreadsheet.xlsx");
}

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();
// Specify an absolute or relative path to your document. Ex: @"C:\Docs\spreadsheet.xlsx"
using (Watermarker watermarker = new Watermarker("spreadsheet.xlsx", 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("spreadsheet.xlsx");
}

Warning
You’ll see the watermark in Excel only when you’re in Page Layout view or Print Preview.
Warning
Excel Headersand footers are not designed for watermarking, so, some features don’t work for header and footer watermarks.

Advanced use cases