The watermarking API enables you extracting information about all shapes in an excel document, Removing a particular shape, Removing shapes with particular text formatting, Replacing text for particular shapes, Replacing shape image and more.
Extracting information about all shapes in an excel document
Search method searches watermarks of all mentioned types, but in some cases, it’s necessary to analyze only one class of Excel objects. Following code sample shows how to get information about all the shapes in an Excel document.
SpreadsheetLoadOptionsloadOptions=newSpreadsheetLoadOptions();// Specify an absolute or relative path to your document. Ex: @"C:\Docs\spreadsheet.xlsx"using(Watermarkerwatermarker=newWatermarker("spreadsheet.xlsx",loadOptions)){SpreadsheetContentcontent=watermarker.GetContent<SpreadsheetContent>();foreach(SpreadsheetWorksheetworksheetincontent.Worksheets){foreach(SpreadsheetShapeshapeinworksheet.Shapes){Console.WriteLine(shape.AutoShapeType);Console.WriteLine(shape.MsoDrawingType);Console.WriteLine(shape.Text);if(shape.Image!=null){Console.WriteLine(shape.Image.Width);Console.WriteLine(shape.Image.Height);Console.WriteLine(shape.Image.GetBytes().Length);}Console.WriteLine(shape.Id);Console.WriteLine(shape.AlternativeText);Console.WriteLine(shape.X);Console.WriteLine(shape.Y);Console.WriteLine(shape.Width);Console.WriteLine(shape.Height);Console.WriteLine(shape.RotateAngle);Console.WriteLine(shape.IsWordArt);Console.WriteLine(shape.Name);}}}
Removing a particular shape
You can also remove a particular shape from the worksheet as shown in the below code sample.
SpreadsheetLoadOptionsloadOptions=newSpreadsheetLoadOptions();// Specify an absolute or relative path to your document. Ex: @"C:\Docs\spreadsheet.xlsx"using(Watermarkerwatermarker=newWatermarker("spreadsheet.xlsx",loadOptions)){SpreadsheetContentcontent=watermarker.GetContent<SpreadsheetContent>();// Remove shape by indexcontent.Worksheets[0].Shapes.RemoveAt(0);// Remove shape by referencecontent.Worksheets[0].Shapes.Remove(content.Worksheets[0].Shapes[0]);watermarker.Save("spreadsheet.xlsx");}
Removing shapes with particular text formatting
You can also find and remove the shapes with a particular text formatting from 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"using(Watermarkerwatermarker=newWatermarker("spreadsheet.xlsx",loadOptions)){SpreadsheetContentcontent=watermarker.GetContent<SpreadsheetContent>();foreach(SpreadsheetWorksheetsectionincontent.Worksheets){for(inti=section.Shapes.Count-1;i>=0;i--){foreach(FormattedTextFragmentfragmentinsection.Shapes[i].FormattedTextFragments){if(fragment.ForegroundColor==Color.Red&&fragment.Font.FamilyName=="Arial"){section.Shapes.RemoveAt(i);break;}}}}watermarker.Save("spreadsheet.xlsx");}
Removing/replacing hyperlink associated with a particular shape
Using GroupDocs.Watermark for .NET, you can also remove/replace hyperlink associated with a particular shape or chart inside an Excel document. Use following code sample to achieve this functionality.
SpreadsheetLoadOptionsloadOptions=newSpreadsheetLoadOptions();// Specify an absolute or relative path to your document. Ex: @"C:\Docs\spreadsheet.xlsx"using(Watermarkerwatermarker=newWatermarker("spreadsheet.xlsx",loadOptions)){SpreadsheetContentcontent=watermarker.GetContent<SpreadsheetContent>();// Replace hyperlinkcontent.Worksheets[0].Charts[0].Hyperlink="https://www.aspose.com/";content.Worksheets[0].Shapes[0].Hyperlink="https://www.groupdocs.com/";// Remove hyperlinkcontent.Worksheets[1].Charts[0].Hyperlink=null;content.Worksheets[1].Shapes[0].Hyperlink=null;watermarker.Save("spreadsheet.xlsx");}
Warning
This feature is also supported for charts.
Replacing text for particular shapes
Since version 17.12, GroupDocs.Watermark supports replacing text for particular shapes in an Excel Worksheet. Following code sample shows the usage of this feature.
GroupDocs.Watermark also allows you to replace the image of the particular shapes in an Excel Worksheet as shown in the following code sample. This feature is supported since version 17.12.
SpreadsheetLoadOptionsloadOptions=newSpreadsheetLoadOptions();// Specify an absolute or relative path to your document. Ex: @"C:\Docs\spreadsheet.xlsx"using(Watermarkerwatermarker=newWatermarker("spreadsheet.xlsx",loadOptions)){SpreadsheetContentcontent=watermarker.GetContent<SpreadsheetContent>();foreach(SpreadsheetShapeshapeincontent.Worksheets[0].Shapes){if(shape.Image!=null){shape.Image=newSpreadsheetWatermarkableImage(File.ReadAllBytes("test.png"));}}watermarker.Save("spreadsheet.xlsx");}
Setting background image for particular shapes
Since version 17.12, GroupDocs.Watermark enables you to set the background image for the particular shapes in an Excel Worksheet. Following code sample shows the usage of this feature.
Since version 17.12, GroupDocs.Watermark also provides the feature of modifying properties (X, Y, Width, Height, RotateAngle or AlternativeText) of particular shapes in an Excel Worksheet. Following code sample shows how to use this feature.