Related blog post: How to Watermark Excel Sheets in Java
Last active
November 13, 2021 04:47
-
-
Save GroupDocsGists/a8ff600a6d3b7460f52abe2836bffe0a to your computer and use it in GitHub Desktop.
Add Watermark to Excel Spreadsheets in Java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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