C#/VB.NET: Add a Watermark to Excel

Watermarks are often used in electronic documents to protect their copyright or to specify their status. Microsoft Excel does not come with a built-in feature to add watermarks to Excel worksheets, but you can use some ingenious approaches to implement the watermark effect, such as inserting a header image or WordArt. In this article, we will demonstrate how to create and add a header image in Excel to mimic a watermark in C# and VB.NET 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

Add a Watermark to Excel in C# and VB.NET

To create a header image, we defined a custom method called DrawWatermarkImage(). This method enables you to generate a custom image based on a string, such as "Confidential", "Draft", "Internal Use" or any text you want to be shown as the watermark. After the image is generated, you can add it to the header section of an Excel worksheet through the Worksheet.PageSetup.LeftHeaderImage and Worksheet.PageSetup.LeftHeader properties.

The following are the detailed steps:

  • Create an instance of the Workbook class.
  • Load an Excel file using Workbook.LoadFromFile() method.
  • Create a font and define a text.
  • Loop through all worksheets in the Excel file.
  • Call the DrawWatermarkImage() method to create a watermark image based on the text.
  • Set the image as the image source of the left header in each worksheet through Worksheet.PageSetup.LeftHeaderImage property.
  • Display the image in the left header section by setting the Worksheet.PageSetup.LeftHeader property to “&G”.
  • Change the view mode of the worksheet to page layout in order to view the watermark.
  • Save the result file using Workbook.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Xls;
using System.Drawing;

namespace AddWatermark
{
    class Program
    {
        static void Main(string[] args)
        {
            //initialize a new instance of the Workbook class and load an Excel file
            Workbook workbook = new Workbook();
            workbook.LoadFromFile(@"Sample.xlsx");

            //Create a font
            Font font = new Font("Arial", 40);
            //Define a text
            string watermark = "Confidential";

            //Loop through all worksheets in the file
            foreach (Worksheet sheet in workbook.Worksheets)
            {
                //Call the DrawWatermarkImage() method to create an image based on the text
                Image imgWtrmrk = DrawWatermarkImage(watermark, font, Color.LightCoral, Color.White, sheet.PageSetup.PageHeight, sheet.PageSetup.PageWidth);

                //Add the image to the left header section of each worksheet 
                sheet.PageSetup.LeftHeaderImage = imgWtrmrk;
                sheet.PageSetup.LeftHeader = "&G";

                //Change the view mode of the worksheet to page layout in order to view the watermark
                sheet.ViewMode = ViewMode.Layout;
            }

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

        private static Image DrawWatermarkImage(string text, Font font, Color textColor, Color backColor, double height, double width)
        {
            //Create an image with specified width and height
            Image img = new Bitmap((int)width, (int)height);
            //Create a Graphics object from the image
            Graphics drawing = Graphics.FromImage(img);

            //Get the size of the text
            SizeF textSize = drawing.MeasureString(text, font);

            //Change the origin of the coordinate system by prepending the specified translation to the transformation matrix of the Graphics
            drawing.TranslateTransform(((int)width - textSize.Width) / 2, ((int)height - textSize.Height) / 2);

            //Apply rotation
            drawing.RotateTransform(-45);

            //Change the origin of the coordinate system by prepending the specified translation to the transformation matrix of the Graphics
            drawing.TranslateTransform(-((int)width - textSize.Width) / 2, -((int)height - textSize.Height) / 2);

            //Paint the background
            drawing.Clear(backColor);

            //Create a brush for the text
            Brush textBrush = new SolidBrush(textColor);

            //Draw the text onto the center position of the Graphics
            drawing.DrawString(text, font, textBrush, ((int)width - textSize.Width) / 2, ((int)height - textSize.Height) / 2);
            drawing.Save();
            return img;
        }
    }
}

C#/VB.NET: Add a Watermark to Excel

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.