Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Add Watermark to Excel Spreadsheets in Java
/*
* Add watermark to all the sheets of the Excel Workbook in Java
*/
// Load the spreadsheet
String filename = "path/spreadsheet.xlsx";
Watermarker watermarker = new Watermarker(filename, new SpreadsheetLoadOptions());
// Set text watermark appearance
TextWatermark watermark = new TextWatermark("DRAFT", 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);
// Add watermark and save the spreadsheet with watermark
watermarker.add(watermark);
watermarker.save("path/watermark-all-spreadsheet.xlsx");
watermarker.close();
/*
* Add watermark as background to Excel Workbook in Java
*/
// Load the spreadsheet
String filename = "path/spreadsheet.xlsx";
Watermarker watermarker = new Watermarker(filename, new SpreadsheetLoadOptions());
// Set text watermark appearance
TextWatermark watermark = new TextWatermark("DRAFT", 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);
// Add watermark to the background
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 */
// Save the spreadsheet with watermark
watermarker.add(watermark, options);
watermarker.save("path/watermark-background-spreadsheet.xlsx");
watermarker.close();
/*
* Add watermark only to the mentioned sheet of the Excel Workbook using Java
*/
// Load the spreadsheet
String filename = "path/spreadsheet.xlsx";
Watermarker watermarker = new Watermarker(filename, new SpreadsheetLoadOptions());
// Set text watermark and its worksheet index
TextWatermark watermark = new TextWatermark("DRAFT", new Font("Segoe UI", 19));
SpreadsheetWatermarkModernWordArtOptions options = new SpreadsheetWatermarkModernWordArtOptions();
options.setWorksheetIndex(0);
// Add watermark and save the spreadsheet with watermark
watermarker.add(watermark, options);
watermarker.save("path/watermark-single-sheet.xlsx");
watermarker.close();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment