Working with worksheet backgrounds

Extracting information about all worksheet backgrounds in an excel document

The API allows you to extract information about all the worksheet backgrounds in an Excel document as shown in the following code sample.

AdvancedUsage.AddingWatermarks.AddWatermarksToSpreadsheets.SpreadsheetGetInformationOfWorksheetBackgrounds

SpreadsheetLoadOptions loadOptions = new SpreadsheetLoadOptions();
// Specify an absolute or relative path to your document. Ex: @"C:\Docs\spreadsheet.xlsx"
using (Watermarker watermarker = new Watermarker("spreadsheet.xlsx", loadOptions))
{
    SpreadsheetContent content = watermarker.GetContent<SpreadsheetContent>();
    foreach (SpreadsheetWorksheet worksheet in content.Worksheets)
    {
        if (worksheet.BackgroundImage != null)
        {
            Console.WriteLine(worksheet.BackgroundImage.Width);
            Console.WriteLine(worksheet.BackgroundImage.Height);
            Console.WriteLine(worksheet.BackgroundImage.GetBytes().Length);
        }
    }
}

Removing a particular background

Following code sample can be used to remove the background of a particular worksheet.

AdvancedUsage.AddingWatermarks.AddWatermarksToSpreadsheets.SpreadsheetRemoveWorksheetBackground

SpreadsheetLoadOptions loadOptions = new SpreadsheetLoadOptions();
// Specify an absolute or relative path to your document. Ex: @"C:\Docs\spreadsheet.xlsx"
using (Watermarker watermarker = new Watermarker("spreadsheet.xlsx", loadOptions))
{
    SpreadsheetContent content = watermarker.GetContent<SpreadsheetContent>();
    content.Worksheets[0].BackgroundImage = null;

    watermarker.Save("spreadsheet.xlsx");
}

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.

AdvancedUsage.AddingWatermarks.AddWatermarksToSpreadsheets.SpreadsheetAddWatermarkToBackgroundImages

SpreadsheetLoadOptions loadOptions = new SpreadsheetLoadOptions();
// Specify an absolute or relative path to your document. Ex: @"C:\Docs\spreadsheet.xlsx"
using (Watermarker watermarker = new Watermarker("spreadsheet.xlsx", loadOptions))
{
    // Initialize image or text watermark
    TextWatermark watermark = new TextWatermark("Protected image", new Font("Arial", 8));
    watermark.HorizontalAlignment = HorizontalAlignment.Center;
    watermark.VerticalAlignment = VerticalAlignment.Center;
    watermark.RotateAngle = 45;
    watermark.SizingType = SizingType.ScaleToParentDimensions;
    watermark.ScaleFactor = 1;

    SpreadsheetContent content = watermarker.GetContent<SpreadsheetContent>();
    foreach (SpreadsheetWorksheet worksheet in content.Worksheets)
    {
        if (worksheet.BackgroundImage != null)
        {
            // Add watermark to the image
            worksheet.BackgroundImage.Add(watermark);
        }
    }

    watermarker.Save("spreadsheet.xlsx");
}

Settings background image for charts

GroupDocs.Watermark for .NET also allows you to set the background image for a chart inside an Excel document. Following code sample shows how to achieve this functionality.

AdvancedUsage.AddingWatermarks.AddWatermarksToSpreadsheets.SpreadsheetSetBackgroundImageForChart

SpreadsheetLoadOptions loadOptions = new SpreadsheetLoadOptions();
// Specify an absolute or relative path to your document. Ex: @"C:\Docs\spreadsheet.xlsx"
using (Watermarker watermarker = new Watermarker("spreadsheet.xlsx", loadOptions))
{
    SpreadsheetContent content = watermarker.GetContent<SpreadsheetContent>();
    content.Worksheets[0].Charts[0].ImageFillFormat.BackgroundImage = 
        new SpreadsheetWatermarkableImage(File.ReadAllBytes("test.png"));
    content.Worksheets[0].Charts[0].ImageFillFormat.Transparency = 0.5;
    content.Worksheets[0].Charts[0].ImageFillFormat.TileAsTexture = true;

    watermarker.Save("spreadsheet.xlsx");
}