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"
Watermarkerwatermarker=newWatermarker("spreadsheet.xlsx",loadOptions);// Add text watermark to the first worksheet
TextWatermarktextWatermark=newTextWatermark("Test watermark",newFont("Arial",8));SpreadsheetWatermarkShapeOptionstextWatermarkOptions=newSpreadsheetWatermarkShapeOptions();textWatermarkOptions.setWorksheetIndex(0);watermarker.add(textWatermark,textWatermarkOptions);// Add image watermark to the second worksheet
ImageWatermarkimageWatermark=newImageWatermark("logo.jpg");SpreadsheetWatermarkShapeOptionsimageWatermarkOptions=newSpreadsheetWatermarkShapeOptions();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).
SpreadsheetLoadOptionsloadOptions=newSpreadsheetLoadOptions();// Specify an absolute or relative path to your document. Ex: "C:\\Docs\\spreadsheet.xlsx"
Watermarkerwatermarker=newWatermarker("spreadsheet.xlsx",loadOptions);SpreadsheetContentcontent=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.
SpreadsheetLoadOptionsloadOptions=newSpreadsheetLoadOptions();// Specify an absolute or relative path to your document. Ex: "C:\\Docs\\spreadsheet.xlsx"
Watermarkerwatermarker=newWatermarker("spreadsheet.xlsx",loadOptions);TextWatermarkwatermark=newTextWatermark("Protected image",newFont("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
SpreadsheetContentcontent=watermarker.getContent(SpreadsheetContent.class);WatermarkableImageCollectionimages=content.getWorksheets().get_Item(0).findImages();// Add watermark to all found images
for(WatermarkableImageimage:images){image.add(watermark);}watermarker.save("spreadsheet.xlsx");watermarker.close();
SpreadsheetLoadOptionsloadOptions=newSpreadsheetLoadOptions();// Specify an absolute or relative path to your document. Ex: "C:\\Docs\\spreadsheet.xlsx"
Watermarkerwatermarker=newWatermarker("spreadsheet.xlsx",loadOptions);TextWatermarktextWatermark=newTextWatermark("Test watermark",newFont("Arial",8));SpreadsheetWatermarkModernWordArtOptionsoptions=newSpreadsheetWatermarkModernWordArtOptions();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).
SpreadsheetLoadOptionsloadOptions=newSpreadsheetLoadOptions();// Specify an absolute or relative path to your document. Ex: "C:\\Docs\\spreadsheet.xlsx"
Watermarkerwatermarker=newWatermarker("spreadsheet.xlsx",loadOptions);TextWatermarkwatermark=newTextWatermark("Test watermark",newFont("Segoe UI",19));SpreadsheetWatermarkShapeOptionsoptions=newSpreadsheetWatermarkShapeOptions();// 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.
SpreadsheetLoadOptionsloadOptions=newSpreadsheetLoadOptions();// Specify an absolute or relative path to your document. Ex: "C:\\Docs\\spreadsheet.xlsx"
Watermarkerwatermarker=newWatermarker("spreadsheet.xlsx",loadOptions);TextWatermarkwatermark=newTextWatermark("Test watermark",newFont("Segoe UI",19));SpreadsheetTextEffectseffects=newSpreadsheetTextEffects();effects.getLineFormat().setEnabled(true);effects.getLineFormat().setColor(Color.getRed());effects.getLineFormat().setDashStyle(OfficeDashStyle.DashDotDot);effects.getLineFormat().setLineStyle(OfficeLineStyle.Triple);effects.getLineFormat().setWeight(1);SpreadsheetWatermarkShapeOptionsoptions=newSpreadsheetWatermarkShapeOptions();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.
SpreadsheetLoadOptionsloadOptions=newSpreadsheetLoadOptions();// Specify an absolute or relative path to your document. Ex: "C:\\Docs\\spreadsheet.xlsx"
Watermarkerwatermarker=newWatermarker("spreadsheet.xlsx",loadOptions);ImageWatermarkwatermark=newImageWatermark("logo.png");SpreadsheetImageEffectseffects=newSpreadsheetImageEffects();effects.setBrightness(0.7);effects.setContrast(0.6);effects.setChromaKey(Color.getRed());effects.getBorderLineFormat().setEnabled(true);effects.getBorderLineFormat().setWeight(1);SpreadsheetWatermarkShapeOptionsoptions=newSpreadsheetWatermarkShapeOptions();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.
SpreadsheetLoadOptionsloadOptions=newSpreadsheetLoadOptions();// Specify an absolute or relative path to your document. Ex: "C:\\Docs\\spreadsheet.xlsx"
Watermarkerwatermarker=newWatermarker("spreadsheet.xlsx",loadOptions);ImageWatermarkwatermark=newImageWatermark("logo.gif");SpreadsheetBackgroundWatermarkOptionsoptions=newSpreadsheetBackgroundWatermarkOptions();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.
SpreadsheetLoadOptionsloadOptions=newSpreadsheetLoadOptions();// Specify an absolute or relative path to your document. Ex: "C:\\Docs\\spreadsheet.xlsx"
Watermarkerwatermarker=newWatermarker("spreadsheet.xlsx",loadOptions);ImageWatermarkwatermark=newImageWatermark("logo.gif");watermark.setHorizontalAlignment(HorizontalAlignment.Center);watermark.setVerticalAlignment(VerticalAlignment.Center);watermark.setRotateAngle(90);watermark.setSizingType(SizingType.ScaleToParentDimensions);watermark.setScaleFactor(0.5);SpreadsheetContentcontent=watermarker.getContent(SpreadsheetContent.class);SpreadsheetBackgroundWatermarkOptionsoptions=newSpreadsheetBackgroundWatermarkOptions();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).
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"
Watermarkerwatermarker=newWatermarker("spreadsheet.xlsx",loadOptions);TextWatermarkwatermark=newTextWatermark("Test watermark",newFont("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);SpreadsheetContentcontent=watermarker.getContent(SpreadsheetContent.class);SpreadsheetBackgroundWatermarkOptionsoptions=newSpreadsheetBackgroundWatermarkOptions();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();
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"
Watermarkerwatermarker=newWatermarker("spreadsheet.xlsx",loadOptions);ImageWatermarkwatermark=newImageWatermark("logo.png");watermark.setVerticalAlignment(VerticalAlignment.Top);watermark.setHorizontalAlignment(HorizontalAlignment.Center);watermark.setSizingType(SizingType.ScaleToParentDimensions);watermark.setScaleFactor(1);SpreadsheetWatermarkHeaderFooterOptionsoptions=newSpreadsheetWatermarkHeaderFooterOptions();options.setWorksheetIndex(0);watermarker.add(watermark,options);watermarker.save("spreadsheet.xlsx");watermarker.close();
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"
Watermarkerwatermarker=newWatermarker("spreadsheet.xlsx",loadOptions);TextWatermarkwatermark=newTextWatermark("Test watermark",newFont("Segoe UI",19,FontStyle.Bold));watermark.setForegroundColor(Color.getRed());watermark.setBackgroundColor(Color.getAqua());watermark.setVerticalAlignment(VerticalAlignment.Top);watermark.setHorizontalAlignment(HorizontalAlignment.Center);SpreadsheetWatermarkHeaderFooterOptionsoptions=newSpreadsheetWatermarkHeaderFooterOptions();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.