C#: Add a Watermark to an Excel Document

Adding a watermark to an Excel spreadsheet can be a useful way to brand your documents or indicate confidentiality. MS Excel does not provide a built-in feature to insert a watermark, however, there are ways to mimic the watermark effect by inserting an image into the header or footer of your Excel worksheet, or setting an image as the background of your spreadsheet.

In this article, you will learn how to add a header or background image watermark to Excel in C# using Spire.XLS for .NET.

Install Spire.XLS for .NET

To begin with, you need to add the DLL files included in the Spire.XLS for .NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.XLS

Header vs. Background Image Watermark

Header Image Watermark

Advantages:

  • The watermark is preserved on the printed sheet, ensuring it appears in the final output.

Disadvantages:

  • The watermark is invisible under the "Normal" view mode in Excel, only becoming visible in "Page Layout" or "Page Break Preview" views.
  • To centrally position the watermark graphic on the Excel page, you need to carefully adjust the white margins, especially on the top and left sides of the image.

Background Image Watermark

Advantages:

  • The watermark image covers the entire worksheet area, providing a consistent background appearance.

Disadvantages:

  • The watermark is not preserved on the printed sheet, meaning it will not appear in the final printed output.

Add a Watermark to Excel Using a Header Image in C#

Spire.XLS for .NET provides the PageSetup class, which allows you to control various settings related to the appearance and layout of the printed worksheet. This class includes the CenterHeader and CenterHeaderImage properties, which enable you to set an image for the center section of the header.

Below are the steps to add a watermark to Excel using a header image in C#.

  • Create a Workbook object.
  • Load an Excel document from a give file path.
  • Load an image using Image.FromFile() method.
  • Get a specific worksheet from the workbook.
  • Add an image field to the header center by setting Worksheet.PageSetup.CenterHeader property to "&G".
  • Apply the image to the header center through Worksheet.PageSetup.CenterHeaderImage property.
  • Save the workbook to a different Excel file.
  • C#
using Spire.Xls;
using System.Drawing;

namespace AddWatermarkToExcelUsingHeader
{
    class Program
    {
        static void Main(string[] args)
        {
            // Create a Workbook object
            Workbook workbook = new Workbook();

            // Load an Excel document
            workbook.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Input.xlsx");

            // Load an image file
            Image image = Image.FromFile("C:\\Users\\Administrator\\Desktop\\confidential.png");

            // Loop through all worksheets in the file
            for (int i = 0; i < workbook.Worksheets.Count; i++)
            {
                // Get a specific worksheet
                Worksheet worksheet = workbook.Worksheets[i];

                // Add an image field to the header center
                worksheet.PageSetup.CenterHeader = "&G";

                // Add the image to the header center
                worksheet.PageSetup.CenterHeaderImage = image;
            }

            // Save the result file
            workbook.SaveToFile("AddWatermark.xlsx", ExcelVersion.Version2016);

            // Dispose resources
            workbook.Dispose();
        }
    }
}

C#: Add a Watermark to an Excel Document

Add a Watermark to Excel Using a Background Image in C#

The PageSetup class also includes a property named BackgroundImage, which allows you to get or set the image used for the worksheet background.

Here are the steps to add a watermark to Excel using a background image in C#.

  • Create a Workbook object.
  • Load an Excel document from a give file path.
  • Load an image file and convert it into a Bitmap image.
  • Get a specific worksheet from the workbook.
  • Apply the image to the worksheet as the background through Worksheet.PageSetup.BackgroundImage property.
  • Save the workbook to a different Excel file.
  • C#
using Spire.Xls;
using System.Drawing;

namespace AddWatermarkToExcelUsingBackground
{
    class Program
    {
        static void Main(string[] args)
        {
            // Create a Workbook object
            Workbook workbook = new Workbook();

            // Load an Excel document
            workbook.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Input.xlsx");

            // Load an image file
            Bitmap bitmapImage = new Bitmap(Image.FromFile("C:\\Users\\Administrator\\Desktop\\sample.png"));

            // Loop through all worksheets in the file
            for (int i = 0; i < workbook.Worksheets.Count; i++)
            {
                // Get a specific worksheet
                Worksheet worksheet = workbook.Worksheets[i];

                // Set the image as the background of the worksheet
                worksheet.PageSetup.BackgoundImage = bitmapImage;
            }

            // Save the result file
            workbook.SaveToFile("AddWatermark.xlsx", ExcelVersion.Version2016);

            // Dispose resources
            workbook.Dispose();
        }
    }
}

C#: Add a Watermark to an Excel Document

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.