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 call setWorksheetIndex()
  5. Add watermark to the worksheet
  6. Save the document

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

advanced_usage.add_watermarks_to_spreadsheets.SpreadsheetAddWatermarkToWorksheet

SpreadsheetLoadOptions loadOptions = new SpreadsheetLoadOptions();                                               
// Specify an absolute or relative path to your document. Ex: "C:\\Docs\\spreadsheet.xlsx"
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.setWorksheetIndex(0);                                                                       
watermarker.add(textWatermark, textWatermarkOptions);                                                            
                                                                                                                 
// Add image watermark to the second worksheet                                                                   
ImageWatermark imageWatermark = new ImageWatermark("logo.jpg");                                           
                                                                                                                 
SpreadsheetWatermarkShapeOptions imageWatermarkOptions = new SpreadsheetWatermarkShapeOptions();                 
imageWatermarkOptions.setWorksheetIndex(1);                                                                      
watermarker.add(imageWatermark, imageWatermarkOptions);                                                          
                                                                                                                 
watermarker.save("spreadsheet.xlsx");                                                                  
                                                                                                                 
watermarker.close();                                                                                             
imageWatermark.close();                                                                                          

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).

advanced_usage.add_watermarks_to_spreadsheets.SpreadsheetGetContentAreaDimensions

SpreadsheetLoadOptions loadOptions = new SpreadsheetLoadOptions();                                               
// Specify an absolute or relative path to your document. Ex: "C:\\Docs\\spreadsheet.xlsx"
Watermarker watermarker = new Watermarker("spreadsheet.xlsx", loadOptions);                             
                                                                                                                 
SpreadsheetContent content = watermarker.getContent(SpreadsheetContent.class);                                   
                                                                                                                 
// Get the size of content area                                                                                  
System.out.println(content.getWorksheets().get_Item(0).getContentAreaHeight());                                  
System.out.println(content.getWorksheets().get_Item(0).getContentAreaWidth());                                   
                                                                                                                 
// Get the size of particular cell                                                                               
System.out.println(content.getWorksheets().get_Item(0).getColumnWidth(0));                                       
System.out.println(content.getWorksheets().get_Item(0).getRowHeight(0));                                         
                                                                                                                 
watermarker.close();                                                                                             

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.

advanced_usage.add_watermarks_to_spreadsheets.SpreadsheetAddWatermarkToWorksheetImages

SpreadsheetLoadOptions loadOptions = new SpreadsheetLoadOptions();                                               
// Specify an absolute or relative path to your document. Ex: "C:\\Docs\\spreadsheet.xlsx"
Watermarker watermarker = new Watermarker("spreadsheet.xlsx", loadOptions);                             
                                                                                                                 
TextWatermark watermark = new TextWatermark("Protected image", new Font("Arial", 8));                            
watermark.setHorizontalAlignment(HorizontalAlignment.Center);                                                    
watermark.setVerticalAlignment(VerticalAlignment.Center);                                                        
watermark.setRotateAngle(45);                                                                                    
watermark.setSizingType(SizingType.ScaleToParentDimensions);                                                     
watermark.setScaleFactor(1);                                                                                     
                                                                                                                 
// Get all images from the first worksheet                                                                       
SpreadsheetContent content = watermarker.getContent(SpreadsheetContent.class);                                   
WatermarkableImageCollection images = content.getWorksheets().get_Item(0).findImages();                          
                                                                                                                 
// Add watermark to all found images                                                                             
for (WatermarkableImage image : images)                                                                          
{                                                                                                                
    image.add(watermark);                                                                                        
}                                                                                                                
                                                                                                                 
watermarker.save("spreadsheet.xlsx");                                                                  
                                                                                                                 
watermarker.close();                                                                                             

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.

advanced_usage.add_watermarks_to_spreadsheets.SpreadsheetAddModernWordArdWatermark

SpreadsheetLoadOptions loadOptions = new SpreadsheetLoadOptions();                                               
// Specify an absolute or relative path to your document. Ex: "C:\\Docs\\spreadsheet.xlsx"
Watermarker watermarker = new Watermarker("spreadsheet.xlsx", loadOptions);                             
                                                                                                                 
TextWatermark textWatermark = new TextWatermark("Test watermark", new Font("Arial", 8));                         
SpreadsheetWatermarkModernWordArtOptions options = new SpreadsheetWatermarkModernWordArtOptions();               
options.setWorksheetIndex(0);                                                                                    
                                                                                                                 
watermarker.add(textWatermark, options);                                                                         
watermarker.save("spreadsheet.xlsx");                                                                  
                                                                                                                 
watermarker.close();                                                                                             

Shape additional options

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

advanced_usage.add_watermarks_to_spreadsheets.SpreadsheetAddWatermarkUsingShapeSettings

SpreadsheetLoadOptions loadOptions = new SpreadsheetLoadOptions();                                               
// Specify an absolute or relative path to your document. Ex: "C:\\Docs\\spreadsheet.xlsx"
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.setName("Shape 1");                                                                                      
                                                                                                                 
// Set the descriptive (alternative) text that will be associated with the shape                                 
options.setAlternativeText("Test watermark");                                                                    
                                                                                                                 
// Editing of the shape in Excel is forbidden                                                                    
options.setLocked(true);                                                                                         
                                                                                                                 
watermarker.add(watermark, options);                                                                             
watermarker.save("spreadsheet.xlsx");                                                                  
                                                                                                                 
watermarker.close();                                                                                             

Text effects

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

advanced_usage.add_watermarks_to_spreadsheets.SpreadsheetAddWatermarkWithTextEffects

SpreadsheetLoadOptions loadOptions = new SpreadsheetLoadOptions();                                               
// Specify an absolute or relative path to your document. Ex: "C:\\Docs\\spreadsheet.xlsx"
Watermarker watermarker = new Watermarker("spreadsheet.xlsx", loadOptions);                             
                                                                                                                 
TextWatermark watermark = new TextWatermark("Test watermark", new Font("Segoe UI", 19));                         
                                                                                                                 
SpreadsheetTextEffects effects = new SpreadsheetTextEffects();                                                   
effects.getLineFormat().setEnabled(true);                                                                        
effects.getLineFormat().setColor(Color.getRed());                                                                
effects.getLineFormat().setDashStyle(OfficeDashStyle.DashDotDot);                                                
effects.getLineFormat().setLineStyle(OfficeLineStyle.Triple);                                                    
effects.getLineFormat().setWeight(1);                                                                            
                                                                                                                 
SpreadsheetWatermarkShapeOptions options = new SpreadsheetWatermarkShapeOptions();                               
options.setEffects(effects);                                                                                     
                                                                                                                 
watermarker.add(watermark, options);                                                                             
watermarker.save("spreadsheet.xlsx");                                                                  
                                                                                                                 
watermarker.close();                                                                                             

Image effects

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

advanced_usage.add_watermarks_to_spreadsheets.SpreadsheetAddWatermarkWithImageEffects

SpreadsheetLoadOptions loadOptions = new SpreadsheetLoadOptions();                                               
// Specify an absolute or relative path to your document. Ex: "C:\\Docs\\spreadsheet.xlsx"
Watermarker watermarker = new Watermarker("spreadsheet.xlsx", loadOptions);                             
                                                                                                                 
ImageWatermark watermark = new ImageWatermark("logo.png");                                                
                                                                                                                 
SpreadsheetImageEffects effects = new SpreadsheetImageEffects();                                                 
effects.setBrightness(0.7);                                                                                      
effects.setContrast(0.6);                                                                                        
effects.setChromaKey(Color.getRed());                                                                            
effects.getBorderLineFormat().setEnabled(true);                                                                  
effects.getBorderLineFormat().setWeight(1);                                                                      
                                                                                                                 
SpreadsheetWatermarkShapeOptions options = new SpreadsheetWatermarkShapeOptions();                               
options.setEffects(effects);                                                                                     
                                                                                                                 
watermarker.add(watermark, options);                                                                             
                                                                                                                 
watermarker.save("spreadsheet.xlsx");                                                                  
                                                                                                                 
watermarker.close();                                                                                             

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.

advanced_usage.add_watermarks_to_spreadsheets.SpreadsheetAddWatermarkAsBackground

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

advanced_usage.add_watermarks_to_spreadsheets.SpreadsheetAddWatermarkAsBackgroundWithRelativeSizeAndPosition

SpreadsheetLoadOptions loadOptions = new SpreadsheetLoadOptions();                                                    
// Specify an absolute or relative path to your document. Ex: "C:\\Docs\\spreadsheet.xlsx"     
Watermarker watermarker = new Watermarker("spreadsheet.xlsx", loadOptions);                                  
                                                                                                                      
ImageWatermark watermark = new ImageWatermark("logo.gif");                                                     
                                                                                                                      
watermark.setHorizontalAlignment(HorizontalAlignment.Center);                                                         
watermark.setVerticalAlignment(VerticalAlignment.Center);                                                             
watermark.setRotateAngle(90);                                                                                         
watermark.setSizingType(SizingType.ScaleToParentDimensions);                                                          
watermark.setScaleFactor(0.5);                                                                                        
                                                                                                                      
SpreadsheetContent content = watermarker.getContent(SpreadsheetContent.class);                                        
SpreadsheetBackgroundWatermarkOptions options = new SpreadsheetBackgroundWatermarkOptions();                          
options.setBackgroundWidth(content.getWorksheets().get_Item(0).getContentAreaWidthPx()); /* set background width */   
options.setBackgroundHeight(content.getWorksheets().get_Item(0).getContentAreaHeightPx()); /* set background height */
watermarker.add(watermark, options);                                                                                  
                                                                                                                      
watermarker.save("spreadsheet.xlsx");                                                                       
                                                                                                                      
watermarker.close();                                                                                                  
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.

advanced_usage.add_watermarks_to_spreadsheets.SpreadsheetAddTextWatermarkAsBackground

SpreadsheetLoadOptions loadOptions = new SpreadsheetLoadOptions();                                                     
// Specify an absolute or relative path to your document. Ex: "C:\\Docs\\spreadsheet.xlsx"      
Watermarker watermarker = new Watermarker("spreadsheet.xlsx", loadOptions);                                   
                                                                                                                       
TextWatermark watermark = new TextWatermark("Test watermark", new Font("Segoe UI", 19));                               
watermark.setHorizontalAlignment(HorizontalAlignment.Center);                                                          
watermark.setVerticalAlignment(VerticalAlignment.Center);                                                              
watermark.setRotateAngle(45);                                                                                          
watermark.setSizingType(SizingType.ScaleToParentDimensions);                                                           
watermark.setScaleFactor(0.5);                                                                                         
watermark.setOpacity(0.5);                                                                                             
                                                                                                                       
SpreadsheetContent content = watermarker.getContent(SpreadsheetContent.class);                                         
SpreadsheetBackgroundWatermarkOptions options = new SpreadsheetBackgroundWatermarkOptions();                           
options.setBackgroundWidth(content.getWorksheets().get_Item(0).getContentAreaWidthPx()); /* set background width */    
options.setBackgroundHeight(content.getWorksheets().get_Item(0).getContentAreaHeightPx()); /* set background height */ 
watermarker.add(watermark, options);                                                                                   
                                                                                                                       
watermarker.save("spreadsheet.xlsx");                                                                        
                                                                                                                       
watermarker.close();                                                                                                   

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.

advanced_usage.add_watermarks_to_spreadsheets.SpreadsheetAddImageWatermarkIntoHeaderFooter

SpreadsheetLoadOptions loadOptions = new SpreadsheetLoadOptions();                                               
// Specify an absolute or relative path to your document. Ex: "C:\\Docs\\spreadsheet.xlsx"
Watermarker watermarker = new Watermarker("spreadsheet.xlsx", loadOptions);                             
                                                                                                                 
ImageWatermark watermark = new ImageWatermark("logo.png");                                                
                                                                                                                 
watermark.setVerticalAlignment(VerticalAlignment.Top);                                                           
watermark.setHorizontalAlignment(HorizontalAlignment.Center);                                                    
watermark.setSizingType(SizingType.ScaleToParentDimensions);                                                     
watermark.setScaleFactor(1);                                                                                     
                                                                                                                 
SpreadsheetWatermarkHeaderFooterOptions options = new SpreadsheetWatermarkHeaderFooterOptions();                 
options.setWorksheetIndex(0);                                                                                    
                                                                                                                 
watermarker.add(watermark, options);                                                                             
                                                                                                                 
watermarker.save("spreadsheet.xlsx");                                                                  
                                                                                                                 
watermarker.close();                                                                                             

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

advanced_usage.add_watermarks_to_spreadsheets.SpreadsheetAddTextWatermarkIntoHeaderFooter

SpreadsheetLoadOptions loadOptions = new SpreadsheetLoadOptions();                                               
// Specify an absolute or relative path to your document. Ex: "C:\\Docs\\spreadsheet.xlsx"
Watermarker watermarker = new Watermarker("spreadsheet.xlsx", loadOptions);                             
                                                                                                                 
TextWatermark watermark = new TextWatermark("Test watermark", new Font("Segoe UI", 19, FontStyle.Bold));         
watermark.setForegroundColor(Color.getRed());                                                                    
watermark.setBackgroundColor(Color.getAqua());                                                                   
watermark.setVerticalAlignment(VerticalAlignment.Top);                                                           
watermark.setHorizontalAlignment(HorizontalAlignment.Center);                                                    
                                                                                                                 
SpreadsheetWatermarkHeaderFooterOptions options = new SpreadsheetWatermarkHeaderFooterOptions();                 
options.setWorksheetIndex(0);                                                                                    
                                                                                                                 
watermarker.add(watermark, options);                                                                             
watermarker.save("spreadsheet.xlsx");                                                                  
                                                                                                                 
watermarker.close();                                                                                             

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