How to Insert watermark in Excel document in C#, VB.NET?
Syncfusion® Excel (XlsIO) library is a .NET Excel library used to create, read, and edit Excel documents. It also, converts Excel documents to PDF files. Using this library, you can insert a watermark in an Excel worksheet.
Steps to insert a watermark in an Excel document, programmatically:
- Create a new C# console application project.
Create a new C# console application project
- Install the Syncfusion.XlsIO.WinForms NuGet package as reference to your .NET Framework application from NuGet.org.
Install NuGet package to the project
- Add an Excel file to your project and make it an embedded resource using the following steps.
- In Visual Studio, click the Project menu and select Add Existing Item. Find and select the Excel file you want to add to your project.
- In the Solution Explorer window, right-click on the Excel file you just added to your project and select Properties from the popup menu. The Properties tool window appears.
- In the Properties window, change the Build Action property to Embedded Resource.
- Build the project. The Excel file will be compiled into your project’s assembly.
- In the same way, browse and add an image to your project and make it an Embedded Resource. This image is used as a watermark for the Excel document.
- You can insert a watermark in an Excel worksheet using the BackgroundImage property available under the IPageSetup interface.
C#
// Insert image watermark in the worksheet Stream imageStream = assembly.GetManifestResourceStream("Watermark.image.png"); worksheet.PageSetup.BackgoundImage = new Bitmap(imageStream);
VB.NET
'Insert image watermark in the worksheet Dim imageStream As Stream = assembly.GetManifestResourceStream("Watermark.image.png") worksheet.PageSetup.BackgoundImage = New Bitmap(imageStream)
To insert a text watermark, you need to take an image of the text and insert it in the background using the same BackgroundImage property.
- Include the following namespaces in the Program.cs file.
C#
using Syncfusion.XlsIO; using System.Drawing; using System.IO; using System.Reflection;
VB.NET
Imports Syncfusion.XlsIO Imports System.Drawing Imports System.IO Imports System.Reflection
- Include the following code snippet in the main method of the Program.cs file to insert a watermark in an Excel worksheet.
C#
using (ExcelEngine excelEngine = new ExcelEngine()) { // Initailize the excel application object IApplication application = excelEngine.Excel; // Load an existing Excel file into IWorkbook Assembly assembly = typeof(Program).GetTypeInfo().Assembly; Stream fileStream = assembly.GetManifestResourceStream("Watermark.Sample.xlsx"); IWorkbook workbook = application.Workbooks.Open(fileStream, ExcelOpenType.Automatic); // Get the first worksheet in workbook into IWorksheet IWorksheet worksheet = workbook.Worksheets[0]; // Insert image watermark in the worksheet Stream imageStream = assembly.GetManifestResourceStream("Watermark.image.png"); worksheet.PageSetup.BackgoundImage = new Bitmap(imageStream); // Save the Excel workbook workbook.SaveAs("Output.xlsx"); }
VB.NET
Using excelEngine As ExcelEngine = New ExcelEngine() 'Initailize the excel application object Dim application As IApplication = excelEngine.Excel 'Load an existing Excel file into IWorkbook Dim assembly As Assembly = GetType(Module1).GetTypeInfo.Assembly Dim fileStream As Stream = assembly.GetManifestResourceStream("Watermark.Sample.xlsx") Dim workbook As IWorkbook = application.Workbooks.Open(fileStream, ExcelOpenType.Automatic) 'Get the first worksheet in workbook into IWorksheet Dim worksheet As IWorksheet = workbook.Worksheets(0) 'Insert image watermark in the worksheet Dim imageStream As Stream = assembly.GetManifestResourceStream("Watermark.image.png") worksheet.PageSetup.BackgoundImage = New Bitmap(imageStream) 'Save the Excel workbook workbook.SaveAs("Output.xlsx") End Using
A complete working sample of how to insert watermark in Excel document can be downloaded from Watermark.zip.
By executing the program, you will get the output Excel document as follows.
Output Excel document
Take a moment to peruse the documentation, where you will find other options like move or copy a worksheet, freeze, unfreeze and split panes, show or hide worksheet and worksheet tabs, page setup settings and more with code examples.
Click here to explore the rich set of Syncfusion® Excel (XlsIO) library features.
Starting with v16.2.0.x, if you reference Syncfusion® assemblies from a trial setup or from the NuGet feed, include a license key in your projects. Refer to the link to learn about generating and registering the Syncfusion® license key in your application to use the components without a trial message.
Conclusion
I hope you enjoyed learning about how to insert watermark in Excel document in C#, VB,NET.
You can refer to our .NET Excel library page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our .NET Excel library example to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedbackportal. We are always happy to assist you!