C # Add a watermark instance to Excel

Source: Internet
Author: User
Tags drawtext
How to add a watermark to excel in C #

We know that Microsoft Excel does not have built-in functionality to add watermarks directly to Excel tables, but there are other workarounds that can be used to solve this problem, 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. Before I also shared how to add watermarks to Word documents and PDF files to add a watermark method, there is a need to also reference.

Here I have downloaded a free version of the Excel component, E-iceblue company developed, 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 namespace:

Using system;using system.drawing;using system.windows.forms;using Spire.xls;

This is the original Excel table:

Here are the 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 images 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 the background drawing. Clear (BackColor); Create text brushes 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 added 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 a layout, be sure to remember to change the view mode to layout.

Font font = new System.Drawing.Font ("Arial", 40); String watermark = "internal data"; foreach (Worksheet sheet in workbook. Worksheets) {//Call DrawText () method to create a new picture System.Drawing.Image IMGWTRMRK = DrawText (watermark, Font, System.Drawing.Color.LightCoral, System.Drawing.Color.White, sheet. Pagesetup.pageheight, Sheet. Pagesetup.pagewidth); Sets the header picture to left-justified sheet. Pagesetup.leftheaderimage = IMGWTRMRK; Sheet. Pagesetup.leftheader = "&g"; The watermark will 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");

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) {//Initializes a new workbook and loads the file to be added to the watermark Workbook Workbook      = new Workbook (); Workbook.      LoadFromFile (@ "C:\Users\Administrator\Desktop\sample.xlsx");      Insert Picture in header font font = new System.Drawing.Font ("Arial", 40);      String watermark = "internal data"; foreach (Worksheet sheet in workbook. Worksheets) {//Call DrawText () method to create a new picture System.Drawing.Image IMGWTRMRK = DrawText (watermark, Font, SYSTEM.DR Awing. Color.lightcoral, System.Drawing.Color.White, sheet. Pagesetup.pageheight, Sheet.        Pagesetup.pagewidth); Sets the header picture to left-justified sheet.        Pagesetup.leftheaderimage = IMGWTRMRK; Sheet.        Pagesetup.leftheader = "&g"; The watermark will 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, Co Lor BackColor, double height, double width) {//Create a bitmap image of the specified width and height images img = new Bitmap ((int) width, (int) Heig      HT);      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 the background drawing.      Clear (BackColor);      Create text brushes 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 visit, I hope this article can bring you a certain help, thank you for the support of this site!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.