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.
SpreadsheetLoadOptionsloadOptions=newSpreadsheetLoadOptions();// Specify an absolute or relative path to your document. Ex: @"C:\Docs\spreadsheet.xlsx"using(Watermarkerwatermarker=newWatermarker("spreadsheet.xlsx",loadOptions)){// Add text watermark to the first worksheetTextWatermarktextWatermark=newTextWatermark("Test watermark",newFont("Arial",8));SpreadsheetWatermarkShapeOptionstextWatermarkOptions=newSpreadsheetWatermarkShapeOptions();textWatermarkOptions.WorksheetIndex=0;watermarker.Add(textWatermark,textWatermarkOptions);// Add image watermark to the second worksheetusing(ImageWatermarkimageWatermark=newImageWatermark("logo.jpg")){SpreadsheetWatermarkShapeOptionsimageWatermarkOptions=newSpreadsheetWatermarkShapeOptions();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).
SpreadsheetLoadOptionsloadOptions=newSpreadsheetLoadOptions();// Specify an absolute or relative path to your document. Ex: @"C:\Docs\spreadsheet.xlsx"using(Watermarkerwatermarker=newWatermarker("spreadsheet.xlsx",loadOptions)){SpreadsheetContentcontent=watermarker.GetContent<SpreadsheetContent>();// Get the size of content areaConsole.WriteLine(content.Worksheets[0].ContentAreaHeight);Console.WriteLine(content.Worksheets[0].ContentAreaWidth);// Get the size of particular cellConsole.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.
SpreadsheetLoadOptionsloadOptions=newSpreadsheetLoadOptions();// Specify an absolute or relative path to your document. Ex: @"C:\Docs\spreadsheet.xlsx"using(Watermarkerwatermarker=newWatermarker("spreadsheet.xlsx",loadOptions)){TextWatermarkwatermark=newTextWatermark("Protected image",newFont("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 worksheetSpreadsheetContentcontent=watermarker.GetContent<SpreadsheetContent>();WatermarkableImageCollectionimages=content.Worksheets[0].FindImages();// Add watermark to all found imagesforeach(WatermarkableImageimageinimages){image.Add(watermark);}watermarker.Save("spreadsheet.xlsx");}
SpreadsheetLoadOptionsloadOptions=newSpreadsheetLoadOptions();// Specify an absolute or relative path to your document. Ex: @"C:\Docs\spreadsheet.xlsx"using(Watermarkerwatermarker=newWatermarker("spreadsheet.xlsx",loadOptions)){TextWatermarktextWatermark=newTextWatermark("Test watermark",newFont("Arial",8));SpreadsheetWatermarkModernWordArtOptionsoptions=newSpreadsheetWatermarkModernWordArtOptions();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).
SpreadsheetLoadOptionsloadOptions=newSpreadsheetLoadOptions();// Specify an absolute or relative path to your document. Ex: @"C:\Docs\spreadsheet.xlsx"using(Watermarkerwatermarker=newWatermarker("spreadsheet.xlsx",loadOptions)){TextWatermarkwatermark=newTextWatermark("Test watermark",newFont("Segoe UI",19));SpreadsheetWatermarkShapeOptionsoptions=newSpreadsheetWatermarkShapeOptions();// Set the shape nameoptions.Name="Shape 1";// Set the descriptive (alternative) text that will be associated with the shapeoptions.AlternativeText="Test watermark";// Editing of the shape in Excel is forbiddenoptions.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.
SpreadsheetLoadOptionsloadOptions=newSpreadsheetLoadOptions();// Specify an absolute or relative path to your document. Ex: @"C:\Docs\spreadsheet.xlsx"using(Watermarkerwatermarker=newWatermarker("spreadsheet.xlsx",loadOptions)){TextWatermarkwatermark=newTextWatermark("Test watermark",newFont("Segoe UI",19));SpreadsheetTextEffectseffects=newSpreadsheetTextEffects();effects.LineFormat.Enabled=true;effects.LineFormat.Color=Color.Red;effects.LineFormat.DashStyle=OfficeDashStyle.DashDotDot;effects.LineFormat.LineStyle=OfficeLineStyle.Triple;effects.LineFormat.Weight=1;SpreadsheetWatermarkShapeOptionsoptions=newSpreadsheetWatermarkShapeOptions();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.
SpreadsheetLoadOptionsloadOptions=newSpreadsheetLoadOptions();// Specify an absolute or relative path to your document. Ex: @"C:\Docs\spreadsheet.xlsx"using(Watermarkerwatermarker=newWatermarker("spreadsheet.xlsx",loadOptions)){using(ImageWatermarkwatermark=newImageWatermark("logo.png")){SpreadsheetImageEffectseffects=newSpreadsheetImageEffects();effects.Brightness=0.7;effects.Contrast=0.6;effects.ChromaKey=Color.Red;effects.BorderLineFormat.Enabled=true;effects.BorderLineFormat.Weight=1;SpreadsheetWatermarkShapeOptionsoptions=newSpreadsheetWatermarkShapeOptions();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.
SpreadsheetLoadOptionsloadOptions=newSpreadsheetLoadOptions();// Specify an absolute or relative path to your document. Ex: @"C:\Docs\spreadsheet.xlsx"using(Watermarkerwatermarker=newWatermarker("spreadsheet.xlsx",loadOptions)){using(ImageWatermarkwatermark=newImageWatermark("logo.gif")){SpreadsheetBackgroundWatermarkOptionsoptions=newSpreadsheetBackgroundWatermarkOptions();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.
SpreadsheetLoadOptionsloadOptions=newSpreadsheetLoadOptions();// Specify an absolute or relative path to your document. Ex: @"C:\Docs\spreadsheet.xlsx"using(Watermarkerwatermarker=newWatermarker("spreadsheet.xlsx",loadOptions)){using(ImageWatermarkwatermark=newImageWatermark("logo.gif")){watermark.HorizontalAlignment=HorizontalAlignment.Center;watermark.VerticalAlignment=VerticalAlignment.Center;watermark.RotateAngle=90;watermark.SizingType=SizingType.ScaleToParentDimensions;watermark.ScaleFactor=0.5;SpreadsheetContentcontent=watermarker.GetContent<SpreadsheetContent>();SpreadsheetBackgroundWatermarkOptionsoptions=newSpreadsheetBackgroundWatermarkOptions();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).
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.
SpreadsheetLoadOptionsloadOptions=newSpreadsheetLoadOptions();// Specify an absolute or relative path to your document. Ex: @"C:\Docs\spreadsheet.xlsx"using(Watermarkerwatermarker=newWatermarker("spreadsheet.xlsx",loadOptions)){TextWatermarkwatermark=newTextWatermark("Test watermark",newFont("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;SpreadsheetContentcontent=watermarker.GetContent<SpreadsheetContent>();SpreadsheetBackgroundWatermarkOptionsoptions=newSpreadsheetBackgroundWatermarkOptions();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");}
Header and footer image watermark
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.
SpreadsheetLoadOptionsloadOptions=newSpreadsheetLoadOptions();// Specify an absolute or relative path to your document. Ex: @"C:\Docs\spreadsheet.xlsx"using(Watermarkerwatermarker=newWatermarker("spreadsheet.xlsx",loadOptions)){using(ImageWatermarkwatermark=newImageWatermark("logo.png")){watermark.VerticalAlignment=VerticalAlignment.Top;watermark.HorizontalAlignment=HorizontalAlignment.Center;watermark.SizingType=SizingType.ScaleToParentDimensions;watermark.ScaleFactor=1;SpreadsheetWatermarkHeaderFooterOptionsoptions=newSpreadsheetWatermarkHeaderFooterOptions();options.WorksheetIndex=0;watermarker.Add(watermark,options);}watermarker.Save("spreadsheet.xlsx");}
Header and footer text watermark
You can also add text watermark in header or footer as shown in the below code sample.
SpreadsheetLoadOptionsloadOptions=newSpreadsheetLoadOptions();// Specify an absolute or relative path to your document. Ex: @"C:\Docs\spreadsheet.xlsx"using(Watermarkerwatermarker=newWatermarker("spreadsheet.xlsx",loadOptions)){TextWatermarkwatermark=newTextWatermark("Test watermark",newFont("Segoe UI",19,FontStyle.Bold));watermark.ForegroundColor=Color.Red;watermark.BackgroundColor=Color.Aqua;watermark.VerticalAlignment=VerticalAlignment.Top;watermark.HorizontalAlignment=HorizontalAlignment.Center;SpreadsheetWatermarkHeaderFooterOptionsoptions=newSpreadsheetWatermarkHeaderFooterOptions();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.