We know that Microsoft Excel does not have built-in functionality to add watermarks directly to Excel tables, but we can actually work around this problem in other ways, such as by adding a header picture or WordArt to mimic the appearance of the watermark. So in this article, I'll show you how to add a watermark to Excel by creating and inserting a header picture in Excel. I also shared how to add a watermark to a Word document and add a watermark to a PDF file, as well as the need for reference.
Here I downloaded a free version of the Excel component, E-iceblue, which was developed by the company, which saves time and simplifies the code.
After the control is installed, create the project, add the DLL file under the installation directory as a reference to the project, and add the following namespaces:
Using System;
Using System.Drawing;
Using System.Windows.Forms;
Using Spire.xls;
This is a screenshot of the original Excel table:
The following are detailed steps and code snippets:
Step 1: first define a DrawText () method, and create a picture based on the contents of the string. The string can be "confidential", "Draft", "Sample", or any text you want to display as a watermark.
private static System.Drawing.Image DrawText (String text, System.Drawing.Font Font, color textcolor, color BackColor, Dou ble height, double width) <br>{
//Create a bitmap image of the specified width and height the image
img = new Bitmap (int) width, (int) height);
Graphics drawing = Graphics.fromimage (IMG);
Gets the text size
SizeF textsize = drawing. MeasureString (text, font);
Rotate the picture
drawing. TranslateTransform ((int) width-textsize.width)/2, ((int) height-textsize.height)/2);
Drawing. RotateTransform ( -45);
Drawing. TranslateTransform ((int) width-textsize.width)/2,-((int) height-textsize.height)/2);
Draws a background
drawing. Clear (BackColor);
Create a text brush
Brush textbrush = new SolidBrush (textcolor);
Drawing. DrawString (text, font, Textbrush, ((int) width-textsize.width)/2, ((int) height-textsize.height)/2);
Drawing. Save ();
return img;
}
Step 2: initialize a new workbook and load the file that adds the watermark.
Workbook workbook = new Workbook ();
Workbook. LoadFromFile (@ "C:\Users\Administrator\Desktop\sample.xlsx");
Step 3: call the DrawText () method to create a new picture and set the header picture to left-aligned. Second, because the header picture is displayed when the view mode is layout, make sure you remember to change the view mode to layout.
Font font = new System.Drawing.Font ("Arial");
String watermark = "internal data";
foreach (worksheet sheet in workbook. Worksheets)
{
//Call DrawText () method new picture
System.Drawing.Image imgwtrmrk = DrawText (watermark, Font, System.Drawing.Color.LightCoral, System.Drawing.Color.White, sheet. Pagesetup.pageheight, Sheet. Pagesetup.pagewidth);
Set the header picture to left-aligned
sheet. Pagesetup.leftheaderimage = IMGWTRMRK;
Sheet. Pagesetup.leftheader = "&g";
Watermarks only show sheet in this mode
. ViewMode = viewmode.layout;
}
Step 4: save and open the file.
Workbook. SaveToFile ("watermark. xlsx", excelversion.version2010);
System.Diagnostics.Process.Start ("watermark. xlsx");
Effect Chart:
All code:
Using System;
Using System.Drawing;
Using System.Windows.Forms;
Using Spire.xls;
namespace Add_watermark_to_excel {public partial class Form1:form {public Form1 () {InitializeComponent (); } private void Button1_Click (object sender, EventArgs e) {//Initialize a new workbook and load the file to add the watermark Workbook workbook = new Wor
Kbook (); Workbook.
LoadFromFile (@ "C:\Users\Administrator\Desktop\sample.xlsx");
Inserts a picture in the header font font = new System.Drawing.Font ("Arial", 40);
String watermark = "internal data"; foreach (worksheet sheet in workbook. Worksheets) {//Call DrawText () method new picture System.Drawing.Image IMGWTRMRK = DrawText (watermark, Font, System.Drawing.Co Lor. Lightcoral, System.Drawing.Color.White, sheet. Pagesetup.pageheight, Sheet.
Pagesetup.pagewidth); Set the header picture to left-aligned sheet.
Pagesetup.leftheaderimage = IMGWTRMRK; Sheet.
Pagesetup.leftheader = "&g"; Watermarks only show sheet in this mode.
ViewMode = Viewmode.layout; } workbook.
SaveToFile ("watermark. xlsx", excelversion.version2010); System.Diagnostics.Process.Start ("watermark. xlsx"); } <br> private static System.Drawing.Image DrawText (String text, System.Drawing.Font Font, color textcolor, color
BackColor, double height, double width {//Create a bitmap image of the specified width and height the image img = new Bitmap (int) width, (int) height);
Graphics drawing = Graphics.fromimage (IMG); Gets the text size SizeF textsize = drawing.
MeasureString (text, font); Rotate the picture drawing.
TranslateTransform ((int) width-textsize.width)/2, ((int) height-textsize.height)/2); Drawing.
RotateTransform (-45); Drawing.
TranslateTransform ((int) width-textsize.width)/2,-((int) height-textsize.height)/2); Draws a background drawing.
Clear (BackColor);
Create a text brush Brush Textbrush = new SolidBrush (textcolor); Drawing.
DrawString (text, font, Textbrush, ((int) width-textsize.width)/2, ((int) height-textsize.height)/2); Drawing.
Save ();
return img;
}
}
}
Thank you for your browsing, I hope this article can bring you some help.