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);for(SpreadsheetWorksheetworksheet:content.getWorksheets()){if(worksheet.getBackgroundImage()!=null){System.out.println(worksheet.getBackgroundImage().getWidth());System.out.println(worksheet.getBackgroundImage().getHeight());System.out.println(worksheet.getBackgroundImage().getBytes().length);}}watermarker.close();
Removing a particular background
Following code sample can be used to remove the background of a particular worksheet.
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);content.getWorksheets().get_Item(0).setBackgroundImage(null);watermarker.save("spreadsheet.xlsx");watermarker.close();
Adding watermark to all backgrounds in an Excel worksheet
You can add watermark to the background images that belong to an Excel document 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);// Initialize image or text watermark
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);SpreadsheetContentcontent=watermarker.getContent(SpreadsheetContent.class);for(SpreadsheetWorksheetworksheet:content.getWorksheets()){if(worksheet.getBackgroundImage()!=null){// Add watermark to the image
worksheet.getBackgroundImage().add(watermark);}}watermarker.save("spreadsheet.xlsx");watermarker.close();
Settings background image for charts
GroupDocs.Watermark for Java also allows you to set the background image for a chart inside an Excel document. Following code sample shows how to achieve this functionality.
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);FileimageFile=newFile("test.png");byte[]imageBytes=newbyte[(int)imageFile.length()];InputStreamimageInputStream=newFileInputStream(imageFile);imageInputStream.read(imageBytes);imageInputStream.close();content.getWorksheets().get_Item(0).getCharts().get_Item(0).getImageFillFormat().setBackgroundImage(newSpreadsheetWatermarkableImage(imageBytes));content.getWorksheets().get_Item(0).getCharts().get_Item(0).getImageFillFormat().setTransparency(0.5);content.getWorksheets().get_Item(0).getCharts().get_Item(0).getImageFillFormat().setTileAsTexture(true);watermarker.save("spreadsheet.xlsx");watermarker.close();
Was this page helpful?
Any additional feedback you'd like to share with us?
Please tell us how we can improve this page.
Thank you for your feedback!
We value your opinion. Your feedback will help us improve our documentation.